Current location: Hot Scripts Forums » Programming Languages » PHP » mysql data in html table


mysql data in html table

Reply
  #1 (permalink)  
Old 02-14-08, 02:45 PM
zoliky's Avatar
zoliky zoliky is offline
Aspiring Coder
 
Join Date: Jun 2006
Posts: 537
Thanks: 0
Thanked 0 Times in 0 Posts
mysql data in html table

I have a table with 2 columns and 2 rows (2x2). Four cells exist (a,b,c and d):

Code:
a | b
--- ---
c | d
I need to list products like an e-commerce website. For example: In cell A I have another table with 3 columns and 3 rows (3x3).

To understand this, see: http://img216.imageshack.us/img216/9052/tablesy1.jpg

This is the HTML code:

Code:
	<table width="100%" border="0">
		<tr>
			<td>
			<table cellpadding="0" cellspacing="0">
				<tr>
					<td><img src="images/procesor.jpg" alt="[image]" /></td>
					<td>&nbsp;</td>
					<td>210 USD</td>
				</tr>
				<tr>
					<td bgcolor="#ff6600">Price</td>
					<td bgcolor="#ffcc99">&nbsp;</td>
					<td bgcolor="#ffcc99">210 USD</tr>
				</tr>
				<tr>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
				</tr>
			</table>
			</td>
			
			<td>
			<table cellpadding="0" cellspacing="0">
				<tr>
					<td><img src="images/procesor.jpg" alt="[image]" /></td>
					<td>&nbsp;</td>
					<td>210 USD</td>
				</tr>
				<tr>
					<td bgcolor="#ff6600">Price</td>
					<td bgcolor="#ffcc99">&nbsp;</td>
					<td bgcolor="#ffcc99">210 USD</tr>
				</tr>
				<tr>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
				</tr>
			</table>
			</td>
			
		</tr>
	</table>
Now I don't undestand how to use mysql_fetch_array to show all products.

Code:
$querydb = mysql_query("SELECT * FORM products");
while ($row = mysql_fetch_array($querydb)) {
}
The first product in cell A (row1) , the second product in cell B (row1), the third product in cell C (row 2), etc..

Someone help me a bit ? Please, any help is appreciated.
Reply With Quote
  #2 (permalink)  
Old 02-14-08, 03:03 PM
Walkere Walkere is offline
Newbie Coder
 
Join Date: Jan 2008
Location: New Jersey
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by zoliky View Post
Now I don't undestand how to use mysql_fetch_array to show all products.

Code:
$querydb = mysql_query("SELECT * FORM products");
while ($row = mysql_fetch_array($querydb)) {
}
The first product in cell A (row1) , the second product in cell B (row1), the third product in cell C (row 2), etc..
There are a couple of ways to handle this situation.

One way would be to cycle through the mySQL result, create an array of the result-set, and then reference that array in your table.

Here's an abbreviated example...
PHP Code:

<?php $resArray = array();

$result mysql_query("SELECT * FROM products");

while ( 
$row mysql_fetch_array($query) ) {
   
$resArray[] = $row;
?>

<table>
  <tr>
    <td><table> ...  use $resArray[0] to populate this table ... </table></td>
    <td><table> ...  use $resArray[1] to populate this table ... </table></td>
  </tr>
...
If you're unsure of how many results you're getting, then you could include the table creation inside your loop through the DB result-set.

Something like this...

PHP Code:

<?php 

echo "<table>";
$iteration 0;

$result mysql_query("SELECT * FROM products");
while (
$row mysql_fetch_array($result)) {
  if (
$iteration == 0) {
    
//  We're at an even iteration.  Open a table row
    
echo "<tr>"; }

   echo 
"<td>";

   
//  This is where you create the table and fill it with values from the $row array

   
echo "</td>";

  if (
$iteration == 1) {
    
//  We're at an odd iteration.  Close a table row
    
echo "</tr>"; }

   
$iteration++;
}

if (
$iteration == 1) {
   
//  Our last iteration in the table was even
   //  Which means we need to close the table row
   
echo "</tr>";
}

echo 
"</table>";
Good luck,
- Walkere
Reply With Quote
  #3 (permalink)  
Old 02-14-08, 03:21 PM
zoliky's Avatar
zoliky zoliky is offline
Aspiring Coder
 
Join Date: Jun 2006
Posts: 537
Thanks: 0
Thanked 0 Times in 0 Posts
Yahhooooo, thanks ! this help me a lot, work great
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting data to mysql table using php Tjobbe PHP 4 09-28-06 06:37 AM
Retrieving data from MySQL DB pork1977 Script Requests 4 06-13-05 10:26 AM
Advice parsing a html table to xml RossC0 JavaScript 3 03-22-05 02:10 PM
filling HTML table with MySQL data warrerj PHP 1 05-02-04 06:03 PM
html page or database table to send by email rpain ASP 5 04-05-04 09:04 AM


All times are GMT -5. The time now is 07:34 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.