开发者

mysql database data into html table limiting column

开发者 https://www.devze.com 2023-03-13 06:41 出处:网络
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 helpfu

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++;
开发者_C百科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.


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


$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>";
    ?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号