Help to use PHP file to read in data from mysql & write out Html

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CDFTim
    New Member
    • Jan 2008
    • 8

    Help to use PHP file to read in data from mysql & write out Html

    O.K. that was a long Title...
    Can you help / show me how I would......... I am going to long windedly try to paint this picture.
    Backround: I have an html page that has a marquee function in it to display a small photo and other information to scroll in a box on the web page.
    The marquee's info is a table with data that I now insert manually.
    I would like to be able to insert the data into it from a mysql database.
    The marquee works by reading anything that is enclosed in my named <DIV>'s.
    Inside the DIV's is the formatted table with the data.
    I would like to populate these DIV's with the data from the database and write an output file that can be included into the main page... BEFORE the main page is loaded. In other words I do not want the main page to be slow in loading due to trying to read the data as it loads.
    I would not mind doing a little back end PhP file stuff manually just to get this file written and in place for the main page to read when ready.
    The file that I need to output is just DIV statements with the table and cell formats already done just needing the data inserted into the proper cells....Like this....
    <DIV><table><tr ><td>Http://smallpicture.jp g<td><tr>
    <tr><td>DATA1 Stuff<td><td>DA TA2 Stuff<td><tr>
    <tr><tdDATA3 Stuff<td><tr><t able>
    </DIV>
    Then another <DIV> and as many as it takes to read the data base which may be 10 items or DiV's woth.
    If I can get that file written then I can include it into the marquee file which will be IFramed into the web page.
    Thanks for any help.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi. Welcome to TSDN!

    This sounds very doable.
    In fact, you could be describing the design of 99% of all PHP code in use today :)

    If your only goal is to take a MySQL table and display it as HTML, you could use something as simple as:
    [code=php]
    # Connect to db
    $db = mysql_connect(" host", "usr", "pwd")
    or die("MySQL connection failed");
    mysql_select_db ("dbName", $db)
    or die("Database selection failed");

    # Query for data
    $sql = "SELECT * FROM `myTbl`";
    $result = mysql_query($sq l, $db) or die("Query failed");

    # Display as HTML table
    echo "<table>";
    while($row = mysql_fetch_ass oc($result))
    {
    echo "<tr>";
    foreach($row as $col)
    {
    echo "<td>". $col ."</td>";
    }
    echo "</tr>";
    }
    echo "</table>";
    [/code]
    This can then be made to create whatever output you need.

    Comment

    • CDFTim
      New Member
      • Jan 2008
      • 8

      #3
      Thanks for responding.
      However, I was trying to just merge the data into an already detailed formatted table. But now looking at what you typed in and thinking about it a little more, I guess I would have to have the PHP echo all those table statements to get the up to 10 DIV statements produced.
      I was thinking to be able to just have a boiler plate formatted table made up and the php drop the selected fields into the proper cells. But then how would that rotate through the up to 10 records? hmmmm. I guess I'm thinking like the database is going to print this stuff out on paper.
      O.K.....
      Lets say I have a table named ADDRESS
      and I have the named Fields = PHOTO, NAME, STREET, CITY, STATE, ZIP
      and I have a table to include this info.
      <DIV>
      |~~~~~~~~~~~~~~ ~~~~~~~~~~~|
      | Insert PHOTO Field ------------ |
      | |
      |--------------------------------------------------|
      | Insert NAME Field |
      --------------------------------------------------|
      | STREET Field |
      --------------------------------------------------|
      | CITY------| STATE--------| ZIP----------- |
      --------------------------------------------------|</DIV>
      So you are saying that I have to echo all the Div and table info?
      What are the commands or the syntax to place these Data Fields in their places.
      its late and I may be babbling on now. but if you think you know what I mean then let me know.

      Comment

      • adamalton
        New Member
        • Feb 2007
        • 93

        #4
        I think I know what you mean... You want to make the table first, and THEN put all the values into it. That's not really possible (well it might be, but only in a very odd and pontless complex way!). I think you should go with the method shown above. You can always add a table row before the while loop with the table headers in it, and similarly after the while() loop at the bottom if you want anything at the base of the table.

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Ok. I'm not entirely sure I'm following you, but consider this.

          You can mix PHP into any HTML you already have, given that you change the file extension so your server knows to execute it as a PHP document.

          For example, I can change the last part of my previous example like this. It will produce exactly the same result my previous example produces.
          [code=php]
          <?php
          # Connect to db
          $db = mysql_connect(" host", "usr", "pwd")
          or die("MySQL connection failed");
          mysql_select_db ("dbName", $db)
          or die("Database selection failed");

          # Query for data
          $sql = "SELECT * FROM `myTbl`";
          $result = mysql_query($sq l, $db) or die("Query failed");

          # Display as HTML table
          ?>
          <table>
          <?php while($row = mysql_fetch_ass oc($result)) { ?>
          <tr>
          <?php foreach($row as $col) { ?>
          <td><?php echo $col; ?></td>
          <?php } ?>
          </tr>
          <?php } ?>
          <table>
          [/code]
          Closing a <?php block before closing a loop, as I do with both the loops in this example, and adding HTML before opening another <?php block and closing the loop, will result in the HTML inside the loop to be printed each loop.

          A more simple example:
          [code=php]
          <?php for($i = 0; $i < 5; $i++) { ?>

          <html>
          <body>
          <font color="Red">Hel lo world!</font>
          </body>
          </html>

          <?php } ?>
          [/code]
          This would print the entire HTML page five times.

          Maybe this is more what you need?

          Comment

          • CDFTim
            New Member
            • Jan 2008
            • 8

            #6
            Thanks for the info. after reading these post and others around the board. I am starting to get it now of what I have to do to Echo out all the table details to write the divs's.
            Now my question is this. In you example above. It looks as if the data will print out in a tabular form in the tables.
            I have the formatted table cells set up to accept the data from the Database table into these cells. What syntax will I use.
            for example a table named ADDRESS
            and I have the named Fields = PHOTO, NAME, STREET, CITY, STATE, ZIP
            how would I address the cells to echo out?
            Do I use echo "<td>".$ADDRESS .PHOTO ."</td>";
            echo "<td>". $ADDRESS.NAME." </td>";
            Is that how that is addressed?

            Comment

            • CDFTim
              New Member
              • Jan 2008
              • 8

              #7
              I think I found my answer.....
              [code=php]
              //Retrieves data from MySQL
              $data = mysql_query("SE LECT * FROM employees") or die(mysql_error ());

              //Puts it into an array
              while($info = mysql_fetch_arr ay( $data ))
              {

              //Outputs the image and other data
              Echo "<img src=http://www.yoursite.co m/images/".$info['photo'] ."> <br>";
              Echo "<b>Name:</b> ".$info['name'] . "<br> ";
              Echo "<b>Email:</b> ".$info['email'] . " <br>";
              Echo "<b>Phone:</b> ".$info['phone'] . " <hr>";
              }
              [/code]
              Last edited by Atli; Jan 20 '08, 10:14 PM. Reason: Added [code] tags.

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Originally posted by CDFTim
                I think I found my answer.....
                [php]
                //Retrieves data from MySQL
                $data = mysql_query("SE LECT * FROM employees") or die(mysql_error ());

                //Puts it into an array
                while($info = mysql_fetch_arr ay( $data ))
                {

                //Outputs the image and other data
                Echo "<img src=http://www.yoursite.co m/images/".$info['photo'] ."> <br>";
                Echo "<b>Name:</b> ".$info['name'] . "<br> ";
                Echo "<b>Email:</b> ".$info['email'] . " <br>";
                Echo "<b>Phone:</b> ".$info['phone'] . " <hr>";
                }[/php]
                Congrats, please remember to use code=php tags when posting. :)

                Comment

                • CDFTim
                  New Member
                  • Jan 2008
                  • 8

                  #9
                  I will do that But how do I do it. Ihave been trying to figure out what the buttons do
                  Code:
                  above the message box.

                  Comment

                  • Markus
                    Recognized Expert Expert
                    • Jun 2007
                    • 6092

                    #10
                    Originally posted by CDFTim
                    I will do that But how do I do it. Ihave been trying to figure out what the buttons do
                    Code:
                    above the message box.
                    All you do is, whatever language you intend to display code for, lets say html, you do:

                    [CODE =HTML]html code goes here...[/CODE]

                    Just take the space out before the =HTML

                    If you wanted to display PHP

                    [CODE =PHP]php code goes here...[/CODE]

                    :)

                    Comment

                    • Atli
                      Recognized Expert Expert
                      • Nov 2006
                      • 5062

                      #11
                      Yea, pretty much what Markus said.

                      You just do something like:

                      &#91;code] .. Plain text goes here... &#91;/code]
                      &#91;code=ht ml] ...HTML markup goes here... &#91;/code]
                      &#91;code=ph p] ...PHP code goes here... &#91;/code]

                      Comment

                      Working...