I've been trying to come up with a better way to do this, so I'm hoping someone can help here.
I'm trying to step through an array (sourced from a mysql query), and whilst stepping through the array I want to generate HTML content based on what item in the array is currently showing (a 3x3 html table). I'm making a little gallery and my sql query pulls out 2 fields (gallery id and thumb).
here's the code:
$image = mysql_fetch_array($image_result);
while ($i <= 9) {
if ($i == 1) {
echo "<tr align=\"centre\" valign=\"top\">";
} else if ($i == 4) {
echo "<tr align=\"centre\" valign=\"top\">";
} else if ($i == 7) {
echo "<tr align=\"centre\" valign=\"top\">";
}
if (isset($image['thumb'])) {
echo "<td align=\"centre\"><a href=\"gallery_view_image.php?id=".$image['gallery_id']."\"><img src=\"image_viewer.php?path=gallery/thumbs/".$image['thumb']."\" border=\"0\"></a></td>";
} else {
echo "<td align=\"centre\"> </td>";
}
if ($i == 3) {
echo "</tr>";
} else if ($i == 6) {
echo "</tr>";
} else if ($i == 9) {
echo "</tr>";
}
$i ++; //increment internal counter
}
I know the code is bit rough. At the moment it creates the table nicely, I'm just not sure how to step through the array.
Any help would be greatly appreciated!
Edit: I know I can use:
while ($image = mysql_fetch_array($image_result))
to step through the elements in the array, but then it doesn't build my table nicely because there aren't always 9 images on the page... I want the code to build the table regardless of the number of images.