Q:

mysql database data into html table limiting column

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.

A:

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

