php分页:每页一次一条记录

时间:2015-03-11 13:13:47

标签: php pagination

下面的代码是显示表格中的内容,一次显示一个内容,以及用于根据需要导航的导航按钮。但目前它正在同时显示所有内容。请帮我修理一下。提前谢谢。

<?php //paging starts here;

// how many rows to show per page
$rowsPerPage = 1;

// by default we show first page
$pageNum = 1;

// if $_GET['page'] defined, use it as page number
if ( isset($_GET['page']) ) {
    $pageNum = $_GET['page'];
}

// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;


$query = "SELECT ap_id, ap_text FROM approach";
$result = mysqli_query($connect, $query) or die('Error : ' . myqli_connect_error());
?>

<table width="99%" border="0" align="center" cellpadding="3"  cellspacing="1" bgcolor="#999999">

<?php
while ( list($id, $text) = mysqli_fetch_array($result, MYSQLI_NUM) ) {
    ?>
    <tr bgcolor="#FFFFFF"> 
        <td width="619" class="mainpageHeadlines"> 
        <?php echo $text;?>  </td>
        <td width="142" align="center" class="mainpageHeadlines">&nbsp;</td>
    </tr>
    <?php
}
echo '<br>';

// how many rows we have in database
$query   = "SELECT COUNT(ap_title) AS numrows FROM approach";
$result  = mysqli_query($connect, $query) or die('Error, query failed here:   ' . mysqli_connect_error());
$row     = mysqli_fetch_array($result, MYSQLI_ASSOC);
$numrows = $row['numrows'];

// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);

$self = $_SERVER['PHP_SELF'];

// creating 'previous' and 'next' link
// plus 'first page' and 'last page' link

// print 'previous' link only if we're not
// on page one
if ( $pageNum > 1 ) {
    $page = $pageNum - 1;
    $prev = " <a href=\"$self?page=$page\">[Prev]</a> ";

    $first = " <a href=\"$self?page=1\">[First Page]</a> ";
} else {
    $prev  = ' [Prev] ';       // we're on page one, don't enable 'previous'    link
    $first = ' [First Page] '; // nor 'first page' link
}

// print 'next' link only if we're not
// on the last page
if ( $pageNum < $maxPage ) {
    $page = $pageNum + 1;
    $next = " <a href=\"$self?page=$page\">[Next]</a> ";

    $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
} else {
    $next = ' [Next] ';      // we're on the last page, don't enable 'next' link
    $last = ' [Last Page] '; // nor 'last page' link
}

// print the page navigation link
//echo $first . $prev . " Showing page <strong>$pageNum</strong> of       <strong>$maxPage</strong> pages " . $next . $last;


include 'db/closedb.php';
?>
</table>

<table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
        <td align="center" class="smaller">   
            <?php 

            // print the page navigation link
            echo '<br>';
            echo $first . $prev . " Page <strong>$pageNum</strong> of    <strong>$maxPage</strong>" . $next . $last;  

            ?>

2 个答案:

答案 0 :(得分:1)

您需要按所在页面限制查询。例如:

$query = "SELECT ap_id, ap_text FROM approach LIMIT $offset, $rowsPerPage";

答案 1 :(得分:0)

使用您的帖子变量尝试SQL OFFSET

$query = "SELECT ap_id, ap_text FROM approach OFFSET " . $offset;

另外,请确保清理查询以防止SQL注入:

$pageNum = $_GET['page'];
应该是:
$pageNum = mysql_escape_string($_GET['page']);