Web Hosting Talk







View Full Version : Array, Loop and Table Question


bmiddleton
06-05-2003, 04:23 PM
I am trying to do something that seems very simple, but I can't get it to work. I want to call up a database, get my results in an array and display those results in a table. What I will be displaying will be images with text that is pulled from the database. My problem is that I want the table to have multiple rows and columns and I want to display the results, one per cell. I have some code going, but it is only returning the first result in the array. What am I missing. I will post my code below.

<html>
<head>
<title>photo gallery</title>
<table cellpadding="2" align="center" cellspacing="2" width="80%" border="0" align="center">
<?PHP

$mysql=mysql_connect('localhost', 'root', 'password');

if(!$mysql)
{
echo 'Cannot Connect To The Database.';
exit;
}

$mysql = mysql_select_db('website_db');
if(!$mysql)
{
echo 'Cannot Select Database.';
exit;
}
$query = mysql_query('select * from papers');
$num_rows = 4;
$photos_per_row = 5;
$photos = mysql_fetch_array($query);
$total_photos = mysql_num_rows($query);
$photos_per_page = $num_rows * $photos_per_row;

for ($row=0; $row < $num_rows; $row++){
print("<tr>\n");
for ($col=0; $col < $photos_per_row; $col++)
{
if($i < $total_photos)
{
print('<td align="center">'.$photos[0].'</td>');
} else {
print("<td></td>\n");
}
$i++;
}
print("</tr>\n");
}
?>
</table>
</body>
</html>

Right now I am simply trying to get one part of each line of the array. I will also want to call up $photos[1], etc.

Any and all help will be greatly appreciated. Thanks!!

-b

bmiddleton
06-05-2003, 06:14 PM
I figured it out on my own. I simply put the mysql_fetch_array inside the 'if($i < $total_photos)' and it works like a charm. I knew it would be simple.

Thanks for looking.

-b