Solved

Building a html table from the database?

Posted on 2007-03-28
7
199 Views
Last Modified: 2013-12-13
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              
     }
}

?>
0
Comment
Question by:AntoniRyszard
  • 4
  • 3
7 Comments
 
LVL 12

Expert Comment

by:Jeff Darling
ID: 18814280

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
0
 

Author Comment

by:AntoniRyszard
ID: 18819660
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?
0
 
LVL 12

Expert Comment

by:Jeff Darling
ID: 18824514

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
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:AntoniRyszard
ID: 18826002
Thank you, I would be very interested to see the how to add an extra row of prices under the image.
0
 

Author Comment

by:AntoniRyszard
ID: 18832560
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
0
 
LVL 12

Expert Comment

by:Jeff Darling
ID: 18834443

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
0
 
LVL 12

Accepted Solution

by:
Jeff Darling earned 90 total points
ID: 18836130
$sql = "SELECT * FROM $DBName WHERE $DBField>0 ORDER BY $DBField";

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


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


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

$ProdPicIdx=0;
$ProdPriceIdx=0;

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

$col = $col + 1;


 if($col == 1){
  echo "<tr>\n";
 }
 $ProdPic=mysql_result($results,$ProdPicIdx,'ProdPic');
 echo "<td><img src=\"$ProdPic\" /></td>\n";
 $ProdPicIdx = $ProdPicIdx + 1;

// start new row?

if($col > 3) {
  $row = row + 1;
  $col = 0;

  // Process Price and Title row

    echo "<tr>\n";
    for($x=0;$x<4;$x++){
      $ProdTitle=mysql_result($results,$ProdPriceIdx,'ProdTitle');
      $ProdPrice=mysql_result($results,$ProdPriceIdx,'ProdPrice');
      echo "<td align=\"CENTER\">".$ProdTitle." - $".$ProdPrice."</td>\n";
      $ProdPriceIdx = $ProdPriceIdx + 1;
    }
    echo "</tr>\n";

}

}

    // Process remaining Price and Titles for last row.
    echo "<tr>\n";
    for($x=0;$x<$col;$x++){
      $ProdTitle=mysql_result($results,$ProdPriceIdx,'ProdTitle');
      $ProdPrice=mysql_result($results,$ProdPriceIdx,'ProdPrice');
      echo "<td align=\"Center\">".$ProdTitle." - $".$ProdPrice."</td>\n";
      $ProdPriceIdx = $ProdPriceIdx + 1;
    }
      echo "</tr>\n";

echo "</table>\n";

mysql_close ($dbh);
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP not parsing ' character 12 46
How to fix Datetime in MySQL? 4 51
Split wordpress loop 35 56
How do I show metrics with PHP or Javascript 6 33
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question