I’ve been working on paginations for images only, and I can seem to get it to display the next four images. I’m still new at PHP I also may have done a bad job of searching to see if this has been solved already.
Well, then let’s start fresh. What exactly are you trying to do? I don’t quite get it from your code, because there are two different, unrelated pieces to it. Please try to be as specific as you can.
So you want to paginate the pages, not the images, but depending on whether they have images or not?
Then I would start with filtering the pages in the first place:
// don't know which page you are querying here, so I just take the children in this example:
$pageCollection = $page->children()->filterBy('hasImages', true)->paginate(4);
$pagination = $pageCollection->pagination();
I think we are almost on the right page so I’m sharing an image to make sure. Down at the bottom I want the next four images from the next four projects. Currently, I’m only getting one with the current code I’m using.
<?php
// get all page siblings
$siblings = $page->siblings();
// get the index nr. of the current page in the collection
$index = $siblings->indexOf($page);
// get the next four siblings (using the index as offset)
$next4Pages = $siblings->not($page)->offset($index)->filterBy('hasImages', true)->limit(4);
// loop through those pages and fetch the first image
foreach($next4Pages as $p) {
$img = $p->images()->first();
echo thumb($img, array('width' => 200));
}
?>
OMG, that worked. I have four images. One very last thing. I’m sorry to be a hassle. I don’t see how to enter my HTML in this format to make it workable for my layout. (I’m sorry)
I’ve seen to have it figured out. Thanks…
<?php
// get all page siblings
$siblings = $page->siblings();
// get the index nr. of the current page in the collection
$index = $siblings->indexOf($page);
// get the next four siblings (using the index as offset)
$next4Pages = $siblings->not($page)->offset($index)->filterBy('hasImages', true)->limit(4);
// loop through those pages and fetch the first image
foreach($next4Pages as $p) {
$img = $p->images()->first();
echo" <div class='col-12 col-sm-3'> <a href = '".$p->url()."'>" . thumb($img) . "</a> </div>";
}
<?php
// get all page siblings
$siblings = $page->siblings();
// get the index nr. of the current page in the collection
$index = $siblings->indexOf($page);
// get the next four siblings (using the index as offset)
$next4Pages = $siblings->not($page)->offset($index)->filterBy('hasImages', true)->limit(4);
// loop through those pages and fetch the first image
foreach($next4Pages as $p):
$img = $p->images()->first();
?>
<div class="col-12 col-sm-3">
<a href="<?= $p->url() ?>"><?= thumb($img) ?></a>
</div>
<?php endforeach ?>
It is not considered good style to echo HTML (unless you can’t really avoid it); it makes your code hard to read and is error prone.