Forum Moderators: coopster

Message Too Old, No Replies

Displaying a HTML table with PHP

         

Antnee

2:50 pm on Jul 13, 2004 (gmt 0)

10+ Year Member



Hi guys

I'm new to PHP and I'm trying to get my head around databases at the moment. So far I have a page that kinda takes the mickey and takes a few details from you, it then creates a cookie that can be removed later. It also prompts you to add the data you gave to a database.

All this works perfectly, but when it comes to displaying the results of the database, all I can show is the first entry in the table. Can someone please tell me how I'm supposed to see all of them?

I've read other articles but they don't make much sense to me.

BTW, the database details are as follows:

Database name: visualanteorg
Table name: people
Fields: id, name, shoe_size, job

As you will see if you choose to look at the pages, getting the information out is not a problem, it's just formatting it how I want it, so each entry appears under the other in the table.

Thanks guys!

[edited by: jatar_k at 5:39 pm (utc) on July 13, 2004]
[edit reason] no urls thanks [/edit]

Timotheos

3:17 pm on Jul 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld Antnee,

There's a very nice thread in the library [webmasterworld.com] here called Basics of extracting data from MySQL. [webmasterworld.com] Check that out. It shows you how to basically loop through the results and display them.

Tim

Antnee

6:09 pm on Jul 13, 2004 (gmt 0)

10+ Year Member



OK, I read through the article and I have managed to get the information I want to appear as plain text, which is OK, but what I really want is to be able to view this in a properly formatted table, not just text. I can draw a table and drop results into it but I cannot get it to automatically create the correct number of rows for how many details I have in there!

I'm sure it's really easy but I'm not grasping it yet!

jatar_k

6:27 pm on Jul 13, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld Antnee,

maybe try this approach
Query Result Arrangement [webmasterworld.com]

Antnee

6:54 pm on Jul 13, 2004 (gmt 0)

10+ Year Member



Not to worrym I figured my own way round which works a treat.

basically all I did was this:

$host = "***.***.***.***";
$user = "**********";
$pass = "**********";
$dbname = "**********";
$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);

$sql = "SELECT id, name, shoe_size, job, lastvisit FROM people";
$query = mysql_query($sql);
?>
<table width="700" border="1" cellspacing="0" cellpadding="0">
<tr>
<td>ID</td>
<td>Name</td>
<td>Shoe Size</td>
<td>Job Title</td>
<td>Time Visited</td>
<?php
while ($row = mysql_fetch_array($query)) {
echo "<tr><td>",$row['id'],"</td><td>",$row['name'],"</td><td>", $row['shoe_size'],"</td><td>",$row['job'],"</td><td>",$row['lastvisit'],"<tr>";
}
?>
</table>

All I did was drop </td><td> in between calling each entry from the database. I thought something like that would work. It may be crude but it works well in my head!

[edited by: jatar_k at 7:02 pm (utc) on July 13, 2004]
[edit reason] no personal urls thanks [/edit]

jatar_k

7:03 pm on Jul 13, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



glad you figured something out

you forgot to close the tr and td at the end

echo "<tr><td>",$row['id'],"</td><td>",$row['name'],"</td><td>", $row['shoe_size'],"</td><td>",$row['job'],"</td><td>",$row['lastvisit'],"</td></tr>";

Antnee

7:32 pm on Jul 13, 2004 (gmt 0)

10+ Year Member



So I did! How silly, yet it still works. Hmm, is it PHP or HTML that is being so forgiving? Either way, it works. Only thing I can't do, and I suspect it is because of the way I've done it, is change the font size in the cells, just doesn't work, no idea why!

jatar_k

7:52 pm on Jul 13, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> is it PHP or HTML that is being so forgiving

a blind guess is you are using IE

>> is change the font size in the cells

apply css font attributes to your tds, that should get it

Antnee

8:18 pm on Jul 13, 2004 (gmt 0)

10+ Year Member



>> apply css font attributes to your tds, that should get it

You lost me there!

BTW, I wouldn't stoop to IE's level! Check out the first page of the site and you'll see a link to my browser of choice!

Anyway, back to the point of the CSS... er, help! ;o)

[edited by: jatar_k at 8:28 pm (utc) on July 13, 2004]
[edit reason] no personal urls, thanks [/edit]

jatar_k

8:30 pm on Jul 13, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if you want to do some css, you can always wander over to the CSS Crash Course [webmasterworld.com]