我有一个名为post_counter的变量,我希望在完成循环后获取它的值,并且我希望它作为要显示的第一个列表项。但问题是php只将变量读为0,因为编译器从上到下读取代码。我怎样才能更新我的柜台?这是我的PHP代码:
$post_counter = 0;
<ul>
<?php
$query_post = "SELECT post_date,post_message FROM tbl_posts";
$post_run = mysqli_query($con,$query_post);
echo "<li style = 'text-align:center; cursor:default'><h3>"."New posts".$post_counter."</h3></li>";
while($row = mysqli_fetch_assoc($post_run))
{
echo "<li><a class = 'mark_read'>"."mark as read"."</a></li>";
$post_counter++;
}
?>
</ul>
这个输出将是:
New posts 0
但它应该是
New posts 5 //since there are 5 rows in my table that will be selected
答案 0 :(得分:3)
使用mysqli_num_rows()
$post_run = mysqli_query($con,$query_post);
$num_posts = mysqli_num_rows($postrun);
echo "<li style = 'text-align:center; cursor:default'><h3>"."New posts".$num_posts ."</h3></li>";