473,216 Members | 2,078 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,216 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 2909
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

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

Similar topics

by: NotGiven | last post by:
I have used dreamweaver to do a lot of php "programming" but now I need to write a real php function. I have a hosted mysql database that I need to export to MS Access. I can't use myODBC to do...
by: Jack | last post by:
I have a text file of data in a file (add2db.txt) where the entries are already entered on separate lines in the following form: INSERT INTO `reviews` VALUES("", "Tony's", "Lunch", "Great...
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
by: | last post by:
The following code: Private Sub ClearControls(ByVal ctrl As Control) Dim i As Int32 For i = ctrl.Controls.Count - 1 To 0 Step -1 ClearControls(ctrl.Controls(i))
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
by: Joe | last post by:
Hello All, I am trying to insert a record in the MS Access DB and for some reason I cannot get rid of error message, System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement. ...
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
by: cuties | last post by:
Hi all.... i'm very new to this programming language. i'm required to fulfill this task in the company i'm doing my practical. i hope i can get guide for my problem... Here is the script i...
by: DarkGiank | last post by:
Hi, im new to csharp and im trying to create a class that can change the application database without no rewriting all connection code... but cause some reason it is not working... it tells me that...
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.