Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Aug 2011
    Posts
    192
    Thanks
    112
    Thanked 0 Times in 0 Posts

    How do I auto populate html table from database? Please help.

    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.

    Code:
    <!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>

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.

  • Users who have thanked Fumigator for this post:

    Buffmin (08-23-2011)

  • #3
    Regular Coder
    Join Date
    Aug 2011
    Posts
    192
    Thanks
    112
    Thanked 0 Times in 0 Posts
    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

  • #4
    New Coder
    Join Date
    Aug 2011
    Location
    Sydney
    Posts
    26
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Buffmin View Post
    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!

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You don't want to create an empty table like this:
    Code:
    <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, a fundamental building block a lot of pros use.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Regular Coder
    Join Date
    Aug 2011
    Posts
    192
    Thanks
    112
    Thanked 0 Times in 0 Posts
    Thank you guys. Got it working. Sincerely, Buffmin


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •