gpt4 book ai didi

php - 将 MySQL 查询结果显示为每行的 HTML 表

转载 作者:行者123 更新时间:2023-11-30 22:49:42 26 4
gpt4 key购买 nike

我尝试搜索但没有找到任何对我有帮助的东西,所以我创建了一个新线程。

我有一个包含多个列的 MySQL 数据库。每行是一个项目的一组单独的数据。现在我已经设法将它显示得非常接近我想要的,但是为了获得更好的 View ,我需要将它格式化为一个双列表,其中第一列是数据库列的标题作为它的全名,第二列列包含响应值。

数据库格式:
| 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 全称xxxxxxxxxxxxxxxxxxxxx: tex4

Col1 全名x: tex5
Col2 全名xxxxxxxxxxx: tex6
Col3 全名xxxx: tex7
Col4 全名xxxxxxxxxxxxxxxxxxxxxx: tex8

http://oi58.tinypic.com/2el8ksl.jpg

“x”-s 给出了具有不同长度的列名称的视觉可比图像。但我想把它作为一个表格,其中“tex”值都从相同的距离开始,这将是最长的列名标签长度或表格第一列的预设宽度。

在视觉上我希望它看起来更像这样:

Col1 全名: | 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>
<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>

关于php - 将 MySQL 查询结果显示为每行的 HTML 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28519551/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com