neridaj Posted March 27, 2008 Share Posted March 27, 2008 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 Quote Link to comment Share on other sites More sharing options...
ucffool Posted March 28, 2008 Share Posted March 28, 2008 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. Quote Link to comment 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.