pagenition中下一个上一页的问题

时间:2014-11-24 11:54:24

标签: php

我遇到了一些问题

注意:未定义的变量:第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>

1 个答案:

答案 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 .= '&nbsp; <span class="pagNumActive">' . $page . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $add1 . '">' . $add1 . '</a> &nbsp;';
} else if ($page == $lastPage) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $page . '</span> &nbsp;';
} else if ($page > 2 && $page < ($lastPage - 1)) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $sub2 . '">' . $sub2 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $page . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $add1 . '">' . $add1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $add2 . '">' . $add2 . '</a> &nbsp;';
} else if ($page > 1 && $page < $lastPage) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $page . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $add1 . '">' . $add1 . '</a> &nbsp;';
}

$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. '&nbsp;  &nbsp;  &nbsp; ';

    if ($page != 1) {
        $previous = $page - 1;
        $paginationDisplay .=  '&nbsp;  <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 .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $nextPage . '" style="text-decoration:none;"> Next</a> ';
    } 
}
?>

<?php echo $paginationDisplay; ?>以显示分页编号