Sinmok Posted October 3, 2014 Posted October 3, 2014 Don't know if you can already do this, but implementing PHPs array_chunk method would be pretty useful.. For example $list = $pages->find("template=something"); // Returns 5 pages $chunked = $list->chunk(3); print_r($chunked); /*prints*/ array( 0=> array( Page, Page, Page ) 1=> array( Page, Page ) ) Especially useful if you're trying to print pages into columns. Can you already do this? 4
netcarver Posted October 3, 2014 Posted October 3, 2014 (edited) This looks similar to (but a little simpler than) pagination to me. Have you tried a straight pagination solution to this? Otherwise, something like this might do if you don't need access to the $page objects themselves... $list = $pages->find('whatever'); $titles = array(); foreach ($list as $p) { $titles[] = $p->title; } $chunked = array_chunk($titles, 3); print_r($chunked); Even simpler, plus you get access to the object with all its data; $list = $pages->find('whatever'); $chunked = array_chunk($list->getArray(), 3); // Output plaintext titles by iterating over chunks and pages in chunks. // Access protected fields as needed using $page->fieldname and format as required. foreach ($chunked as $i => $chunk) { echo "Chunk $i\n"; foreach ($chunk as $page) { echo $page->title, "\n"; } } Edited October 3, 2014 by netcarver Updated second example to show how to access fields. 5
Gayan Virajith Posted October 3, 2014 Posted October 3, 2014 Thanks Sinmok and netcarver. It would be really useful when we are using a front-end framework like Bootstrap. <?php $list = $pages->find("template=basic-page"); // Returns 5 pages $chunked = array_chunk($list->getArray(), 3); ?> <?php foreach($chunked as $i => $row): ?> <div class='row'> <?php foreach($row as $item) : ?> <div class="col-md-4"> <?php echo $item->title; ?> </div> <?php endforeach; ?> </div> <?php endforeach; ?> 8
Sinmok Posted October 3, 2014 Author Posted October 3, 2014 I don't suppose there's a way to access the protected data array within the PageArray object?
netcarver Posted October 3, 2014 Posted October 3, 2014 @Sinmok Take a look at what Gayan posted above, it shows you how to do it. I've also updated my previous example to make it more obvious. 2
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now