我是分页新手,我尝试过这样做。它部分有效。我的分页,在每页上显示正确的结果数量以及正确的页数。但问题是它在每个页面上显示相同的结果。
以下是代码:
$rows = mysqli_num_rows($result);
echo $rows;
//Total row count.
//Number of results we want displayed per page.
$page_rows = 3;
//Last page, page number.
$last = ceil($rows/$page_rows);
//Making sure that last page cannot be less than one.
if($last < 1){
$last = 1;
}
//Establish pagenum variable;
$pagenum = 1;
//Get paenum variable from the URL if it exists.
if(isset($_GET['pn'])){
$pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}
//Making sure the pagenum isn't below one.
if($pagenum < 1){
$pagenum = 1;
}else if($pagenum > $last){
$pagenum = $last;
}
//This sets the limit.
$limit = 'LIMIT ' .($pagenum-1) * $page_rows .',' .$page_rows;
//Establish the pagination controls.
$paginationCtrls = '';
//If there is more than one page worth of results.
if($last != 1){
if($pagenum > 1){
$previous = $pagenum - 1;
$paginationCtrls .= '<a href = "'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">Previous</a> ';
//Clickable links that should appear on the left.
for($i = $pagenum - 4; $i < $pagenum; $i++){
if($i > 0){
$paginationCtrls .='<a href = "'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> ';
}
}
}
$paginationCtrls .= ''.$pagenum.' ';
for($i = $pagenum+1; $i<= $last; $i++){
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> ';
if($i > $pagenum+4){
break;
}
}
if($pagenum != $last){
$next = $pagenum+1;
$paginationCtrls .= ' <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a>';
}
}
$sql = "SELECT * FROM blogdata2 ORDER BY id DESC ".$limit;
我在查询期间也试过这个:
$sql = "SELECT * FROM blogdata2 ORDER BY id DESC $limit";
但似乎都没有用。这可能是什么问题?
感谢您的帮助。