Tamara
Tamara

Reputation: 39

html table to display php photo gallery

I need to display a photo gallery in a table, I want five pictures on each line, but can't find the way to insert </tr><tr> after each fifth picture. Here's my code:

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

}
?>

Upvotes: 0

Views: 1183

Answers (2)

Phil Rykoff
Phil Rykoff

Reputation: 12087

Replace the line

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

with

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

Then you place something like this into the for loop.

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

Upvotes: 1

David
David

Reputation: 4361

Untested Code

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

Upvotes: 0

Related Questions