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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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; ?>

Link to comment
Share on other sites

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;

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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