[thelist] php/mysql/html - best way to loop data

Simon Willison cs1spw at bath.ac.uk
Mon Aug 4 16:05:52 CDT 2003

Hi Tom,

Monday, August 4, 2003, 8:27:48 PM, you wrote:
> --- Peter Lowe <pgl at instinct.org> wrote:
>> Here's how I would do it:
>> <? while ($listing = mysql_fetch_array($result, MYSQL_ASSOC)) { ?>
>> <tr>
>> <td><?=$listing['title']?></td>
>> <td><?=$listing['category']?></td>
>> <td><?=$listing['actor_1']?></td>
>> <td><?=$listing['actor_2']?></td>
>> <td><img src="images/<?=$listing['rating']?>_star.gif"></td>
>> </tr>
>> <? } ?>

> This is a really nice way to loop through a result set. Is there a
> "best practice" for this? I have been using the mysql_fetch_array()
> but not quite like this. Just curious.

I prefer to use <<<EOD syntax rather than relying on <?= short tags,
which are turned off in many PHP installations as they intefere with
XML processing directives. Also, mysql_fetch_assoc() is an alternative
to mysql_fetch_array which only returns indexed values. My database
looops end up looking like this:

while ($row = mysql_fetch_assoc($result)) {
print <<<EOD
<td><img src="images/{$listing['rating']}_star.gif"></td>


I don't know if you could call it "best practise" or not, but it works
for me :)



More information about the thelist mailing list