-1
if( isset($_GET['page'] ) )
{ 
   $page = $_GET['page']-1;
   $offset = $recLimit * $page;$page = $_GET['page']+1;
}
else
{
  $page=2;
  $offset = 0;
}

$phpself = $_SERVER['PHP_SELF'];
if( $totalPages <= $noofpages )
{
   echo "Pages if less than or equal 5 : ";
   for($i=1; $i <= $totalPages; $i++)
   {
      echo  "<a href = \"$phpself?page=$i\">$i</a> |";
   }
}
else
 {
    $i=1;
    for($i; $i <= $totalPages; $i++)
    {
       echo  "<a href = \"$phpself?page=$i\">$i</a> |";
     }
  }
  ?><table border="1" cellpadding="8"><tr> <th>ID</th><th>Name</th><th>Age</th><th>E-mail ID</th><th>Verified</th>

我要做一个 PHP 分页,但我有一些问题,我想显示 5 页和每页 4 条记录。如果我们点击下一个按钮,那么它会显示剩余的页面。总共有7页。

4

1 回答 1

0

由于我无法真正理解您的代码,因此可以帮助您入门:

$items_per_page = 4;

/* Get the total number of records */
$result = mysqli_query("select count(*) as n from ...");
$row = mysqli_fetch_assoc($result);
$total_count = $row["n"];

/* Calculate the total number of pages */
$total_pages = ceil($total_count / $items_per_page);

/* Determine the current page */
$current_page = intval(@$_GET["page"]);
if(!$current_page || $current_page < 1) { $current_page = 1; }
if($current_page > $total_pages)        { $current_page = $total_pages; }

/* Get the records for the current page */
$limit = $items_per_page;
$offset = ($current_page - 1) * $items_per_page;
$result = mysqli_query("select ... limit " . $offset . ", " . $limit);

要显示分页链接,请使用以下命令:

for($i = 1; $i <= $total_pages; $i++)
{
    if($i > 1) { print(" | "); }
    if($current_page == $i) { print($i); }
    else { print('<a href="?page=' . $i . '">' . $i . '</a>'; }
}
于 2013-11-04T13:05:28.883 回答