Link to home
Start Free TrialLog in
Avatar of AntoniRyszard
AntoniRyszard

asked on

Building a html table from the database?

Hello,

I wondered if anyone could advise, I am trying to write some php code to access the data from the products table, and then display these products as 4 per row in the html table.

For example if there were 4 products there would be 1 html table row displaying the 4, and 10 products 3 rows.

I am not quite sure where to start with the display. I wondered anyone would be-able to offer any guidance to writing this type of nested loop?

Thank you

<?php
$Sproducts = "SELECT title, price, description, small_image, large_image FROM products";
$Srproducts = mysql_query($Sproducts);
                     
if($Srproducts && mysql_num_rows($Srproducts) > 0) {                                                    
?>        

<?php  
     while ($rproducts = mysql_fetch_assoc($Srproducts)) {                            
?>

<?php              
     }
}

?>
Avatar of Jeff Darling
Jeff Darling
Flag of United States of America image


The html table looks like this:

<br />
<table border="0">
<tr>
<td><img src="1.jpg" /></td>
<td><img src="2.jpg" /></td>
<td><img src="3.jpg" /></td>
<td><img src="4.jpg" /></td>
</tr>
<tr>
<td><img src="5.jpg" /></td>
<td><img src="6.jpg" /></td>
<td><img src="7.jpg" /></td>
<td><img src="8.jpg" /></td>
</tr>
<tr>
<td><img src="9.jpg" /></td>
<td><img src="10.jpg" /></td>
</tr>
</table>


The PHP code that generates the html above forces the table to wrap at 4 columns.


$results = mysql_query($sql) or die ('problem with db query' . mysql_error());

echo "<table border=\"0\">\n";
echo "<tr>\n";

$rowcnt = mysql_num_rows($results);
$row = 0;
$col = 0;

for($u=0;$u<$rowcnt;$u++) {

$col = $col + 1;

$ProdPic=mysql_result($results,$u,'ProdPic');
$ProdTitle=mysql_result($results,$u,'ProdTitle');

// start new row
if($col > 4) {
 $row = row + 1;
 $col = 1;
 echo "</tr>\n";
 echo "<tr>\n";
}


echo "<td><img src=\"$ProdPic\" /></td>\n";

}

echo "</tr>\n";
echo "</table>\n";

You can see a demo at http://www.polysyncronism.com/CodeShare/PHPHTMLTableDemo/phpsql1.php
Avatar of AntoniRyszard
AntoniRyszard

ASKER

Thank you, I will try the code with my database.

I was hoping to display the name and price of the product under each photo. Would adding another two rows under the image be simple to add to the existing loop?

I think you are looking for something like this?

<table border="1">
<tr>
<td><img src="1.jpg" /></td>
<td><img src="2.jpg" /></td>
<td><img src="3.jpg" /></td>
<td><img src="4.jpg" /></td>
</tr>
<tr align="center">
<td>one $1</td>
<td>two $2</td>
<td>three $3</td>
<td>four $4</td>
</tr>
<tr>
<td><img src="5.jpg" /></td>
<td><img src="6.jpg" /></td>
<td><img src="7.jpg" /></td>
<td><img src="8.jpg" /></td>
</tr>
<tr align="center">
<td>five $5</td>
<td>six $6</td>
<td>seven $7</td>
<td>eight $8</td>
</tr>
</table>

I'll build the PHP to create this later.  
Here is a demo of the above html: http://www.polysyncronism.com/CodeShare/PHPHTMLTableDemo/Table1.html
Thank you, I would be very interested to see the how to add an extra row of prices under the image.
Thank you, I would be very interested to look through the code to add a row under the image.

Could I ask if the code would be posted here, or on the site below?

www.polysyncronism.com/CodeShare/PHPHTMLTableDemo/Table1.html

Thank you

Take a look at this.  It may be a bit messy but it appears to work.  

You should get the general idea.

http://www.polysyncronism.com/CodeShare/PHPHTMLTableDemo/phpsql2.php
ASKER CERTIFIED SOLUTION
Avatar of Jeff Darling
Jeff Darling
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial