Forum Moderators: coopster

Message Too Old, No Replies

How to display each mysql row in a table column via PHP?

         

irock

3:32 pm on Dec 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



After I retrieved 3 rows from my mysql product database, I want to display each row in its own column, similar to what you see on this product comparison page. Each row also contains 15 different specs, FYI.

reviews.cnet.com/4504-5_7-0.html?id=30569389&id=20536585&id=30525842&id=30527150&tag=compare

I know how to display mysql result on a page, but this format presents major difficulty for someone with limited PHP knowledge like myself.

Could anyone shed light to this problem?

Thanks!

[edited by: jatar_k at 4:33 pm (utc) on Dec. 23, 2003]
[edit reason] shortened link for sidescroll [/edit]

daisho

4:20 pm on Dec 23, 2003 (gmt 0)

10+ Year Member



You cannot fully process each row then move onto the next because of the way you have to create the tables.

Have one process create your articles (ie what will go into a table cell) and store them in an array.

Then take that array and format it into html as you see fit.

daisho.

Birdman

5:27 pm on Dec 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's one way to do it:

$result = mysql_query("SELECT stuff FROM somewhere");
while ($row = mysql_fetch_array($result)) {
$product_name .= "<td>$row['product_name']</td>";
$release_date .= "<td>$row['release_date']</td>";
etc...
}

Then in the HTML:

<table>
<tr><td>Product Name</td><?=$product_name?></tr>
<tr><td>Release Date</td><?=$release_date?></tr>
etc...
</table>

travelbuff

5:32 pm on Dec 23, 2003 (gmt 0)

10+ Year Member



This is some some sample code from a script where I do what diasho is suggesting:

//start a while loop to to create an array called $row for each record that $matches
while ($row = mysql_fetch_array($matches)) {
$id = $row['siteid'] ;
$ad_title = $row['sitetitle'] ;
$desc = $row['sitedescription'] ;
$short_desc = substr($desc, 0, 100) ;
//make a display block to display the results on a html table row at a time

$display_block .= "

<tr>

<td width=\"25%\"><a href=\"http://$url/detail.php?annid=$id\"><strong>$ad_title</strong></a></td>

<td width=\"75%\">$short_desc ....<a href=\"http://$url/detail.php?annid=$id\"><em>read more</em></a></td>

</tr>" ;
} //close the while loop
// close the php and start a html table
?>
<table width="98%" border="1" align="center">
<tr>
<td width="23%"><strong>Title</strong></td>
<td width="77%"><strong>Description</strong></td>
</tr>
<? //open a php block to populate the table
echo "$display_block" ;
//close the php block and then the table
?>
</table>

HTH

Mark