使用PHP和HTML表中的while循环使用内联样式的正确语法是什么



我需要使用php变量和while循环来显示表中的行,但我不知道我做错了什么。我尝试过单引号和双引号的各种不同组合,但我仍然无法使用正确的语法使这些行在表中输出而不产生任何错误。我正在使用Dreamweaver 2019对其进行编码。我使用了Netbeans 8.2,但仍然无法找出该代码的正确语法。

这也是我到目前为止在stackoverflow上发现的,但我仍然没有找到我需要的东西。在这种情况下,我也找不到如何使用谷歌使用正确的语法:

php-for while循环中的循环,语法正确吗?

PHP 中的内联样式

https://stackoverflow.com/search?q=use+inline+styleing+with+php+html+表格

html表&内联样式

HTML表与内联PHP

<?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>

因为您使用了$stmt->bind_result($id, $first_name, $last_name);,所以您选择的列将由$stmt->fetch()返回到称为$id$first_name$last_name的变量中

还要记住,每个echo都是一个不同的语句,并且应该以;结束

还要注意,当使用双引号字符串文字时,变量将自动展开。此外,在双引号文本中,您可以使用单引号,而不会导致文本提前终止的问题。这也适用于在单引号文字中使用双引号。

while($stmt->fetch()) {                   
echo "<th class='text-center'>'#'</th>";
echo "<th>$id</th>";
echo "<th>$first_name</th>";
echo "<th>$last_name</th>";
echo "<th class='text-center'>Details</th>";
}

更好的方法离类似模板的解决方案更近了一步。

<tbody>   
<?php while($result = $stmt->fetch()): ?>        
<tr>
<td clas="text-center">#</td>
<td><?php echo $id; ?> </td>
<td><?php echo $first_name; ?> </td>
<td><?php echo $last_name; ?> </td>
<td class="text-center">Details</td>
</tr>
<?php endwhile; ?>

首先,您需要了解如何使用循环。循环中的代码不正确。有许多报价问题。此外,根据您的代码,我相信您正在获取表,而不是使用循环中的列,类似于$result['id']$result->id

您可能还需要了解PHP连接是如何工作的。PHP允许字符串使用单引号'和双引号"。如果使用相同的代码包装变量,则必须使用反斜杠对其进行转义。

请尝试以下代码,这可能会解决您的问题。

<?php
while($result = $stmt->fetch()) {    
echo '<th class="text-center">#</th>';
echo '<th>' . $id . '</th>';
echo '<th>' . $first_name . '</th>';
echo '<th>' . $last_name . '</th>';     
echo '<th class="text-center">Details</th>';
}
?>

此外,您应该检查此项以了解PHP字符串和连接

尝试使用以下代码

这样写'$id, $last_name, $firs_name

<?php   
while($result = $stmt->fetch()) {                   
echo '<th class="text-center">#</th>';
echo  '<th>'.$id.'</th>';
echo '<th>'.$lastt_name.'</th>';
echo '<th>'.$first_name.'</th>';
echo '<th class="text-center">Details/th>';
}
?>

尝试使用<?=$string; ?>,如下所示:

<?php while($result = $stmt->fetch()): ?>        
<tr>
<td clas="text-center">#</td>
<td><?=$id; ?> </td>
<td><?=$first_name; ?> </td>
<td><?=$last_name; ?> </td>
<td class="text-center">Details</td>
</tr>
<?php endwhile; ?>

在html中使用较少的php可以保持干净。

最新更新