dennismonsewicz Posted August 29, 2010 Share Posted August 29, 2010 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 More sharing options...
jcbones Posted August 30, 2010 Share Posted August 30, 2010 Not sure you need the for loop in there. Link to comment https://forums.phpfreaks.com/topic/212038-foreach-and-for-loop-help/#findComment-1105025 Share on other sites More sharing options...
dennismonsewicz Posted August 30, 2010 Author Share Posted August 30, 2010 Hmmm what would I do instead of using a for loop? Link to comment https://forums.phpfreaks.com/topic/212038-foreach-and-for-loop-help/#findComment-1105029 Share on other sites More sharing options...
jcbones Posted August 30, 2010 Share Posted August 30, 2010 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 https://forums.phpfreaks.com/topic/212038-foreach-and-for-loop-help/#findComment-1105035 Share on other sites More sharing options...
dennismonsewicz Posted August 30, 2010 Author Share Posted August 30, 2010 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 https://forums.phpfreaks.com/topic/212038-foreach-and-for-loop-help/#findComment-1105037 Share on other sites More sharing options...
jcbones Posted August 30, 2010 Share Posted August 30, 2010 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 https://forums.phpfreaks.com/topic/212038-foreach-and-for-loop-help/#findComment-1105045 Share on other sites More sharing options...
dennismonsewicz Posted August 30, 2010 Author Share Posted August 30, 2010 Wow thats a lot closer than what I could have done tonight! Thanks man! Now its cutting off at 9 images but not continuing the foreach loop to show the rest of the images. Link to comment https://forums.phpfreaks.com/topic/212038-foreach-and-for-loop-help/#findComment-1105051 Share on other sites More sharing options...
dennismonsewicz Posted August 30, 2010 Author Share Posted August 30, 2010 And what is the difference between ++$i and $i++? Link to comment https://forums.phpfreaks.com/topic/212038-foreach-and-for-loop-help/#findComment-1105053 Share on other sites More sharing options...
dennismonsewicz Posted August 30, 2010 Author Share Posted August 30, 2010 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. Link to comment https://forums.phpfreaks.com/topic/212038-foreach-and-for-loop-help/#findComment-1105065 Share on other sites More sharing options...
jcbones Posted August 30, 2010 Share Posted August 30, 2010 And what is the difference between ++$i and $i++? ++$i increments the number BEFORE testing against it, $i++ would increment it AFTER the test runs. Link to comment https://forums.phpfreaks.com/topic/212038-foreach-and-for-loop-help/#findComment-1105070 Share on other sites More sharing options...
dennismonsewicz Posted August 30, 2010 Author Share Posted August 30, 2010 bump Link to comment https://forums.phpfreaks.com/topic/212038-foreach-and-for-loop-help/#findComment-1105204 Share on other sites More sharing options...
wildteen88 Posted August 30, 2010 Share Posted August 30, 2010 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 https://forums.phpfreaks.com/topic/212038-foreach-and-for-loop-help/#findComment-1105211 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.