473,320 Members | 1,858 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,320 software developers and data experts.

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

8
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.jpg<td><tr>
<tr><td>DATA1 Stuff<td><td>DATA2 Stuff<td><tr>
<tr><tdDATA3 Stuff<td><tr><table>
</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.
Jan 18 '08 #1
10 2913
Atli
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");
  6.  
  7. # Query for data
  8. $sql = "SELECT * FROM `myTbl`";
  9. $result = mysql_query($sql, $db) or die("Query failed");
  10.  
  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>";
  23.  
This can then be made to create whatever output you need.
Jan 18 '08 #2
CDFTim
8
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.
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
Atli
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");
  7.  
  8. # Query for data
  9. $sql = "SELECT * FROM `myTbl`";
  10. $result = mysql_query($sql, $db) or die("Query failed");
  11.  
  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>
  23.  
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++) { ?>
  2.  
  3. <html>
  4. <body>
  5.   <font color="Red">Hello world!</font>
  6. </body>
  7. </html>
  8.  
  9. <?php } ?>
  10.  
This would print the entire HTML page five times.

Maybe this is more what you need?
Jan 19 '08 #5
CDFTim
8
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
CDFTim
8
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());
  3.  
  4. //Puts it into an array
  5. while($info = mysql_fetch_array( $data ))
  6. {
  7.  
  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. }
  14.  
Jan 20 '08 #7
Markus
6,050 Expert 4TB
I think I found my answer.....
[php]
//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>";
}[/php]
Congrats, please remember to use code=php tags when posting. :)
Jan 20 '08 #8
CDFTim
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
Markus
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
Atli
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

2
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...
15
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...
8
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 -------------------------------------------------------------------------------- ...
1
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))
6
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...
1
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. ...
0
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...
3
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...
1
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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.