Page 1 of 1
PHP Table Loop
Posted: Sat Jul 29, 2006 6:20 pm
by phpnut
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.
Posted: Sat Jul 29, 2006 6:25 pm
by Ambush Commander
Check out the modulus operator.
Posted: Sat Jul 29, 2006 6:26 pm
by alex.barylski
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>';
}
Not sure if it's technically accurate, but you get the idea I hope

Posted: Sat Jul 29, 2006 6:27 pm
by feyd
check the first link in Useful Posts.

Posted: Sat Jul 29, 2006 7:59 pm
by phpnut
feyd | Please use 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
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]