Reputation: 171
I have 10 images in my database. I would like to display 5 images per table row.
image1 - image 2 - image 3 - image 4 - image 5
(new row)
image6 - image7 - image8 - image9 - image10
How am I able to do this using MySQL and PHP?
My Code:
$query = mysql_query("SELECT * FROM gallery WHERE gallery_number='$gallery_number'");
while($fetch = mysql_fetch_assoc($query)){
$image_name = $fetch['image_name'];
$image_location = $fetch['image_location'];
echo '<table width="960">';
echo '<tr>';
echo '<td>'.'<img src="'.$image_location.'" alt="'.$image_name.'"/>'.'</td>';
echo '</tr>';
echo '</table>';
}
As you can see, the results only display in one huge row. The thing I must add is the rows will need to expand with the result.
Example; there maybe more than 10 images per result. There could be, say, 20 images, this would mean I would need 4 rows...
Upvotes: 0
Views: 7865
Reputation: 328
$query = mysql_query("SELECT * FROM gallery WHERE gallery_number='$gallery_number'");
echo '<table width="960">';
$i = 0; //first, i set a counter
while($fetch = mysql_fetch_assoc($query)){
//counter is zero then we are start new row
if ($i==0){
echo '<tr>';
}
//here we creating normal cells <td></td>
$image_name = $fetch['image_name'];
$image_location = $fetch['image_location'];
echo '<td>'.'<img src="'.$image_location.'" alt="'.$image_name.'"/>'.'</td>';
//there is a magic - if our counter is greater then 5 we set counter to zero and close tr tag
if ($i>5){
$i=0;
echo '</tr>';
};
$i++; //$i = $i + 1 - counter + 1
}
echo '</table>';
Upvotes: 1