gpt4 book ai didi

PHP mysql jquery 分页修改

转载 作者:行者123 更新时间:2023-11-29 23:16:23 25 4
gpt4 key购买 nike

我的分页出现问题,需要您的帮助。我有 53 条记录,但使用此分页,我只能获取 50 条记录,看不到其他 3 条记录

我想更改此代码以获取所有记录,并且如果我想让分页易于导航大记录。像这样(对于exp):

<< 1 2 3 4 5 ...... 184 185 >>

谢谢

   <?php

include("db.php"); //include config file

//sanitize post value
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);

//validate page number is really numaric
if(!is_numeric($page_number)){die('Invalid page number!');}

//get current starting point of records
$position = ($page_number * $item_per_page);

//Limit our results within a specified range.
$results = mysqli_query($db,"SELECT id,name,message FROM paginate ORDER BY id ASC LIMIT $position, $item_per_page");

//output results from database

while($row = mysqli_fetch_array($results))
{


echo' <ul class="page_result">
<li class="page_result_img"><img src="images/pic1.jpg" class="img-responsive" alt=""/></li>
<li class="page_result_desc" id="item_'.$row["id"].'">
<h3><a href="#">'.$row["id"].'.'.$row["name"].'</a></h3>
<p>'.$row["message"].'</p>
</li>
<p class="no">'.$row["id"].'<br><span>projet</span></p>
<div class="clearfix"> </div>
</ul>';
}

?>

projet.php

        <?php
include("db.php");

$results = mysqli_query($db,"SELECT COUNT(*) FROM paginate");
$get_total_rows = mysqli_fetch_array($results); //total records

//break total records into pages
$pages = ceil($get_total_rows[0]/$item_per_page);

//create pagination
if($pages > 1)
{
$pagination = '';
$pagination .= '<ul class="paginate">';
for($i = 1; $i<$pages; $i++)
{
$pagination .= '<li><a href="#" class="paginate_click" id="'.$i.'-page">'.$i.'</a></li>';
}
$pagination .= '</ul>';
}

?>



.
.
.
.
<div class="approach" id="app">
<div class="container">
<div class="gallery-head text-center">
<h3>Nos projets</h3>
<p>Trouvez ici tout les projets</p>
<span> </span>
</div>

<ul id="results"></div>
<?php echo $pagination; ?>
</div>

</div>

最佳答案

为了在 SQL 查询中正确使用 LIMIT,第一页的 $page_number 值必须等于 0。但是您的分页从 1 开始。那么您永远不会得到 $position = 0 并且 SQL 查询会跳过第一个 $item_per_page 记录。

尝试修改$position计算:

$position = (($page_number - 1) * $item_per_page);

第二个问题出现在 projet.php 中。对于 53 条记录(且 $item_per_page = 10),$pages 的值 = 6。但是对于 5 条记录,它应该是:

for($i = 1; $i <= $pages; $i++)

关于PHP mysql jquery 分页修改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27758067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com