output query in already existing html table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
elmiku
Forum Newbie
Posts: 2
Joined: Tue Dec 20, 2005 1:48 am

output query in already existing html table

Post by elmiku »

Hi all

I'm relatively new to PHP and Mysql.
The problem for what I can't find the solution is the following:
I would like to make appear the 5 latest records of a "news" tabel (only 2 coloms: date and description) in 5 cells in an existing html tabel on a homepage written in html. So "date" and "description goes in the same cell. Is this possible with PHP???
I know how to make appear this query with the 5 latest records in a new tabel generated by PHP with a loop. But the question is: is there a way to make appear the dynamic data in an already existing html table because I don't want to rewrite the homepage with a lot of nested and formatted tables in PHP.
Any help is welcome!

Greetzzz
Kurt
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

First off you'd need to rename you .html file to .php so it can be parsed.

Lets say you had this table to start with:

Code: Select all

<table>
    <tr>
        <td>Header 1</td>
        <td>Header 2</td>
    </tr>
    <tr>
        <td>Some info</td>
        <some more info</td>
    </tr>
</table>
Now you decide you need to use PHP to add 5 rows from a database.

Code: Select all

<?php

$query = "select column1, column2 from tbl_name limit 5";
$result = mysql_query($query) or die(mysql_error());
//Now we break back into the usual HTML stuff

?>
<table>
    <tr>
        <td>Header 1</td>
        <td>Header 2</td>
    </tr>
    <tr>
        <td>Some info</td>
        <some more info</td>
    </tr>
<?php

//and back into php again for the loop

while ($row = mysql_fetch_assoc($result))
{
    echo "    <tr>
        <td>{$row['column1']}</td>
        <td>{$row['column2']}</td>
    </tr>\n";
}
//and again back into HTML to finish off

?>
</table>
elmiku
Forum Newbie
Posts: 2
Joined: Tue Dec 20, 2005 1:48 am

Am I wrong?

Post by elmiku »

Hi there ..
Thanks fot your reply !!
as far as I understand, this code is not putting data in the existing cells of the existing html table but generates a new table with the php code. Or am I wrong?
What I need is the code to put the 10 datastrings from my database in a php variable that I can use to put afterwards in an existing tablecell within a html page but I dont know how to write this php code :-(
greetzz
Post Reply