Php 为什么在下面的代码中退出While循环?
我不明白他们为什么在下面的代码中退出while循环。我试着查看谷歌,但没有给出答案 PHP标记在之前已关闭Php 为什么在下面的代码中退出While循环?,php,Php,我不明白他们为什么在下面的代码中退出while循环。我试着查看谷歌,但没有给出答案 PHP标记在之前已关闭 <h1 class="page-header"> 这是 <?php } ?> 在代码末尾添加以关闭循环。谁能给我解释一下,让我更好地理解代码。谢谢 <?php $query = "SELECT * FROM post"; $select_all_post_query = my
<h1 class="page-header">
这是
<?php } ?>
在代码末尾添加以关闭循环。谁能给我解释一下,让我更好地理解代码。谢谢
<?php
$query = "SELECT * FROM post";
$select_all_post_query = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc ($select_all_post_query)){
$post_title = $row['post_title'];
$post_author = $row['post_author'];
$post_date = $row['post_date'];
$post_image = $row['post_image'];
$post_content = $row['post_content'];
?>
<h1 class="page-header">
Page Heading
<small>Secondary Text</small>
</h1>
<!-- First Blog Post -->
<h2>
<a href="#"><?php echo $post_title ?></a>
</h2>
<p class="lead">
by <a href="index.php"><?php echo $post_author ?></a>
</p>
<p><span class="glyphicon glyphicon-time"></span> <?php echo $post_date ?></p>
<hr>
<img class="img-responsive" src="http://placehold.it/900x300" alt="">
<hr>
<p><?php echo $post_content ?></p>
<a class="btn btn-primary" href="#">Read More <span class="glyphicon glyphicon-chevron-right"></span></a>
<hr>
<?php }
?>
页眉
次要文本
通过
两个PHP标记之间的HTML是循环的一部分。循环迭代时,HTML应该多次发送到页面(假设查询返回多个结果)。PHP的一个关键点是,您可以将其与HTML混合以动态生成服务器端HTML
第一个PHP块只是一些仪式性的代码,它在while循环中引入了一些变量,稍后将在HTML中回响,如图所示:
<!-- First Blog Post -->
<h2>
<a href="#"><?php echo $post_title ?></a>
</h2>
闭幕词:
<?php }
?>
基本上是这样的:
<h1 class="page-header">
Page Heading
<small>Secondary Text</small>
</h1>
<!-- First Blog Post -->
<h2>
<a href="#"><?php echo $post_title ?></a>
</h2>
<p class="lead">
by <a href="index.php"><?php echo $post_author ?></a>
</p>
<p><span class="glyphicon glyphicon-time"></span> <?php echo $post_date ?></p>
<hr>
<img class="img-responsive" src="http://placehold.it/900x300" alt="">
<hr>
<p><?php echo $post_content ?></p>
<a class="btn btn-primary" href="#">Read More <span class="glyphicon glyphicon-chevron-right"></span></a>
<hr>
页眉
次要文本
通过
将在while循环的每次迭代中以HTML的形式进行一次回显。这导致在一个HTML页面上有多个博客帖子。我完全无法理解您想问什么。首先,您需要告诉我们,如果没有while循环,while循环应该在哪里结束?他们最终结束了它,因为他们希望以这种方式打印来自数据库的数据。它在所有必需的数据上迭代并在处打印。您可以在@SaadSuri这里找到更多关于它的信息。我知道他们应该在代码结束的地方结束它,但是为什么PHP在代码之间退出,在循环结束时括号}有另一个PHP开始和结束标记。如果有,mysqli_fetch_assoc的返回值为空结果集中没有更多行。这将导致while表达式的计算结果为false,即while循环中出现退出。
<h1 class="page-header">
Page Heading
<small>Secondary Text</small>
</h1>
<!-- First Blog Post -->
<h2>
<a href="#"><?php echo $post_title ?></a>
</h2>
<p class="lead">
by <a href="index.php"><?php echo $post_author ?></a>
</p>
<p><span class="glyphicon glyphicon-time"></span> <?php echo $post_date ?></p>
<hr>
<img class="img-responsive" src="http://placehold.it/900x300" alt="">
<hr>
<p><?php echo $post_content ?></p>
<a class="btn btn-primary" href="#">Read More <span class="glyphicon glyphicon-chevron-right"></span></a>
<hr>