创建分页以获得数据库结果

问题描述 投票:0回答:1

我想创建一个分页,所以这是我尝试过的

                <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更改为随机数,但是在我的数据库中又得到了另一个结果,并且有一段时间给我一个错误,我尝试替换代码周围,​​但仍然没有工作任何人都知道我哪里出了错

php
1个回答
0
投票
在while循环中,您将在每次迭代中覆盖值。您还只在HTML中输出

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>块。
© www.soinside.com 2019 - 2024. All rights reserved.