Forum Moderators: open
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>";
}
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 . . .
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>
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
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>";
?>
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
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]