By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,760 Members | 969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,760 IT Pros & Developers. It's quick & easy.

Formatting mysql_fetch_array results in HTML table

P: n/a
Hi,

This looks like a simple problem, but I just can't seem to wrap my
head around it.

I'm trying to build a table dynamically, based on a result set from
mysql. There will be 9 results for a full page, with 3 table rows of 3
table cells. One result per cell. But all of the pages won't be full.

Here's one of many ways I've tried to do it:

<html>
<head>
<title>test</title>
</head>
<body>
<table width="100%" border="1">
<?php

include_once('db.inc.php');

$query = "SELECT DISTINCT title FROM mytable LIMIT 9";
$result = mysql_query($query);

while ($row = mysql_fetch_array($result)) {
$title = $row['title'];
}

for ($i = 0; $i < 3; $i++) {
print ' <tr>' . "\n";

for ($e = 0; $e < 3; $e++) {
print ' <td align="center">' . "\n".
' title : ' . $title . "\n".
' </td>' . "\n";
}
print ' </tr>' . "\n";
}
?>
</table>
</body>
</html>

This prints out 3 rows of three like I hoped, but $title is the same
in all 9 cells. I also have tried it with the for loops inside the
while loop, which prints out 9 rows of 9 cells 9 times!

I know I'm probably missing something obvious, so maybe someone can
point out what I'm doing wrong?
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
I noticed that Message-ID: <s0********************************@4ax.com>
from bill contained the following:
This prints out 3 rows of three like I hoped, but $title is the same
in all 9 cells. I also have tried it with the for loops inside the
while loop, which prints out 9 rows of 9 cells 9 times!


Your problem is that you need to generate the table, whether or not you
have results. This means that the table generation code must exist
independently of the calls to the results array. Fortunately
mysql_fetch_array() steps to the next record each time it is called so
if you put it in a function it should insert consecutive records until
they are all used. However the table creation can continue.

Something like (untested):

<table width="100%" border="1">
<?php

include_once('db.inc.php');

$query = "SELECT DISTINCT title FROM mytable LIMIT 9";
$result = mysql_query($query);

function next_row() {
$row = mysql_fetch_array($result);
$title = $row['title'];
if($title==""){$title="&nbsp;";}
return $title;
}

for ($i = 0; $i < 3; $i++) {
print ' <tr>' . "\n";

for ($e = 0; $e < 3; $e++) {

print ' <td align="center">' . "\n".
' title : ' .next_row() . "\n".
' </td>' . "\n";
}
print ' </tr>' . "\n";
}
?>
</table>

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.