Jump to content

foreach loop or for loop?


Imaulle

Recommended Posts

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!

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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" : '';

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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" : '';
}

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.