[thelist] php/mysql/html

Peter Lowe pgl at instinct.org
Mon Aug 4 14:27:29 CDT 2003


On Aug 04, Roger Harness wrote:
> 'Volters. I have a two-parter, but I'm just going to ask the first part
> first.
> 
> I'm a beginning PHP'er, so hopefully I'm just sort of dense on this one.
> 
> I have a simple database, with movies, categories, actors, and personal
> ratings (1-4 stars).
> 
> I have a simple while loop that is supposed to go something like this:
> 
> <tr>
> <td>$title</td>
> <td>$category</td>
> <td>$actor_1</td>
> <td>$actor_2</td>
> <td><img src='images/$rating_star.gif'></td>
> </tr>
> 
> That last variable is $rating, (and I'm tring to concatenate the _star.gif).
> For now, $rating with contain either "1", "2", "3", or "4".
> 
> So if i did it correctly, I'd have an <a href='images/1_star.gif'> html tag.
> This *almost* works. But it won't. My output is <img src='images/.gif'>
> 
> Now I *can* mess around with the $rating data and have it contain 1_star.gif
> and use <img src='images/$rating'> and THAT will work fine.
> 
> But for what i'm doing, i'd really like to just have 1, 2, 3, 4 and
> concatenate (?) the _star.gif to the end. I've tried every combo of single
> quotes, double quotes, excaped quotes, etc etc, and i either just get <img
> src='images/_star.gif'> (or something similiar) or just get parsing errors.
> 
> This *should* be do-able, right? Any Suggestions/tips/advice?? Does this
> even make sense??

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>

<?	} ?>

Another way I used to use:

<? while (expand(mysql_fetch_array($result, MYSQL_ASSOC))) { ?>

<tr>
<td><?=$title?></td>
<td><?=$category?></td>

<?	} // ... etc...?>

This can leave a lot of random variables lying around that can cause
weird problems later in the script (say if you started using $title for
a footer including the title of the page, it might show the wrong
thing).

This is assuming that you're using mysql_fetch_array(), but the same
works for foreach() and other kinds of loops.

cheers,

Peter

-- 
The Czech Republic: Home of the world's finest beer.
Litres drunk by Czechs so far this year: 970,170,727.10

 - http://prague.tv/toys/beer/


More information about the thelist mailing list