471,863 Members | 1,149 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,863 software developers and data experts.

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....
<tr><td>DATA1 Stuff<td><td>DATA2 Stuff<td><tr>
<tr><tdDATA3 Stuff<td><tr><table>
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.
Jan 18 '08 #1
10 2681
5,058 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. # Connect to db
  2. $db = mysql_connect("host", "usr", "pwd")
  3.   or die("MySQL connection failed");
  4. mysql_select_db("dbName", $db)
  5.   or die("Database selection failed");
  7. # Query for data
  8. $sql = "SELECT * FROM `myTbl`";
  9. $result = mysql_query($sql, $db) or die("Query failed");
  11. # Display as HTML table
  12. echo "<table>";
  13. while($row = mysql_fetch_assoc($result))
  14. {
  15.   echo "<tr>";
  16.   foreach($row as $col)
  17.   {
  18.     echo "<td>". $col ."</td>";
  19.   }
  20.   echo "</tr>";
  21. }
  22. echo "</table>";
This can then be made to create whatever output you need.
Jan 18 '08 #2
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.
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.
| Insert PHOTO Field ------------ |
| |
| Insert NAME Field |
| STREET Field |
| CITY------| STATE--------| ZIP----------- |
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.
Jan 19 '08 #3
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.
Jan 19 '08 #4
5,058 Expert 4TB
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.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. # Connect to db
  3. $db = mysql_connect("host", "usr", "pwd")
  4.   or die("MySQL connection failed");
  5. mysql_select_db("dbName", $db)
  6.   or die("Database selection failed");
  8. # Query for data
  9. $sql = "SELECT * FROM `myTbl`";
  10. $result = mysql_query($sql, $db) or die("Query failed");
  12. # Display as HTML table
  13. ?>
  14. <table>
  15.   <?php while($row = mysql_fetch_assoc($result)) { ?>
  16.   <tr>
  17.     <?php foreach($row as $col) { ?>
  18.     <td><?php echo $col; ?></td>
  19.     <?php } ?>
  20.   </tr>
  21.   <?php } ?>
  22. <table>
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:
Expand|Select|Wrap|Line Numbers
  1. <?php for($i = 0; $i < 5; $i++) { ?>
  3. <html>
  4. <body>
  5.   <font color="Red">Hello world!</font>
  6. </body>
  7. </html>
  9. <?php } ?>
This would print the entire HTML page five times.

Maybe this is more what you need?
Jan 19 '08 #5
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?
Jan 20 '08 #6
I think I found my answer.....
Expand|Select|Wrap|Line Numbers
  1. //Retrieves data from MySQL
  2. $data = mysql_query("SELECT * FROM employees") or die(mysql_error());
  4. //Puts it into an array
  5. while($info = mysql_fetch_array( $data ))
  6. {
  8. //Outputs the image and other data
  9. Echo "<img src=http://www.yoursite.com/images/".$info['photo'] ."> <br>";
  10. Echo "<b>Name:</b> ".$info['name'] . "<br> ";
  11. Echo "<b>Email:</b> ".$info['email'] . " <br>";
  12. Echo "<b>Phone:</b> ".$info['phone'] . " <hr>";
  13. }
Jan 20 '08 #7
6,050 Expert 4TB
I think I found my answer.....
//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM employees") or die(mysql_error());

//Puts it into an array
while($info = mysql_fetch_array( $data ))

//Outputs the image and other data
Echo "<img src=http://www.yoursite.com/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>";
Congrats, please remember to use code=php tags when posting. :)
Jan 20 '08 #8
I will do that But how do I do it. Ihave been trying to figure out what the buttons do
Expand|Select|Wrap|Line Numbers
  1. above the message box.
Jan 22 '08 #9
6,050 Expert 4TB
I will do that But how do I do it. Ihave been trying to figure out what the buttons do
Expand|Select|Wrap|Line Numbers
  1. 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]

Jan 22 '08 #10
5,058 Expert 4TB
Yea, pretty much what Markus said.

You just do something like:

[code] .. Plain text goes here... [/code]
[code=html] ...HTML markup goes here... [/code]
[code=php] ...PHP code goes here... [/code]
Jan 22 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by NotGiven | last post: by
reply views Thread by NeoPa | last post: by
reply views Thread by YellowAndGreen | last post: by
reply views Thread by aboka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.