Web Analytics Made Easy -
StatCounter How do I auto populate html table from database? Please help. - CodingForum

Announcement

Collapse
No announcement yet.

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

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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.

    Comment


    • #3
      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

      Comment


      • #4
        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!
        “Don't pray for lighter burdens, but for stronger backs.”
        Deals in Sydney

        Comment


        • #5
          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.

          Comment


          • #6
            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.)
            Be yourself. No one else is as qualified.

            Comment


            • #7
              Thank you guys. Got it working. Sincerely, Buffmin

              Comment

              Working...
              X