Forum Moderators: coopster

Message Too Old, No Replies

What's the best way to do this?

Using PHP to generate/fill an HTML table

         

BadGoat

10:21 pm on Mar 28, 2005 (gmt 0)

10+ Year Member



Hi! I am looking to create a page which will display all database entries sorted by date, where the date is today or later. I am not sure what to call it, so Googling for a tutorial hasn't been effective yet. What I forsee it looking like is this:

Date ¦ Name ¦ ID
3/1/05 ¦ Albert ¦ 003
4/9/05 ¦ Bob ¦ 001
5/5/05 ¦ Craig ¦ 002

I can make this in HTML easy enough, but I am not sure how to get PHP to fill in the table.. I hope this makes sense.. As I am not a professional programmer, I am not even sure how to accurately describe this. :)

I kick in the pants in the right direction would be most appreciated!

dreamcatcher

10:41 pm on Mar 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi BadGoat,

Something like this might get you started:


<table>
<tr>
<td>DATE</td>
<td>NAME</td>
<td>ID</td>
</tr>
<?php

$query = mysql_query("SELECT * FROM table") or die(mysql_error());

while ($row = mysql_fetch_assoc($query))
{
echo "<tr>\n";
echo " <td>" . $row['date'] . "</td>\n";
echo " <td>" . $row['name'] . "</td>\n";
echo " <td>" . $row['id'] . "</td>\n";
echo "</tr>\n";
}

?>
</table>

Good luck.

dc

BadGoat

10:46 pm on Mar 28, 2005 (gmt 0)

10+ Year Member



Hello again dc!

I follow ya, the part I get lost in is once there are more than one entry which I wish to display.. Sicne I only want to show entries which are current and now past, entries wiol fall off once they are in the past, so the # of rows will change daily. Must I do something special to allow the table to grow and shrink depending on the # of entries?

jatar_k

11:02 pm on Mar 28, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try this thread from our PHP Library
Create a Dynamic Table from mysql result [webmasterworld.com] msg 4

that will take care of the table but your sql query itself will control what you pull from the database. You will need to create a query that pulls only the information you want.

BadGoat

11:06 pm on Mar 28, 2005 (gmt 0)

10+ Year Member



Thank you jatar! I have the query part of it already planned.. Evne though it is just theoretical at the moment, I think the best way would be to echo all entries, and once I have that working I can edit the query to only post what is current.

Checking the link now!

BadGoat

11:47 pm on Mar 28, 2005 (gmt 0)

10+ Year Member



Hi Jatar, I looked at the post about generating a dynamic HTML table based on the sql query, and I have modified it as I think needed, but it is not outputting correctly. My SQL table does have 93 entries in it, and when I run the script it is creating 93 instances of <TD>, but I cannot get it to echo the variables within the SQL database. Here is ythe code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>NEW DIW</title>
</head>
<body>
<?PHP
$connect= mysql_connect("localhost","root")
or die("Could not connect to database in localhost!");
$result=mysql_select_db("testdiw")
or die("Could not select that database!");
$sqlquery = "SELECT timestamp, diwtitle, id from diw_alpha";
$queryresult = mysql_query($sqlquery) or die(" Could not execute mysql query!");

$tdcount = 1;
$numtd = 3; // number of cells per row
echo '<table border="0" width="" bgcolor="">
<tr>
<td colspan=3 bgcolor="#000080"><font face="Tahoma" size="5" color="white"> I-DEP - Current Depositions</font></td>
</tr>
<tr>
<td bgcolor="#ccccff" width=100><font face="Tahoma" size=2 color="black"><b> Date of Dep </b></font></td>
<td bgcolor="#ccccff" width=400><font face="Tahoma" size=2 color="black"><b> Title </b></font></td>
<td bgcolor="#ccccff" width=60><font face="Tahoma" size=2 color="black"><b> Dep ID </b></font></td>';
while($row = mysql_fetch_array($queryresult)) {
if ($tdcount == 1) echo "<tr>";
// echo "<td>some stuff: $tdcount</td>"; // display as you like
echo "<td>$diwtitle: $tdcount</td>"; // display as you like
if ($tdcount == $numtd) {
echo "</tr>";
$tdcount = 1;
} else {
$tdcount++;
}
}
// time to close up our table
if ($tdcount!= 1) {
while ($tdcount <= $numtd) {
echo "<td>&nbsp;</td>";
$tdcount++;
}
echo "</tr>";
}
echo "</table>";

jatar_k

12:02 am on Mar 29, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



here is the display line

echo "<td>$diwtitle: $tdcount</td>";
so you need to echo your vars in the cell

this is what is pulling each individual row
while($row = mysql_fetch_array($queryresult)) {

so since your query states this
SELECT timestamp, diwtitle, id from diw_alpha

you could do something like
echo "<td>id: ",$row['id'],"<br>title: ",$row['diwtitle'],"<br>time: ",$row['timestamp'],"</td>";

that would get your data in there

willybfriendly

12:25 am on Mar 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I read the OP correctly, this is a simple matter of putting a row of MySQL data into a row on a table.

Date ¦ Name ¦ ID
3/1/05 ¦ Albert ¦ 003
4/9/05 ¦ Bob ¦ 001
5/5/05 ¦ Craig ¦ 002

If so, an easy way might be:

$msg = "<table>";
while($row = mysql_fetch_array($queryresult))
{
$msg.= "<tr><td>$row[date]</td><td>$row[Name]</td><td>$row[ID]</td></tr>";
}
$msg.="</table>";
echo $msg;

I find that a little easier to read when I have to go back to the code months (or years) later.

WBF

BadGoat

12:34 am on Mar 29, 2005 (gmt 0)

10+ Year Member



Hi Jatar,

I got it! I changed the $numtd to 1 rather than 3 and changed the echo line to:

echo "<td>",$row['timestamp'],"</td><td>",$row['diwtitle'],"</td><td>",$row['id'],"</td>";

Works like a charm!

dreamcatcher

8:57 am on Mar 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad you got it sorted. :)