PHP分页5页

时间:2013-11-04 11:40:10

标签: php mysqli

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页。

1 个答案:

答案 0 :(得分: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>'; }
}