我遇到了一些问题
注意:未定义的变量:第28行的C:\ xampp \ htdocs \ pagge \ pagenition.php中的分页
注意:未定义的变量:第29行的C:\ xampp \ htdocs \ pagge \ pagenition.php中的prev
警告:mysql_fetch_array()要求参数1为资源,第41行的C:\ xampp \ htdocs \ pagge \ pagenition.php中给出布尔值
这是我的代码
<?php
require("conn.php");
$count_query = mysql_query("SELECT null FROM product");
$count = mysql_num_rows($count_query);
if(isset($_GET['page'])){
$page=preg_replace("#[^0-9]#","",$_GET['page']);
}else{
$page = 1;
}
$perPage = 2;
$lastPage = ceil($count/$perPage);
$limit = "LIMIT". ($page-1)*$perPage .", $perPage";
$query = mysql_query("SELECT P_name FROM product ORDER BY P_id DESC '$limit'");
if($lastPage!=1){
if($page != $lastPage){
$next = $page + 1;
$pageination .= '<a href= "pagenition.php?page='.$next.">NEXT </a>" ;
}
}
if($lastPage!=1){
if($page != $lastPage){
$prev = $page - 1;
$pageination .= '<a href= "pagenition.php?page='.$prev.">Prev </a>" ;
}
}
while($row=mysql_fetch_array($query)){
$output .= $row['P_name'] . "<hr/>";
}
?>
<html>
<body>
<h1> pagenition example </h1>
<?php echo $output ?>
<?php echo $pageination ?>
</body>
</html>
答案 0 :(得分:0)
简单的分页代码......
<?php
$sql = "select * from tb_name order by id desc";
$result = mysql_query($sql);
$no = mysql_num_rows($result);
if (isset($_GET['page'])) {
$page = preg_replace('#[^0-9]#i', '', $_GET['page']);
} else {
$page = 1;
}
$itemsPerPage = 30;
$lastPage = ceil($no / $itemsPerPage);
if ($page < 1) {
$page = 1;
} else if ($page > $lastPage) {
$page = $lastPage;
}
$centerPages = "";
$sub1 = $page - 1;
$sub2 = $page - 2;
$add1 = $page + 1;
$add2 = $page + 2;
if ($page == 1) {
$centerPages .= ' <span class="pagNumActive">' . $page . '</span> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $add1 . '">' . $add1 . '</a> ';
} else if ($page == $lastPage) {
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $sub1 . '">' . $sub1 . '</a> ';
$centerPages .= ' <span class="pagNumActive">' . $page . '</span> ';
} else if ($page > 2 && $page < ($lastPage - 1)) {
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $sub2 . '">' . $sub2 . '</a> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $sub1 . '">' . $sub1 . '</a> ';
$centerPages .= ' <span class="pagNumActive">' . $page . '</span> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $add1 . '">' . $add1 . '</a> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $add2 . '">' . $add2 . '</a> ';
} else if ($page > 1 && $page < $lastPage) {
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $sub1 . '">' . $sub1 . '</a> ';
$centerPages .= ' <span class="pagNumActive">' . $page . '</span> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $add1 . '">' . $add1 . '</a> ';
}
$limit = 'limit ' .($page - 1) * $itemsPerPage .',' .$itemsPerPage;
$sql2 = mysql_query("select * from tb_name order by id desc $limit");
$paginationDisplay = "";
if ($lastPage != "1"){
$paginationDisplay .= 'Page <strong>' . $page . '</strong> of ' . $lastPage. ' ';
if ($page != 1) {
$previous = $page - 1;
$paginationDisplay .= ' <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $previous . '" style="text-decoration:none;"> Previous </a> ';
}
$paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';
if ($page != $lastPage) {
$nextPage = $page + 1;
$paginationDisplay .= ' <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $nextPage . '" style="text-decoration:none;"> Next</a> ';
}
}
?>
写<?php echo $paginationDisplay; ?>
以显示分页编号