将 MySQL 查询结果显示为每行 HTML 个表
Display MySQL query result as HTML tables per row
我尝试搜索但没有找到任何对我有帮助的东西,所以我创建了一个新线程。
我有 MySQL 包含多个列的数据库。每行是一个项目的一组单独的数据。
现在我已经设法将它显示得非常接近我想要的,但是为了获得更好的视图,我需要将它格式化为两列 table 其中第一列是数据库列的标题作为其完整名称,第二列包含响应值。
数据库格式:
| col1 | col2 | col3 | col4 |
| tex1 | tex2 | tex3 | tex4 |
| tex5 | tex6 | tex7 | tex8 |
http://oi61.tinypic.com/23gzmec.jpg
现在我看到输出为:
Col1 全名x: tex1
Col2 全名xxxxxxxxxxx: tex2
Col3 全名xxxx: tex3
Col4 全名xxxxxxxxxxxxxxxxxxxxxx: tex4
第 1 列全名 x: tex5
Col2 全名xxxxxxxxxxx: tex6
Col3 全名xxxx: tex7
Col4 全名xxxxxxxxxxxxxxxxxxxxxx: tex8
http://oi58.tinypic.com/2el8ksl.jpg
"x"-s 给出了具有不同长度的列名的视觉可比图像。但我想把它作为 table ,其中 "tex" 值都从相同的距离开始,这将是最长的列名称标签长度或 [=61= 的第一列的预设宽度].
视觉上我希望它看起来更像这样:
第 1 列全名: | tex1
Col2 全名: | tex2
Col3 全名: | tex3
Col4 全名: | tex4
现在我的代码如下所示:
<?php
$servername = "mysqlsite";
$username = "admin";
$password = "password";
$dbname = "mydb";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT btitle, basin, busurl, bukurl, bimageurl, bauthor, bauthoremail, bauthorbio, bgenre, bsdesc, bldesc FROM book_info";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<strong>Title: </strong>" .$row["btitle"].
"<br><strong>ASIN: </strong>" .$row["basin"].
"<br><strong>US URL: </strong>" .$row["busurl"].
"<br><strong>UK URL: </strong>" .$row["bukurl"].
"<br><strong>Image: </strong>" .$row["bimageurl"].
"<br><strong>Author: </strong>" .$row["bauthor"].
"<br><strong>Author Bio: </strong>" .$row["bauthorbio"].
"<br><strong>Genre: </strong>" .$row["bgenre"].
"<br><strong>Short Description: </strong>" .$row["bsdesc"].
"<br><strong>Long Description: </strong>" .$row["bldesc"].
"<br><br><br><br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
我从 w3schools 获得了这段代码并对其进行了修改以满足我的需要,但是当我尝试将其更改为显示为 table.
时没有任何好事发生
有人可以帮忙吗?
您应该使用 HTML<table>
标记。在您的情况下,它看起来像这样:
<table>
<tr><!-- This is a table row -->
<td> <!-- This is a column inside a row -->
ASIN:
</td>
<td>
YourValueOrWhatever
</td>
</tr>
<!-- Include the same markup for all rows here -->
</table>
像这样...并相应地放置您的数据..
<table>
<tr>
<th> <!--your table headers -->
...
</th>
while($row = $result->fetch_assoc()) {
<tr>
echo '<td>'.$row["btitle"].'</td>';
echo '<td>'.$row["basin"].'</td>'; //your table data
echo '<td>'.$row["busurl"].'</td>';
...
</tr>
}
</table>
如果您需要表格数据,为什么不构建一个 table?如果不想看到边框,可以移除边框。这样,列单元格将调整为最长的标题,文本单元格将在此之后开始。
<table>
<tr>
<td>Col 1</td>
<td>tex1</td>
</tr>
<!-- Create other rows the same way -->
</table>
您要么将样式设置为与样式属性一致,要么创建 类 and/or id 并添加 css 样式 sheet.
多亏了 Moid Mohd,我才让它工作,但它需要一些额外的修补才能没有错误。所以我发布了工作代码,以便将来遇到此问题的人也可以看到解决方案。
echo "<table><tr><th>table headding</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td><strong>Title:</strong></td><td>" .$row["btitle"]."</td></tr>";
echo "<tr><td><strong>ASIN:</strong></td><td>" .$row["basin"]. "</td></tr>";
echo "<tr><td><strong>US URL:</strong></td><td>" .$row["busurl"]."</td></tr>" ;
echo "<tr><td><strong>UK URL:</strong></td><td>" .$row["bukurl"]."</td></tr>" ;
echo "<tr><td><strong>Image URL:</strong></td><td>" .$row["bimageurl"]."</td></tr>" ;
echo "<tr><td><strong>Author:</strong></td><td>" .$row["bauthor"]."</td></tr>" ;
echo "<tr><td><strong>Author Email:</strong></td><td>" .$row["bauthoremail"]."</td></tr>" ;
echo "<tr><td><strong>Author Bio:</strong></td><td>" .$row["bauthorbio"]."</td></tr>" ;
echo "<tr><td><strong>Genre:</strong></td><td>" .$row["bgenre"]."</td></tr>" ;
echo "<tr><td><strong>Short Description:</strong></td><td>" .$row["bsdesc"]."</td></tr>" ;
echo "<tr><td><strong>Long Description:</strong></td><td>" .$row["bldesc"]. "</td></tr>";
echo "<tr><td><br><br></td></tr>";
}
echo "</table>";
我尝试搜索但没有找到任何对我有帮助的东西,所以我创建了一个新线程。
我有 MySQL 包含多个列的数据库。每行是一个项目的一组单独的数据。 现在我已经设法将它显示得非常接近我想要的,但是为了获得更好的视图,我需要将它格式化为两列 table 其中第一列是数据库列的标题作为其完整名称,第二列包含响应值。
数据库格式:
| col1 | col2 | col3 | col4 |
| tex1 | tex2 | tex3 | tex4 |
| tex5 | tex6 | tex7 | tex8 |
http://oi61.tinypic.com/23gzmec.jpg
现在我看到输出为:
Col1 全名x: tex1
Col2 全名xxxxxxxxxxx: tex2
Col3 全名xxxx: tex3
Col4 全名xxxxxxxxxxxxxxxxxxxxxx: tex4
第 1 列全名 x: tex5
Col2 全名xxxxxxxxxxx: tex6
Col3 全名xxxx: tex7
Col4 全名xxxxxxxxxxxxxxxxxxxxxx: tex8
http://oi58.tinypic.com/2el8ksl.jpg
"x"-s 给出了具有不同长度的列名的视觉可比图像。但我想把它作为 table ,其中 "tex" 值都从相同的距离开始,这将是最长的列名称标签长度或 [=61= 的第一列的预设宽度].
视觉上我希望它看起来更像这样:
第 1 列全名: | tex1
Col2 全名: | tex2
Col3 全名: | tex3
Col4 全名: | tex4
现在我的代码如下所示:
<?php
$servername = "mysqlsite";
$username = "admin";
$password = "password";
$dbname = "mydb";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT btitle, basin, busurl, bukurl, bimageurl, bauthor, bauthoremail, bauthorbio, bgenre, bsdesc, bldesc FROM book_info";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<strong>Title: </strong>" .$row["btitle"].
"<br><strong>ASIN: </strong>" .$row["basin"].
"<br><strong>US URL: </strong>" .$row["busurl"].
"<br><strong>UK URL: </strong>" .$row["bukurl"].
"<br><strong>Image: </strong>" .$row["bimageurl"].
"<br><strong>Author: </strong>" .$row["bauthor"].
"<br><strong>Author Bio: </strong>" .$row["bauthorbio"].
"<br><strong>Genre: </strong>" .$row["bgenre"].
"<br><strong>Short Description: </strong>" .$row["bsdesc"].
"<br><strong>Long Description: </strong>" .$row["bldesc"].
"<br><br><br><br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
我从 w3schools 获得了这段代码并对其进行了修改以满足我的需要,但是当我尝试将其更改为显示为 table.
时没有任何好事发生有人可以帮忙吗?
您应该使用 HTML<table>
标记。在您的情况下,它看起来像这样:
<table>
<tr><!-- This is a table row -->
<td> <!-- This is a column inside a row -->
ASIN:
</td>
<td>
YourValueOrWhatever
</td>
</tr>
<!-- Include the same markup for all rows here -->
</table>
像这样...并相应地放置您的数据..
<table>
<tr>
<th> <!--your table headers -->
...
</th>
while($row = $result->fetch_assoc()) {
<tr>
echo '<td>'.$row["btitle"].'</td>';
echo '<td>'.$row["basin"].'</td>'; //your table data
echo '<td>'.$row["busurl"].'</td>';
...
</tr>
}
</table>
如果您需要表格数据,为什么不构建一个 table?如果不想看到边框,可以移除边框。这样,列单元格将调整为最长的标题,文本单元格将在此之后开始。
<table>
<tr>
<td>Col 1</td>
<td>tex1</td>
</tr>
<!-- Create other rows the same way -->
</table>
您要么将样式设置为与样式属性一致,要么创建 类 and/or id 并添加 css 样式 sheet.
多亏了 Moid Mohd,我才让它工作,但它需要一些额外的修补才能没有错误。所以我发布了工作代码,以便将来遇到此问题的人也可以看到解决方案。
echo "<table><tr><th>table headding</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td><strong>Title:</strong></td><td>" .$row["btitle"]."</td></tr>";
echo "<tr><td><strong>ASIN:</strong></td><td>" .$row["basin"]. "</td></tr>";
echo "<tr><td><strong>US URL:</strong></td><td>" .$row["busurl"]."</td></tr>" ;
echo "<tr><td><strong>UK URL:</strong></td><td>" .$row["bukurl"]."</td></tr>" ;
echo "<tr><td><strong>Image URL:</strong></td><td>" .$row["bimageurl"]."</td></tr>" ;
echo "<tr><td><strong>Author:</strong></td><td>" .$row["bauthor"]."</td></tr>" ;
echo "<tr><td><strong>Author Email:</strong></td><td>" .$row["bauthoremail"]."</td></tr>" ;
echo "<tr><td><strong>Author Bio:</strong></td><td>" .$row["bauthorbio"]."</td></tr>" ;
echo "<tr><td><strong>Genre:</strong></td><td>" .$row["bgenre"]."</td></tr>" ;
echo "<tr><td><strong>Short Description:</strong></td><td>" .$row["bsdesc"]."</td></tr>" ;
echo "<tr><td><strong>Long Description:</strong></td><td>" .$row["bldesc"]. "</td></tr>";
echo "<tr><td><br><br></td></tr>";
}
echo "</table>";