【发布时间】:2017-03-01 12:22:27
【问题描述】:
我对 MySQL/PHP 还是很陌生,我正在尝试编写循环遍历我的整个 SQL 表的代码。我能够做到这一点,为了清理我想使用 html 表来存储值以使事情看起来更整洁。我尝试将 HTML 代码添加到打印在 HTML 部分代码底部的字符串中,但表格边框不显示。我做错了什么,我该如何解决?代码和输出截图如下:
<?php
include("connection.php");
$query= "SELECT * FROM schedule";
$result = mysqli_query($link, $query);
$scheduletext="<table>";
if($result = mysqli_query($link, $query)) {
while ($row=mysqli_fetch_array($result)) {
$scheduletext="<tr><td>".$scheduletext.$row[1]."</td>";
$scheduletext="<td>".$scheduletext.$row[2]."</td>";
$scheduletext="<td>".$scheduletext.$row[3]."</td>";
$scheduletext="<td>".$scheduletext.$row[4]."</td></tr>";
}
}
$scheduletext=$scheduletext."</table>";
?>
<html>
<head>
<title>TastySnack - Production Schedule</title>
<link href="https://fonts.googleapis.com/css?family=Kaushan+Script" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="tasty.css">
</head>
<body>
<div id="top">
<div id="top-left">
TastySnack Production
</div>
<div id="top-right">
<img id="logo" src="images/TastysnackLogo.jpg">
</div>
</div>
<div id="split"></div>
<div id="schedule">
<?php
print_r($scheduletext);
?>
</div>
</body>
</html>
【问题讨论】:
-
使用连接:
$scheduletext .=(注意点) -
查看您的源代码,您应该会看到问题所在。
<tr><td><table>是不对的。 -
@chris85 我明白你在说什么!为什么它在开始时添加所有额外内容?
0000-00-0022oz Dark12ct 4oz Dark2016-01-01978524782016-01-02875421362016-01-03…………
因为您在每次迭代时将<td>添加到变量的开头。简单的例子,eval.in/663332,然后乘以你拥有的行数。扩展示例:eval.in/663337我不熟悉 eval.in,我假设它就像 jsbin?老实说我没有完全理解我的代码,我还在学习,玩弄它,它似乎可以用于输出 MySQL 表。