php query database print to html table with headers
I have never programmed anything in php before and haven't touched html in 10 years. I could use some help. I am querying a postgresql database using php. I am trying to display my query results in a table format with headers like this:
first_name last_name employee_id
tom jones 111
bob barker 112
bill davis 113
Sample code I am trying to get to work corre开发者_开发知识库ctly:
echo("<table border=2");
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
foreach ($line as $col_value => $row_value) {
echo("<tr><td>$col_value</td><td>$row_value</td></tr>\n");
}
}
echo("</table>");
My formatting is being displayed like this:
first_name tom
last_name jones
employee_id 111
first_name bob
last_name barker
employee_id 112
first_name bill
last_name davis
employee_id 113
As you can see I am storing my query in an associative array.
Thanks for any help.
Looks like you might be missing a bracket on the opening table tag:
Try changing this:
echo("<table border=2");
to this:
echo('<table border="2">');
and see if that helps.
echo("<table border=2><tr><td>first_name</td><td>last_name</td><td>employee_id</td></tr>");
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
echo("<tr>");
foreach ($line as $col_value => $row_value) {
echo("<td>$row_value</td>");
}
echo("</tr>\n");
}
echo("</table>");
Or:
echo("<table border=2><tr><td>first_name</td><td>last_name</td><td>employee_id</td></tr>");
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
echo("<tr><td>".$line[0]."</td><td>".$line[1]."</td><td>".$line[2]."</td></tr>\n");
}
echo("</table>");
<?php
echo "<table width=100% border="1">";
while ($row = mysql_fetch_array($result))
{
$id= $row["id"];
$f_name= $row["f_name"];
echo "<tr><td>";
echo $id;
echo"</td>";
echo"<td>";
echo $f_name;
echo"</tr>";
}
echo"</table>";
?>
try this may help
echo "<table>\n";
echo("<table border=2");
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value => $row_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
break;
}
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
精彩评论