Jump to content

Array Chunk problem


Joss
 Share

Recommended Posts

I am trying to use array_chunk to split a pile of results from a table field into two columns

This is my code:

$testimonials = $pages->get("/")->testimonials;
$testcount = $testimonials->count();
$resultnum = ceil($testcount / 2);

echo $resultnum;
foreach(array_chunk($testimonials, $resultnum) as $list){

	echo "<div class='block block12 '>";
	echo "<p>hello column</p>";

	foreach($list as $test){
	echo "<p class='quote'>{$test->text}helo</p>";
	echo "<p class='from'>{$test->from}</p>";
	} //end inner foreach

	echo "</div>";
}

The problem, according to debug, is that it says that:

Warning: array_chunk() expects parameter 1 to be array, object given in /home/justright/public_html/site/templates/testimonials.php on line 30

Parameter 1 is $testimonials.

I am echoing out $resultnum, and that is giving the right value and I have used my selector without array_chunk and it is definitely outputting the content of the table field.

So why does it not work in the above case?

Joss

Link to comment
Share on other sites

Solved - I needed to tell it what it was:

$testimonials = $pages->get("/")->testimonials;
$testcount = $testimonials->count();
$resultnum = ceil($testcount / 2);

echo $resultnum;
foreach(array_chunk($testimonials->getArray(), $resultnum) as $list){

	echo "<div class='block block12 '>";
	echo "<p>hello column</p>";

	foreach($list as $test){
	echo "<p class='quote'>{$test->text}helo</p>";
	echo "<p class='from'>{$test->from}</p>";
	} //end inner foreach

	echo "</div>";
}

So, added getArray

Thanks to this little post as a clue from Netcarver

https://processwire.com/talk/topic/7803-implement-array-chunk/?p=75573


Aww! You beat me to it, LostKobrakai

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...