[英]Output sql query into html table
I'm trying to place the output of this PHP SQL query into a database table, but it is outputting all of the row data into one column. 我正在尝试将此PHP SQL查询的输出放置到数据库表中,但是它将所有行数据输出到一列中。
if(isset($_POST['submit'])) {
$name = htmlentities($_POST['name']);
$parts = explode(" ", $name);
$lastname = array_pop($parts);
$firstname = implode(" ", $parts);
$connection = mysql_connect("localhost", "user", "password");
mysql_select_db("shoretoshore", $connection);
$result = mysql_query("SELECT ship_no, shipment_id, arrival_date, origin, destination, lname, fname from shipment, captain WHERE captain.capt_id=shipment.capt_id AND captain.fname='$firstname' AND captain.lname='$lastname'", $connection);
echo '<table border="0" cellpadding="5px" cellspacing="1px" style="font-family:Verdana, Geneva, sans-serif; font-size:11px; background-color:#E1E1E1" width="100%">
<tr bgcolor="#FFFFFF" style="font-weight:bold">
<th>Shipment No.</th>
<th>Shipment Id.</th>
<th>Arrival Date</th>
<th>Origin</th>
<th>Destination</th>
<th>Last Name</th>
<th>First Name</th>
</tr>';
while ($row = mysql_fetch_row($result)) {
foreach ($row as $value)
print "<tr><td>"."{$value}"."</td></tr>";
echo "<br>";
}
echo "</table>";
}
How do I output the results of the query into an HTML table? 如何将查询结果输出到HTML表中?
You need to explain the issue you're having. 您需要解释您遇到的问题。 But from what I can see off the bat, you're looping through all the values of the row and outputting them as rows itself, instead of as a cell within the table row.
但是据我所知,您正在遍历该行的所有值,并将它们输出为行本身,而不是作为表行中的单元格输出。 The curly brackets around value are unnecessary as well, since you are concatenating the strings, you can just do
'<tr><td>'.$value.'</td></tr>'
OR "<tr><td>$value</td></tr>"
. 值周围的大括号也是不必要的,因为您要连接字符串,所以只需执行
'<tr><td>'.$value.'</td></tr>'
或"<tr><td>$value</td></tr>"
。 PHP will parse variables within a string if the string is double quoted. 如果字符串用双引号引起来,PHP将解析字符串中的变量。 I would also refrain from adding
<br>
tags as direct children of a table. 我还避免添加
<br>
标记作为表的直接子代。 If you need spacing between table rows, use padding and margins. 如果您需要在表行之间留有间距,请使用填充和边距。
You are putting the $value
inside quotation marks, it will be treated as string. 您将
$value
放在引号内,它将被视为字符串。
Try: 尝试:
while ($row = mysql_fetch_row($result)) {
echo '<tr>';
foreach ($row as $value)
{
echo "<td>".$value."</td>";
}
echo "</tr>";
}
try this 尝试这个
while ($row = mysql_fetch_row($result)) {
print "<tr><td>".$row[0]."</td></tr>";
echo "<br>";
}
The problem is that you're outputting a <tr>
tag for every row and column. 问题是您要为每行和每列输出一个
<tr>
标记。 You need to move the <tr>
outside the inner loop. 您需要将
<tr>
移到内部循环之外。
Technically, you don't need to concatenate "{$value}"
with the other two strings, but you really should pass $value
through htmlspecialchars()
to avoid producing incorrect HTML if the value contains any <
or &
characters. 从技术上讲,您无需将
"{$value}"
与其他两个字符串连接在一起,但实际上您应该通过htmlspecialchars()
传递$value
,以避免在值包含任何<
或&
字符的情况下产生错误的HTML。 Eg: 例如:
while ($row = mysql_fetch_row($result)) {
print '<tr>';
foreach ($row as $value) {
$v = htmlspecialchars ($value);
print "<td>$v</td>";
}
echo "</tr>\n";
}
Also, you're not supposed to have a <br>
element between table rows, which is why I replaced it with a newline above. 此外,您不应该在表行之间使用
<br>
元素,这就是为什么我在上面用换行符替换了它。 Personally, I would skip the closing </td>
and </tr>
tags since they are optional in HTML. 就个人而言,我会跳过
</td>
和</tr>
标记,因为它们在HTML中是可选的。
Note also, that the MySQL extension is deprecated and no longer maintained. 还要注意,不建议使用MySQL扩展,并且不再对其进行维护。 You should be using MySQLi or PDO these days.
这些天您应该使用MySQLi或PDO 。
while ($row = mysql_fetch_row($result)) {
echo '<tr>';
foreach ($row as $value)
{
echo "<td>".$value."</td>";
}
echo "</tr>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.