Jump to content

Foreach and For Loop Help


dennismonsewicz

Recommended Posts

I am working on a photo gallery script in which I need to show 9 images at a time.

 

Here is my script so far

 

<?php foreach($pictures->result() as $p): ?>
	<?php for($i = 1; $i <= 9; $i++) { ?>
    		<div class="item">
	     <ul>
	     	 <li><a href="images/gallery/<?=$p->category;?>/<?=$p->photo_name;?>" rel="example1" ><img src="images/gallery/<?=$p->category;?>/thumb_<?=$p->photo_name;?>" alt="#" /></a></li>
	     </ul>
	    </div><!-- end item -->
	<?php } ?>
<?php endforeach; ?>

 

At the moment the script is showing one image at a time 9 times and for every entry in my database.

 

I have been battling this for a little while now. Thanks in advance for all of your help!

Link to comment
https://forums.phpfreaks.com/topic/212038-foreach-and-for-loop-help/
Share on other sites

If $pictures->result() holds an array of 9 pictures, then you shouldn't need the for loop, just take it out.  Right now you are printing 9 images of the same picture.  Removing the for loop will let it move on to the next image in $pictures->result() after it displays the image.

Ah gotcha... I just tried it and thats right... I am just trying to show 9 images at a time because of a photo gallery I am working on. Its built to show 9 at a time, but if there is more than 9 then a sliver of the 10th image appears on the page... I just need the script to break at the 9 and then show the next set of 9 images.

Try something like:

<?php 
$i = 0;
foreach($pictures->result() as $p):  
  if(++$i == 9) break; ?>
          <div class="item">
           <ul>
               <li><a href="images/gallery/<?=$p->category;?>/<?=$p->photo_name;?>" rel="example1" ><img src="images/gallery/<?=$p->category;?>/thumb_<?=$p->photo_name;?>" alt="#" /></a></li>
           </ul>
          </div><!-- end item -->      
   <?php endforeach; ?>

Ok I think I know what I need to do...

 

Its the way a jquery slider is setup... there is an unordered list that has to have 9 items per list. So I have to figure out a way to show blocks of unordered lists with 9 items per.

Not sure by try

<?php

// $i is the counter
$i = -1;
foreach($pictures->result() as $p):  
    // if the counter is at zero start a new unordered list
    if(++$i == 0): 
        echo "    <ul>\n";
    endif;
?>
        <li><a href="images/gallery/<?=$p->category;?>/<?=$p->photo_name;?>" rel="example1" ><img src="images/gallery/<?=$p->category;?>/thumb_<?=$p->photo_name;?>" alt="#" /></a></li>
<?php
    // if the counter is at 9
    // stop the current unordered list
    if($i == 9):
        echo "    </ul>\n";
        // reset the counter
        $i = -1;
    endif;
endforeach;

// This is used to clean up invalid html markup for incomplete lists
if($i < 9):
    for( ; $i < 9; $i++):
        echo "        <li></li>\n";
    endfor;
    echo "    </ul>\n";
endif;

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.