jakebur01 Posted August 2, 2014 Share Posted August 2, 2014 I am trying to display two images per row. Will add in table tags later so it will look nicer. Anyway, it is leaving off the second image. So, I have 6 images and image number two is blank. $dirname = "Files/$listingid/images/"; $images = glob($dirname."*"); /* $html .= '<table width="100%">'; foreach($images as $image){ $html .= "<tr><td><center>"; $html .="<img src=\"$image\" width=\"300\">"; $html .= "</a></center></td>"; $html .= "</td></tr>"; } $html .= "</table>"; */ $countImages = count($images) ; $imagesPerRow = 2; for ($i = 0 ; $i < $countImages; $i++) { //display image here $image = $images[$i] ; $html .= "<img width = \"200\" src='$image'>" ; if ($i % $imagesPerRow == 0) { //have displayed an entire row $html .= '<br>' ; } } Quote Link to comment https://forums.phpfreaks.com/topic/290235-trouble-displaying-two-images-per-row/ Share on other sites More sharing options...
mac_gyver Posted August 2, 2014 Share Posted August 2, 2014 is the html that's being produced and output to the browser what you expect? if the problem is always the second image, does the first or second image name contain any html special characters in it that could be breaking the html? what are the file names and what is the html that's being output? Quote Link to comment https://forums.phpfreaks.com/topic/290235-trouble-displaying-two-images-per-row/#findComment-1486668 Share on other sites More sharing options...
jakebur01 Posted August 2, 2014 Author Share Posted August 2, 2014 It's only image #2. If I have 29 images, #2 is the only one missing. I looked at the source and the image is missing all together. <img width = "200" src='PW-Files/10/images/1255967188.jpg'><br><img width = "200" src='PW-Files/10/images/1279977704.jpg'><img width = "200" src='PW-Files/10/images/1831679637.jpg'><br><img width = "200" src='PW-Files/10/images/2652035406.jpg'><img width = "200" src='PW-Files/10/images/3335979548.jpg'><br><img width = "200" src='PW-Files/10/images/4176309676.jpg'><img width = "200" src='PW-Files/10/images/428132950.jpg'><br><img width = "200" src='PW-Files/10/images/5397313372.jpg'><img width = "200" src='PW-Files/10/images/5406659459.jpg'><br><img width = "200" src='PW-Files/10/images/6463512950.jpg'><img width = "200" src='PW-Files/10/images/6660251971.jpg'><br><img width = "200" src='PW-Files/10/images/6978163793.jpg'><img width = "200" src='PW-Files/10/images/7276068595.jpg'><br><img width = "200" src='PW-Files/10/images/775470584.jpg'><img width = "200" src='PW-Files/10/images/871558021.jpg'><br><img width = "200" src='PW-Files/10/images/8794863931.jpg'><img width = "200" src='PW-Files/10/images/8799152527.jpg'><br><img width = "200" src='PW-Files/10/images/9769868608.jpg'><img width = "200" src='PW-Files/10/images/9896351471.jpg'><br> Quote Link to comment https://forums.phpfreaks.com/topic/290235-trouble-displaying-two-images-per-row/#findComment-1486669 Share on other sites More sharing options...
nik_jain Posted August 2, 2014 Share Posted August 2, 2014 what does print_r ($images) show ? Is the missing image even there in the array ? Quote Link to comment https://forums.phpfreaks.com/topic/290235-trouble-displaying-two-images-per-row/#findComment-1486671 Share on other sites More sharing options...
mac_gyver Posted August 2, 2014 Share Posted August 2, 2014 the issue isn't that the second image is entirely missing, it's that the first row of output only has one image in it. the problem is because $i is a zero and - if ($i % $imagesPerRow == 0) is a true value. Quote Link to comment https://forums.phpfreaks.com/topic/290235-trouble-displaying-two-images-per-row/#findComment-1486672 Share on other sites More sharing options...
jakebur01 Posted August 2, 2014 Author Share Posted August 2, 2014 That's correct mac_gyver. There are not any missing images. I saw that after I used print_r ($images) like nik_jain suggested. How could I alter the code so that the first row will display two images? Quote Link to comment https://forums.phpfreaks.com/topic/290235-trouble-displaying-two-images-per-row/#findComment-1486673 Share on other sites More sharing options...
mac_gyver Posted August 2, 2014 Share Posted August 2, 2014 because the for() loop is what is incrementing the $i variable, you would need to put the if ($i % $imagesPerRow == 0) test before you output the image. this would result in an extra <br> before all the output or you would need to add a condition so that it only does this when $i is greater than zero. what i would do is use a foreach() loop instead of a for() loop and use a separate $i variable that gets incremented inside the loop. Quote Link to comment https://forums.phpfreaks.com/topic/290235-trouble-displaying-two-images-per-row/#findComment-1486674 Share on other sites More sharing options...
ginerjm Posted August 2, 2014 Share Posted August 2, 2014 Wrap your mod test in an if that tests if $I is not 0 Quote Link to comment https://forums.phpfreaks.com/topic/290235-trouble-displaying-two-images-per-row/#findComment-1486675 Share on other sites More sharing options...
nik_jain Posted August 2, 2014 Share Posted August 2, 2014 If I understand the problem correctly, then changing the forloop to: for ($i = 1 ; $i <= $countImages; $i++) { should help The issue being that - 0 % $any_number is always equal to 0 . If we start counting from 1 instead of zero, then we can avoid this effect. Quote Link to comment https://forums.phpfreaks.com/topic/290235-trouble-displaying-two-images-per-row/#findComment-1486681 Share on other sites More sharing options...
Solution CroNiX Posted August 2, 2014 Solution Share Posted August 2, 2014 if (($i+1) % $imagesPerRow == 0) Quote Link to comment https://forums.phpfreaks.com/topic/290235-trouble-displaying-two-images-per-row/#findComment-1486684 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.