[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:
<?php
while ($row = mysql_fetch_assoc($result)) {
print <<<EOD
<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>
EOD;
}
?>
I don't know if you could call it "best practise" or not, but it works
for me :)
Cheers,
Simon
--
http://simon.incutio.com/
More information about the thelist
mailing list