显示PHP照片库的HTML表


html table to display php photo gallery

我需要在一个表中显示一个图片库,我想每行五张图片,但无法找到在每五张图片后插入</tr><tr>的方法。下面是我的代码:

<?php
// table name
$tbl_name=gallery1;
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
while($rows= mysql_fetch_assoc($result)){
$id         = $rows['id'];
$path       = $rows['path'];
$image_name = $rows['image_name'];
$title      = $rows['title'];
?>

<img src="<?php echo $path."/".$image_name;?>" height="120"/>Name:<?php echo $title;?>
<?php
echo "<form action='pictry.php' enctype='multipart/form-data' method='post'>
<input name='file[]' type='hidden' value='".$image_name."' />
<input name='id' type='hidden' id='id' value='".$id."'/>
<input type='submit' name='button' id='button' value='Delete Picture' /></form>";
}
?>

替换行

while($rows= mysql_fetch_assoc($result)){

for($i = 0; $rows= mysql_fetch_assoc($result); ++$i) {

然后在for循环中放入如下内容:

if($i % 5 == 0) { /* insert stuff */ }

未测试代码

<?php
  $perrow = 5;
  $i = 0;
  echo '<table>';
  echo '<tr>';
  while($rows = mysql_fetch_assoc($result)) {
    echo '<td><img src=[grapresultfromrows] /></td>';
    ++$i;
    if($i == $perrow) {
      $i = 0;
      echo '</tr>';
      echo '<tr>';
    }
  }
  // If not a multiple of $perrow you need to add extra cells
  for($i; $i < $perrow; ++$i) {
   echo '<td></td>';
  }
  echo '</tr>';
  echo '</table>';
?>