Jump to content

Missing images when iterating array


neridaj

Recommended Posts

Hello,

 

I'm trying to build a 5 column table of divs containing images from a directory and I don't understand why there is always 2 images missing from the output. The correct number of divs show up but when I view the html output, the image name is not getting picked up from the loop for the first 2 divs. If I echo the $imgarr[$i]  in the foreach loop I get all of the images, but when the function goes into the for loop it skips 2 images.

 

function display_photos()
{
$propertyaddress = $_GET['pa'];
$userfolder = $_SESSION['valid_user'];
$dir    = 'members/' . $userfolder . '/' . $propertyaddress . '/';
$files = scandir($dir);
$i = 0;
foreach($files as $value) {
	// check for image files
	if(valid_image_file($value))
		// build image array
		$imgarr[$i] = $value;
		$i++;
}

$count = count($imgarr);	
for($j = 0; $j < $count; $j++) {
 	if($j == 0 || $j%5 == 0)
			// echo '<div class="lastrowcol"><img src="' . $dir . $imgarr[$j] . '" width="100" height="100" /></div>';
			// 	 			else
			// 	 		echo '<div class="floatright"><img src="' . $dir . $imgarr[$j] . '" width="100" /></div>';
		echo '<div class="lastrowcol">' . $imgarr[$j] . '</div>';
				else
			echo '<div class="floatright">' . $imgarr[$j] . '</div>';
}
}

 

Thanks for any help with this,

 

Jason

Link to comment
https://forums.phpfreaks.com/topic/98239-missing-images-when-iterating-array/
Share on other sites

I don't see anything wrong with the code. I did this to the code and got the expected output:

function display_photos()
{
/*$propertyaddress = $_GET['pa'];
$userfolder = $_SESSION['valid_user'];
$dir    = 'members/' . $userfolder . '/' . $propertyaddress . '/';
$files = scandir($dir);
$i = 0;
foreach($files as $value) {
	// check for image files
	if(valid_image_file($value))
		// build image array
		$imgarr[$i] = $value;
		$i++;
}
*/$imgarr = array('one','two','three','four','five');
$count = count($imgarr);	
for($j = 0; $j < $count; $j++) {
 	if($j == 0 || $j%5 == 0)
			// echo '<div class="lastrowcol"><img src="' . $dir . $imgarr[$j] . '" width="100" height="100" /></div>';
			// 	 			else
			// 	 		echo '<div class="floatright"><img src="' . $dir . $imgarr[$j] . '" width="100" /></div>';
		echo '<div class="lastrowcol">' . $imgarr[$j] . '</div>';
				else
			echo '<div class="floatright">' . $imgarr[$j] . '</div>';
}
}
display_photos();

// output (not source code output, which has all the div tags too)
one
two
three
four
five

So, check the source code to see how it may differ. Otherwise, before $count=count($imgarr); also add in:

echo '<pre>'; print_r($imgarr); echo '</pre>';

Just to double-check things.

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.