Imaulle Posted December 5, 2011 Share Posted December 5, 2011 I'm having a really bad brain fart and I cannot get the logic correct here lol. I need divs wrapped around every set of 5 images so if there is a total of 8 images then there would be 2 sets of divs (first with 5 images, second with 3 images) and if there was 12 images then there would be 3 sets of divs (first two with five images, last one with 2 images) etc etc What am I doing wrong and is there an easier way to do this? http://pastebin.com/QfsFxe4G Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/252496-foreach-loop-or-for-loop/ Share on other sites More sharing options...
Drongo_III Posted December 5, 2011 Share Posted December 5, 2011 If your images originate from an array how about: <?php //Do you array count to get number of values and set this to count $img= array(); //images array $img[] = "image1"; $img[] = "image2"; $img[] = "image3"; $img[] = "image4"; $img[] = "image5"; $img[] = "image6"; $img[] = "image7"; $img[] = "image8"; $img[] = "image9"; $img[] = "image10"; $img[] = "image11"; $img[] = "image12"; $count= count($img); echo "$count"; for ($i=0; $i<$count; $i++){ if ( $i==0){ echo "<div>"; } if ($i % 5 == 0 && !$i == 0){ echo "</div> <div>"; } echo $img[$i]; } echo "</div>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/252496-foreach-loop-or-for-loop/#findComment-1294536 Share on other sites More sharing options...
cyberRobot Posted December 5, 2011 Share Posted December 5, 2011 You wouldn't need the "!$i == 0" if you use an "ifelse()". <?php //... for ($i=0; $i<$count; $i++){ if($i==0) { echo "<div>"; } elseif($i % 5 == 0) { echo "</div> <div>"; } //... ?> Quote Link to comment https://forums.phpfreaks.com/topic/252496-foreach-loop-or-for-loop/#findComment-1294583 Share on other sites More sharing options...
Pikachu2000 Posted December 7, 2011 Share Posted December 7, 2011 Just move the opening <div> tag before start of the loop. Then after the loop check to see if you need to echo a closing </div> tag; if $i % 5 isn't 0, you need to. echo '<div>'; for( $i = 0; $i < $count; $i++ ) { echo $i . ' | '; // or whatever data you need to echo . . . echo $i % 5 === 0 ? "</div>\n<div>" : ''; } echo $i %5 !== 0 ? "</div>\n" : ''; Quote Link to comment https://forums.phpfreaks.com/topic/252496-foreach-loop-or-for-loop/#findComment-1295176 Share on other sites More sharing options...
cyberRobot Posted December 7, 2011 Share Posted December 7, 2011 Just move the opening <div> tag before start of the loop. Then after the loop check to see if you need to echo a closing </div> tag; if $i % 5 isn't 0, you need to. @Pikachu2000 - The code provided doesn't quite work. With the way it's set up, the first item is displayed in its own <div>. Also, an empty <div> is created when there is 1, 6, 11, etc. item(s). For what it's worth, neither solution takes into account the potential that the image array may be blank. Here is an updated solution: <?php //CREATE AN ARRAY FOR TESTING ($_GET['numItems'] is used to dynamically change the number of items in the array without changing the code) $img = array(); for($i=1; $i<=$_GET['numItems']; $i++) { $img[] = "image$i"; } //IF THERE ARE ITEMS IN THE ARRAY, DISPLAY THEM $count = count($img); if($count > 0) { for($i=0; $i<$count; $i++){ if($i == 0) { echo "<div>"; } elseif($i%5 == 0) { echo "</div>\n<div>"; } echo $img[$i]; } echo "</div>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/252496-foreach-loop-or-for-loop/#findComment-1295244 Share on other sites More sharing options...
Pikachu2000 Posted December 7, 2011 Share Posted December 7, 2011 Just move the opening <div> tag before start of the loop. Then after the loop check to see if you need to echo a closing </div> tag; if $i % 5 isn't 0, you need to. @Pikachu2000 - The code provided doesn't quite work. With the way it's set up, the first item is displayed in its own <div>. Also, an empty <div> is created when there is 1, 6, 11, etc. item(s). For what it's worth, neither solution takes into account the potential that the image array may be blank. Here is an updated solution: It wasn't meant to be a drop-in solution, rather it was a guideline, but still I should have tested it I suppose. The only thing needed to fix the empty div/lone item in a div issue is to add a $i !== 0 check to the ternary statement. As far as empty images in the array, IMO that should be prevented while building the array. The whole code block can simply be wrapped in an if( !empty($array) ) { conditional too, for obvious reasons. I didn't take into account however, that since the for() loop increments the value of $i, the value needs to be decremented before the modulus check in the last ternary to echo the final closing </div> tag . . . if( !empty($array) ) { echo '<div>'; $count = count($array); for( $i = 0; $i < $count; $i++ ) { echo $i . ' | '; // or whatever data you need to echo . . . echo $i % 5 === 0 && $i !== 0 ? "</div>\n<div>" : ''; } echo --$i % 5 !== 0 ? "</div>\n" : ''; } Quote Link to comment https://forums.phpfreaks.com/topic/252496-foreach-loop-or-for-loop/#findComment-1295271 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.