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

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


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


慕姐8265434
浏览 113回答 1
1回答

Cats萌萌

在您的 while 循环中,您将覆盖每次迭代的值。您也只在 HTML 中输出一篇文章。将<article>..</article>代码放在循环while中,以便为每次迭代打印。while将循环更改为:while ($row=mysqli_fetch_array($get)) {&nbsp; &nbsp; $name = $row['product_name'];&nbsp; &nbsp; $price = $row['product_price'];&nbsp; &nbsp; $img = $row['img'];?><article>&nbsp; <a href="showproduct.php"><img src="adminpanel/<?php echo $img?>" alt="" style="height:13rem;width:13rem;"></a>&nbsp; <h3><a href="showproduct.php"><?php echo $name;?></a></h3>&nbsp; <h4><a href="showproduct.php">$<?php echo $price ?></a></h4>&nbsp; <a href="cart.php" class="btn-add">Add to cart</a></article><?php}并删除<article>...</article>您现在拥有的单个块。
打开App,查看更多内容
随时随地看视频慕课网APP