...

View Full Version : How do I auto populate html table from database? Please help.



Buffmin
08-23-2011, 03:39 PM
I am trying to figure out the best way to accomplish this task. I am making a website for a restaurant. I would like the client to be able to update prices, add items etc, and I have a mysql form that they can use to automatically update a database table, but I am trying to find the best way to automatically populate my html tables (on page load) with the data from the database. (Or reference table cells to cells in the database). any help would be greatly appreciated!
I included my code which contains one table.



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<table id="Table" border="0" cellspacing="1" cellpadding="2" width="479px" height:100%;">
<tr style="height: 40px;">
<td width="209px"><p style="margin-bottom: 0px;"><b><span style="font-size: 16pt;">&nbsp;&nbsp; Pizzas</span></b></p></td>
<td width="65"><p align="center" style="margin-bottom: 0px;">Giant<br />
slice</p>
</td>
<td width="65"><p align="center" style="margin-bottom: 0px;">12&quot;<br />
Small</p></td>
<td width="65"><p align="center" style="margin-bottom: 0px;">16&quot;<br />
Large</p></td>
<td width="65"><p align="center" style="margin-bottom: 0px;">18x26&quot;<br />
Sheet</p></td>
</tr>
<tr style="height: 20px;">
<td><p style="margin-bottom: 0px;">&nbsp; Cheese</p></td>
<td><p align="center" style="margin-bottom: 0px;">&nbsp;$3.95</p></td>
<td><p align="center" style="margin-bottom: 0px;">&nbsp;$3.95</p></td>
<td><p align="center" style="margin-bottom: 0px;">&nbsp;$3.95</p></td>
<td><p align="center" style="margin-bottom: 0px;">&nbsp;$3.95</p></td>
</tr>
<tr style="height: 22px;">
<td><p style="margin-bottom: 0px;">&nbsp; Cheese & Pepperoni</p></td>
<td><p align="center" style="margin-bottom: 0px;">&nbsp;$1.25</p></td>
<td><p align="center" style="margin-bottom: 0px;">&nbsp;$1.25</p></td>
<td><p align="center" style="margin-bottom: 0px;">&nbsp;$1.25</p></td>
<td><p align="center" style="margin-bottom: 0px;">&nbsp;$1.25</p></td>
</tr>
<tr style="height: 21px;">
<td><p style="margin-bottom: 0px;">&nbsp; Extra Toppings</p></td>
<td><p align="center" style="margin-bottom: 0px;">&nbsp;$1.75</p></td>
<td><p align="center" style="margin-bottom: 0px;">&nbsp;$1.75</p></td>
<td><p align="center" style="margin-bottom: 0px;">&nbsp;$1.75</p></td>
<td><p align="center" style="margin-bottom: 0px;">&nbsp;$1.75</p></td>
</tr>
<tr style="height: 20px;">
<td><p style="margin-bottom: 0px;">&nbsp; Extra Cheese</p></td>
<td><p align="center" style="margin-bottom: 0px;">&nbsp;$4.95</p></td>
<td><p align="center" style="margin-bottom: 0px;">&nbsp;$4.95</p></td>
<td><p align="center" style="margin-bottom: 0px;">&nbsp;$4.95</p></td>
<td><p align="center" style="margin-bottom: 0px;">&nbsp;$4.95</p></td>
</tr>
</table>
</body>
</html>

Fumigator
08-23-2011, 03:52 PM
For this task you would use a server-side language such as PHP or ColdFusion. (PHP is ubiquitous so it's a common choice.)

Assuming you used PHP, you would use PHP's built-in MySQL functions to grab the data out of the database and output it to a browser.

There are many tutorials online on this subject; google to find your favorite one.

Buffmin
08-23-2011, 04:07 PM
Yes, I am getting somewhat comfortable with php and mysql. Do you think I would just let php auto-create the table, or could I first create an empty table (that fits nicely in my div) and reference each td cell to a variable from the database? I would prefer to the latter, unless..... I can specify the table dimensions when php creates it. (Just trying to head in the correct direction. Thank-you

kunz
08-23-2011, 07:16 PM
Yes, I am getting somewhat comfortable with php and mysql. Do you think I would just let php auto-create the table, or could I first create an empty table (that fits nicely in my div) and reference each td cell to a variable from the database? I would prefer to the latter, unless..... I can specify the table dimensions when php creates it. (Just trying to head in the correct direction. Thank-you

You can do both.

If you have an ever-growing database, defining <table> initially (even if it's echo "<table>" in php and then dynamically creating each row and column might be the way to go.

I pretty much never deviate from the above.

Good luck!

Fumigator
08-24-2011, 04:31 AM
You don't want to create an empty table like this:


<table>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
</table>


If that's what you meant. The only way you'd be able to go back later and insert text into a cell would be to manipulate the DOM through Javascript which is completely unnecessary.

Instead, build the table output as you select data out of your database. Even better, take the whole functionality of selecting data from your database out of your main script-- make it a separate PHP file to select your data and put it in a nice little array, and then let your output script loop through that array to generate your html table.

That kind of separation of duties is the beginning of what is called the model-view-controller architecture, (http://www.google.com/#sclient=psy&hl=en&source=hp&q=model-view-controller+php&pbx=1&oq=model-view-controller+php&aq=f&aqi=g1&aql=&gs_sm=e&gs_upl=50124l50516l1l50731l4l3l0l0l0l0l204l514l0.2.1l3l0&bav=on.2,or.r_gc.r_pw.&fp=1369d3b118875e74&biw=1286&bih=942) a fundamental building block a lot of pros use.

Old Pedant
08-24-2011, 04:58 AM
MVC with PHP! What a novel concept!

(Just joking, of course. Obviously a great idea. Just that I so seldom see it suggested for PHP...or for ASP, for that matter. Seems to be the realm of JSP and ASP.NET for the most part.)

Buffmin
11-22-2011, 07:41 PM
Thank you guys. Got it working. Sincerely, Buffmin



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum