Jump to content

do a for each with missing pictures


pwired
 Share

Recommended Posts

Hi,

I have a parent page with 30 child pages each child holding a max of 20 pictures. The thing is that there are now pictures missing that will be available in the future. The pictures are from cars with a production number. The pictures must stay in a sequence like first 001 then 002, 003 etc etc. So pictures 001 until 020 will be in the first child, pictures 021 until 040 will be in the second child, etc. etc. But like I said there are pictures missing now. So in the first child I have only pictures 001, 005 and 016. In the second child I have only pictures 025, 029 and 035. What I want to accomplish is to cycle through all the pictures and output 20 pictures on the front regardless of any missing pictures. I tried to use this code in this thread https://processwire.com/talk/topic/390-images-pagination/?do=findComment&comment=3195 but nothing seems to work so far. Any ideas about this ?

 

Link to comment
Share on other sites

I think I am missing something here. The first picture in child 1 is 001.jpg and the last picture is 020.jpg And then 021.jpg is the first picture in child 2 and 040.jpg is the last picture in child 2. And so on. In the same sequence the for each should fetch them out. But see my first post about the missing pictures. What I need is that the for each goes through the childs, skips the places in childs where pictures are missing and as soon as it has counted a total of 20 pictures outputs them to the front.

Link to comment
Share on other sites

You should be able to implement it the "other way round". If you know the actual number of all the images (both "missing" and not missing), and they are also named (or will be named) according to their position in the sequence, and you know that they are grouped by 20, then why don't you just create nested loops and work out your logic according to the current element you might find or not found uploaded to the actual page the iteration points to?

Link to comment
Share on other sites

Here's a snippet that should do what you need. I don't know of any pre-built solution for it, since all pagination features that I've seen want to work with Pages/PageArrays.

$limit = 20;
$offset = ($input->pageNum - 1) * $limit;
$images = array();
$total = 0;

// Iterating through all image pages, make sure to put the right page into $parentpage
foreach($parentpage->children("sort=sort") as $pg) {
	// If we're at or above our offset and do still need more images to output
	if(count($images) < $limit && $pg->images->count() + $total >= $offset) {
		// If offset is above total count so far, calculate start, else go from beginning
		$start = ($offset > $total) ? $offset - $total : 0;
		// How many images do we still need?
		$missing = $limit - count($images);
		// Add up to $limit images to our array, perhaps you might need to call
		// $pg->image->sort("name") beforehand to get images in the right order
		$images = array_merge($images, array_slice($pg->images->getArray(), $start, $missing));
	}
	$total += $pg->images->count();
}

// Output images
foreach($images as $img) {
	echo "<img src='$img->url'><br>\n";	
}

$prevlink = $nextlink = "";

// Build prev link only if we aren't at first page
if($input->pageNum > 1)
	$prevlink = "<a href='{$page->url}page" . ($input->pageNum - 1) . "/'>< Prev</a>";

// Build next link only if there are more images to display
if($total > $input->pageNum * $limit  )
	$nextlink = "<a href='{$page->url}page" . ($input->pageNum + 1) . "/'> Next ></a>";

?>
<div style='border: 1px solid grey;'><?= $prevlink ?> ... <?= $nextlink ?></div>

 

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...