Jump to content

Trouble displaying two images per row


Go to solution Solved by CroNiX,

Recommended Posts

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

	
}
Link to comment
https://forums.phpfreaks.com/topic/290235-trouble-displaying-two-images-per-row/
Share on other sites

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?

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>

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.

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. 

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.