【发布时间】:2019-04-29 03:13:04
【问题描述】:
我需要使用 php 变量和 while 循环在表中显示行,但我不知道我做错了什么。我尝试了各种不同的单引号和双引号组合,但我仍然无法使用正确的语法来使这些行在表中输出而不会产生任何错误。我正在使用 Dreamweaver 2019 对其进行编码。我使用了 Netbeans 8.2,但我仍然无法弄清楚这段代码的正确语法。
到目前为止,这也是我在 stackoverflow 上找到的内容,但我仍然没有找到我真正需要的内容。在这种情况下,我也无法准确找到如何使用谷歌使用正确的语法:
php - for loop inside a while loop, correct syntax?
https://stackoverflow.com/search?q=use+inline+styling+with+php+html+table
<?php
include 'connection.php'; // includes the connection.php file to connect
to the database
// query to database and prepares and executes
$query = "SELECT id, first_name, last_name FROM customers ORDER BY
last_name asc";
$stmt = $db->prepare($query);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($id, $first_name, $last_name);
// count the number of customers
$total_customers = $stmt->num_rows;
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Granger Customers</title>
<link href="assets/bootstrap.min.css" rel="stylesheet" type="text/css">
</head>
<body>
<!--start container to center design in browser-->
<div class="container">
<div class="row" style="margin-bottom:30px">
<div class="col-xs-12">
<ul class="nav nav-pills">
<li class="nav-item">
<a class="nav-link" href="index.php">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="customers.php">Customers</a>
</li>
<li class="nav-item">
<a class="nav-link" href="">Search</a>
</li>
<li class="nav-item">
<a class="nav-link" href="">Add New</a>
</li>
</ul>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<table class="table table-bordered table-striped table-hover">
<p><strong>There are a total of <?PHP echo $total_customers; ?>
customers.</strong></p> <!-- output total number of customers -->
<thead>
<tr class="success">
<th class="text-center">#</th>
<th>Customer ID</th>
<th>Last Name</th>
<th>First name</th>
<th class="text-center">Details</th>
</tr>
</thead>
<tbody>
<tr>
<?php
while($result = $stmt->fetch()) {
echo '<th class="text-center">'#'</th>'
echo <th>"$result"</th>
echo <th>"$result"</th>
echo <th>"$result"</th>
echo <th class="text-center">"Details"</th>
}
?>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<!--end container to center design in browser-->
</body>
</html>
【问题讨论】:
-
一些分号似乎是一个好的开始
-
将error reporting添加到文件顶部测试时在您打开PHP标签之后,例如
<?php error_reporting(E_ALL); ini_set('display_errors', 1); -
这条线看起来有点奇怪!
echo '<th class="text-center">'#'</th>'S你到底想要什么输出?? -
那里没有人报告这个问题?
... ?>[newline][newline]<!doctype html> -
感谢@RiggsFolly 对使用分号的建议,尤其是使用错误报告!在第一个回声语句后不小心输入了一个 S :-)
标签: php loops html-table syntax