PHP Table Loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpnut
Forum Newbie
Posts: 5
Joined: Sat Jul 29, 2006 6:17 pm

PHP Table Loop

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Check out the modulus operator.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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 :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

check the first link in Useful Posts. :)
phpnut
Forum Newbie
Posts: 5
Joined: Sat Jul 29, 2006 6:17 pm

Post by phpnut »

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]
Post Reply