0

My script is retrieving images from a database and displaying the images in a table. I want to have the table have 4 columns of images before it breaks the row and starts over. I've found some helpful answers on this forum but after re-organizing and fussing with the code it displays every image in it's own table rather than adding the row breaks after every fourth image. I'm running on little sleep, but hopefully a second pair of eyes could help me spot the problem.

<?php
include_once "connect.php";
$userid = $_SESSION['id'];
$albumid = $_GET['album'];
$pic = mysql_query("SELECT * FROM `pictures` WHERE userid='$userid' AND
albumid='$albumid'");
$i = 0;
echo "<center><table width='50%'><tr>";
while($row = mysql_fetch_assoc($pic)){
$id = $row["id"];
$thumbnail = $row["thumbnail"];
echo "<td><a href='viewphoto.php?photo=$id'><img src='$thumbnail'>  
</a></td>";
if ($i && $i%4 == 0) echo '</tr><tr>';
$i++;
echo "</tr><tr>";
}
echo "</table> </center>";
?>

Fiddled around a little bit and

while($row = mysql_fetch_assoc($pic)){
$id = $row["id"];
$thumbnail = $row["thumbnail"];
if ($i && $i%4 == 0) echo '</tr><tr>'; 
$i++;
echo "<td><a href='viewphoto.php?photo=$id' rel='facebox'><img src='$thumbnail'>
</a></td>";}

worked like a charm.

2 Answers 2

2

The problem is that you are declaring your table inside the while loop. You should open and close your table tags on either side of the while loop, and only have tr and td's inside the loop.

There seem to be a couple of interchanged variable names here too that would cause unexpected behaviour. In your SQL query I think you mean ...albumid='$albumid'... and in the while loop I think you want while($row = mysql_fetch_assoc($pic)) {

Also, you should take note that this SQL query is open to SQL injection attacks

Sign up to request clarification or add additional context in comments.

1 Comment

I can't believe that I didn't see that, and the variable names were all matching, I was just cleaning it up for the site but (stupidly) interchanged some. Thank you!
1

$i was initialized inside the loop. and Table also bring outside.

see the code below.

     <?php
        include_once "connect.php";
        $userid = $_SESSION['id'];
        $albumid = $_GET['album'];
        $pic = mysql_query("SELECT * FROM `pictures` WHERE userid='$userid' AND
        albumid='$pic'");
       $i = 0;
    echo "
    <center><table width='50%'><tr>";

        while($row = mysql_fetch_assoc($image)){
    $id = $row["id"];
    $thumb = $row["thumb"];
    $date = strftime("%b %d, %Y", strtotime($row['date']));
    echo "<td><img src='$thumbnail'></td>";

    if ($i && $i%4 == 0) echo '</tr><tr>';
      $i++;
    }
    echo "</table> </center>";
    ?>

3 Comments

I think you meant $row = mysql_fetch_assoc($pic) instead of $row = mysql_fetch_assoc($image)? variable names $pic and $albumid are confusing. see also nicja's note about SQL injection attacks
Yeah, I'd feel pretty silly if my problem was just interchanged variables.. lol. I appreciate the input, and Ajay, thank you for the corrected code, but for some reason even with the table columns and rows being the only thing inside the while loop, it's still being applied after every input, instead of after 4 =/
yeah, Now I corrected. @Matthew please see and verify the changes now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.