Welcome to WebmasterWorld Guest from 50.19.53.104

Forum Moderators: incrediBILL

Message Too Old, No Replies

Populating a table using php

php & html tables

     

garywhite glw

9:00 pm on Dec 23, 2009 (gmt 0)

5+ Year Member



Hi

I have a need to get php to create a table a single column, multi-row table to display visitors details. At the moment I can only achieve the reverse - the visitors details appear as multi-columns in a single row table. No probs with the data retrieval side or MySQL. Any help gratefully received....

Cheers in advance
Gary

PS Code at moment looks like this:

while($row=mysql_fetch_array($result)){

echo "</td><td>";

echo $row['Firstname'];

echo "</td><td>";

echo $row['email'];

echo "</td><td>";

echo $row['Phone'];

echo "</td></tr>";

}

chasehx

9:07 pm on Dec 23, 2009 (gmt 0)

5+ Year Member



Each table row needs to be opened.
It would be much easier like this

<?
while ($row = mysql_fetch_array($res)){
?>
<tr>
<td><? echo $row['Firstname']; ?></td>
<td><? echo $row['email']; ?></td>
<td><? echo $row['Phone']; ?></td>
</tr>
<?
}
?>

rocknbil

10:11 pm on Dec 23, 2009 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Welcome aboard garywhite glw, there are many ways to do this. The prevous would never be my preference (sorry chasehx) becasue it switches between "plain output" and PHP with a high frequency, which makes it very hard to debug and follow in large acripts.

create a table a single column, multi-row table to display visitors details.

I'd do something more like this, but will add what a lot of people like - alternate color between rows.

// Define these in your style sheet
$white_bg = 'white-bg'; // i.e., .white-bg { background:#fff; }
$gray_bg = 'gray-bg'; // i.e., .gray-bg { background:#d8d8d8; }
// Note also class "hd": .hd { background:#000; color:#fff; text-align:center; }

$output = NULL;


while($row=mysql_fetch_array($result)){
$thisClass=($thisClass==$gray_bg)?$white_bg:$gray_bg;
$output .= '
<tr>
<td class="' . $thisClass . '">' .
$row['Firstname'] . '<br>' .
'<a href="mailto:' .
$row['email'] . '">' .
$row['email'] . '</a><br>' .
$row['Phone'] . '</td>
</tr>';
}


if ($output) {
echo '
<table align="center">
<tr><td class="hd">Name,Email,Phone</td></tr>' .
$output .
'</table>';
}
else { echo '<p>Oops, no records found.</p>'; }

Typed on the fly, may need some debugging . . .

garywhite glw

10:22 pm on Dec 23, 2009 (gmt 0)

5+ Year Member



Wow - that was quick! Only just joined! Thanks very much - will try the both and report back.

Cheers

garywhite glw

11:45 pm on Dec 23, 2009 (gmt 0)

5+ Year Member



Hi again

Have tried the first code and all it does is replicate a three-columned 1 row table....)-:

Even tried my own jigging about with it but still couldn't get anything but the above. Any ideas Chasehx?

StoutFiles

11:57 pm on Dec 23, 2009 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



If you want more rows you need to use more <tr> tags.

Example: Three column, three row table.

<table>
<tr>
<td>row 1, column 1</td>
<td>row 1, column 1</td>
<td>row 1, column 1</td>
</tr>
<tr>
<td>row 2, column 1</td>
<td>row 2, column 2</td>
<td>row 2, column 3</td>
</tr>
<tr>
<td>row 3, column 1</td>
<td>row 3, column 2</td>
<td>row 3, column 3</td>
</tr>
</table>

garywhite glw

12:10 am on Dec 24, 2009 (gmt 0)

5+ Year Member



Still not happening...

heres the code as it stands:

<?php
while ($row = mysql_fetch_array($result)){
<table>
<tr>
<td> $row['Firstname'] </td>
</tr>
<tr>
<td> $row['email'] </td>
</tr>
<tr>
<td> $row['Phone'] </td>
</tr>
</table>
}
?>

Starting to get a headache - feel one of my turns coming on - VB was never this hard

Cheers
Gary

deejay

12:31 am on Dec 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi gary

Can you give us an example of the table as you would like it to look - just the html and fields.

One point - if you want to output multiple records from your database into a single table, then you need to open and close the table OUTSIDE the while that rolls through the records.

As you have it at present:

<?php
while ($row = mysql_fetch_array($result)){
<table>
<tr>
<td> $row['Firstname'] </td>
</tr>
<tr>
<td> $row['email'] </td>
</tr>
<tr>
<td> $row['Phone'] </td>
</tr>
</table>
}
?>

...You're going to create a table for each person. - "<table>" and "</table>" are inside the while, so they'll be repeated with each record.

whereas the following will create one table, with all your records:

<?php
echo "<table>";
while ($row = mysql_fetch_array($result)){

echo "<tr><td>".$row['Firstname']." </td></tr><tr><td>".$row['email']." </td></tr><tr><td> ".$row['Phone']." </td></tr>";

}

echo "</table>";

?>

garywhite glw

12:44 am on Dec 24, 2009 (gmt 0)

5+ Year Member



Hey Deejay

Your code is the dog's....works a treat. Probably my mistake for not explaining myself properly. Can I be greedy? Would love to be able to incorporate the following into the setup as is:

<div id="wb_Table1" style="position:absolute;left:260px;top:230px;" align="left">
<table border=1 style="background-color:#F0F8FF;" >
<caption><EM>Visitor Record</EM></caption>

Top man
thanx

deejay

12:58 am on Dec 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



no probs. Just slot it into the echos where it would normally appear in your html code.

The only thing to be aware of is because you use double quotes to enclose what you want php to echo, if you then actually want double quotes to appear in the html code when it is echoed you have to clarify that by 'escaping' the quotes, ie, you put a BACK slash (edit - had front slash incorrectly) in front of the quote to tell the php engine to process those quotes as just quote symbols and not to take them as an indication of where the echo starts and stops.

eg:

<?php
echo "<div id=\"wb_Table1\"><table>";
while ($row = mysql_fetch_array($result)){

etc..

:) I'll let you nut out the rest.

NB - single quotes work in some instances as an alternative to double quotes, both in html and php, but honestly I've not got the rules down for where and when is appropriate for each, though there are plenty here could clarify.

[edited by: deejay at 1:54 am (utc) on Dec. 24, 2009]

garywhite glw

1:07 am on Dec 24, 2009 (gmt 0)

5+ Year Member



Will try that - off to bed as its 2 in the morning here and my head's nipping.

Will post further tomorrow.

Big thank you to everyone who helped me - very impressed by the turnout.

Thanx
Gary

rocknbil

2:59 am on Dec 24, 2009 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I don't get it. Post #3 does exactly what you're asking without bloating the code with a new row for every value. <shrug>
 

Featured Threads

Hot Threads This Week

Hot Threads This Month