我想创建一个分页,所以这是我尝试过的
<section class="products">
<?php
$result_per_page=10;
$get = mysqli_query($conn," SELECT * FROM products");
$number_of_results=mysqli_num_rows($get);
if (!isset($_GET['page'])) {
$page=1;
} else{
$page=$_GET['page'];
}
$this_page_first_result=($page-1)*$result_per_page;
$get = mysqli_query($conn," SELECT * FROM products LIMIT ".$this_page_first_result.','.$result_per_page);
$number_of_results=mysqli_num_rows($get);
while ($row=mysqli_fetch_array($get)) {
$name = $row['product_name'];
$price = $row['product_price'];
$img = $row['img'];
}
$number_of_pages=ceil($number_of_results/$result_per_page);
?>
<article>
<a href="showproduct.php">
<img src="adminpanel/<?php echo $img?>" alt="" style="height:13rem;width:13rem;"></a>
<h3><a href="showproduct.php"><?php echo $name;?></a></h3>
<h4><a href="showproduct.php">$<?php echo $price ?></a></h4>
<a href="cart.php" class="btn-add">Add to cart</a>
</article>
</section>
</div>
<!-- / content -->
</div>
<?php for ($page=1; $page <=$number_of_pages ; $page++) {
echo '<a href="products.php?page='.$page.'">'.$page.'</a>';
} ?>
</div>
<!-- / container -->
</div>
<!-- / body -->
</ul>
所以这是我的问题,无论我做什么,我都只能从数据库中得到1个结果,有时我将$result_per_page
更改为随机数,但是在我的数据库中又得到了另一个结果,并且有一段时间给我一个错误,我尝试替换代码周围,但仍然没有工作任何人都知道我哪里出了错
one文章。将<article>..</article>
代码inside放置在while
循环中,以便在每次迭代时都打印它。
while
循环更改为:while ($row=mysqli_fetch_array($get)) {
$name = $row['product_name'];
$price = $row['product_price'];
$img = $row['img'];
?>
<article>
<a href="showproduct.php"><img src="adminpanel/<?php echo $img?>" alt="" style="height:13rem;width:13rem;"></a>
<h3><a href="showproduct.php"><?php echo $name;?></a></h3>
<h4><a href="showproduct.php">$<?php echo $price ?></a></h4>
<a href="cart.php" class="btn-add">Add to cart</a>
</article>
<?php
}
并删除您现在拥有的单个<article>...</article>
块。