Hey guys, I'm in a bind and thought this'd be the best place to ask for help.
I have a MySQL database containing image URLs that I need displayed in a table that is three images wide (for thumbnails). My dilemma is that I cannot think of how to create a loop that will stop after three rows of the database, add variables into HTML table code, and then restart to do another three rows. Any kind of help would be greatly appreciated.
PHP Table Loop
Moderator: General Moderators
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Code: Select all
$res = mysql_query('SELECT * FROM tbl_images');
$cnt = mysql_num_rows($res);
for($i=0; $i<$cnt; $i++){
echo '<tr>';
// Print out 3 columns per row/line
for($cols=0; $cols<3; $cols++){
$row = mysql_fetch_array($res);
echo '<td>';
echo $row['title'];
echo '</td>';
}
echo '</tr>';
}feyd | Please use
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Thanks guys, but I'm still having a problem. Nothing's being outputted for some reason...Code: Select all
<?php
$gallery = strip_tags($_GET['gallery']);
$dbhost = "****";
$dbuser = "****";
$dbpass = "****";
$db = "siteinfo";
$connect = mysql_connect($dbhost,$dbuser,$dbpass)
or die("No can do");
$selectdb = mysql_select_db($db,$connect)
or die("sorry");
$query = "SELECT id,thumb,gallery FROM photographs WHERE gallery = '$gallery'";
$res = mysql_query($query)
or die("haha");
$cnt = mysql_num_rows($res);
echo "<table border='0' cellspacing='1' cellpadding='1'>";
for($i=0; $i<$cnt; $i++)
{
echo "<tr>";
for($cols=0; $cols<3; $cols++)
{
$row = mysql_fetch_array($res,MYSQL_ASSOC);
echo '<td>';
echo $row['id'];
echo '</td>';
}
echo '</tr>';
}
echo "</table>";
?>feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]