php分页功能是在做php网站开发的时候经常需要用到的,一个好的分页效果往往集优异的性能和用户体验于一体,下面是我用到的一种分页代码,供大家参考使用。我个人觉得还是非常不错的,分页的样式也非常稳重地体。
第一个页面是显示列表页面list.php:
<table>
<?php
require_once("../conn.php");
$page = isset($_GET['p']) ? intval($_GET['p']) : 1;
//每页显示记录数
$pageSize = 10;
$sql = "select count(id) as totalNum from hc_product";
$result = mysql_query($sql);
$rs = mysql_fetch_array($result);
//总记录数
$totalRecords = $rs['totalNum'];
//计算总页数
$totalPage = 0;
if( ($totalRecords % $pageSize) == 0 ) {
$totalPage = $totalRecords / $pageSize;
}else {
$totalPage = floor($totalRecords / $pageSize) + 1;
}
if( $page <= 0 ) {
$page = 1;
}elseif( $page > $totalPage ) {
$page = $totalPage;
}
$sql = "select * from yourtable " ." limit ".(($page - 1) * $pageSize).",".$pageSize;
$result = mysql_query($sql);
//页码
include_once("pagecode.php");
$pageCode = new pagecode($totalPage, $page, 0, "proList.php?p=");
$pageCodeHtml = $pageCode->get_page_html_code()
?>
<tr><td><?php echo $rs['productName']; ?></td></tr>
<?php }?>
<tr>
<tr>
<td colspan="5" height="30" id="pager"><?php echo $pageCodeHtml;?></td>
</tr>
</tr>
</table>
第二个页面是被调用的一个分页功能页面pagecode.php:
<?php
class pagecode {
var $total; // the number of all pages.
var $p; //the current page.
var $is_ajax; //its value is 0 or 1.
var $ajax_func; //the javascript function used to pass page parameter.
var $parameter;
var $page_html_code; //used to present the page view.
function __construct($total, $p=1, $is_ajax=0, $parameter="", $ajax_func="") {
$this->total = $total;
$this->p = $p;
$this->is_ajax = $is_ajax;
$this->ajax_func = $ajax_func;
$this->parameter = $parameter;
}
function setpaged($p,$i){
if ($p == $i){
return "class='paged'";
}
}
function get_page_html_code() {
$pre = $this->p-1;
$next = $this->p+1;
//hidden previous
if ($this->p > 1)
{
if($this->is_ajax == 0)
$this->page_html_code .= "<div><a href='".$this->parameter.$pre."'> 上一页</a></div>";
if($this->is_ajax == 1)
$this->page_html_code .= "<div><a href='javascript:void(0);' onclick='".$this->ajax_func."(".$this->parameter.$pre.");'> 上一页</a></div>";
}
//pagination
if ($this->p > 5)
{
//pre 1
if (($this->p-1)%5 == 0)
{
if ($this->p+4 < $this->total)
{
for ($i=$this->p-5;$i<=$this->p+4;$i++)
{
if($this->is_ajax == 0)
$this->page_html_code .= "<div ".$this->setpaged($this->p,$i)."><a href='".$this->parameter.$i."'>$i</a></div>";
if($this->is_ajax == 1)
$this->page_html_code .= "<div ".$this->setpaged($this->p,$i)."><a href='javascript:void(0);' onclick='".$this->ajax_func."(".$this->parameter.$i.");'>$i</a></div>";
}
}
else
{
for ($i=$this->p-5;$i<=$this->total;$i++)
{
if($this->is_ajax == 0)
$this->page_html_code .= "<div ".$this->setpaged($this->p,$i)."><a href='".$this->parameter.$i."'>$i</a></div>";
if($this->is_ajax == 1)
$this->page_html_code .= "<div ".$this->setpaged($this->p,$i)."><a href='javascript:void(0);' onclick='".$this->ajax_func."(".$this->parameter.$i.");'>$i</a></div>";
}
}
}
//pre 2
if (($this->p-2)%5 == 0)
{
if ($this->p+3 < $this->total)
{
for ($i=$this->p-6;$i<=$this->p+3;$i++)
{
if($this->is_ajax == 0)
$this->page_html_code .= "<div ".$this->setpaged($this->p,$i)."><a href='".$this->parameter.$i."'>$i</a></div>";
if($this->is_ajax == 1)
$this->page_html_code .= "<div ".$this->setpaged($this->p,$i)."><a href='javascript:void(0);' onclick='".$this->ajax_func."(".$this->parameter.$i.");'>$i</a></div>"; }
}
else
{
for ($i=$this->p-6;$i<=$this->total;$i++)
{
if($this->is_ajax == 0)
$this->page_html_code .= "<div ".$this->setpaged($this->p,$i)."><a href='".$this->parameter.$i."'>$i</a></div>";
if($this->is_ajax == 1)
$this->page_html_code .= "<div ".$this->setpaged($this->p,$i)."><a href='javascript:void(0);' onclick='".$this->ajax_func."(".$this->parameter.$i.");'>$i</a></div>";
}
}
}
//next 1
if (($this->p-3)%5 == 0)
{
if ($this->p+7 < $this->total)
{
for ($i=$this->p-2;$i<=$this->p+7;$i++)
{
if($this->is_ajax == 0)
$this->page_html_code .= "<div ".$this->setpaged($this->p,$i)."><a href='".$this->parameter.$i."'>$i</a></div>";
if($this->is_ajax == 1)
$this->page_html_code .= "<div ".$this->setpaged($this->p,$i)."><a href='javascript:void(0);' onclick='".$this->ajax_func."(".$this->parameter.$i.");'>$i</a></div>";
}
}
else
{
for ($i=$this->p-2;$i<=$this->total;$i++)
{
if($this->is_ajax == 0)
$this->page_html_code .= "<div ".$this->setpaged($this->p,$i)."><a href='".$this->parameter.$i."'>$i</a></div>";
if($this->is_ajax == 1)
$this->page_html_code .= "<div ".$this->setpaged($this->p,$i)."><a href='javascript:void(0);' onclick='".$this->ajax_func."(".$this->parameter.$i.");'>$i</a></div>";
}
}
}
//next 2
if (($this->p-4)%5 == 0)
{
if ($this->p+6 < $this->total)
{
for ($i=$this->p-3;$i<=$this->p+6;$i++)
{
if($this->is_ajax == 0)
$this->page_html_code .= "<div ".$this->setpaged($this->p,$i)."><a href='".$this->parameter.$i."'>$i</a></div>";
if($this->is_ajax == 1)
$this->page_html_code .= "<div ".$this->setpaged($this->p,$i)."><a href='javascript:void(0);' onclick='".$this->ajax_func."(".$this->parameter.$i.");'>$i</a></div>";
}
}
else
{
for ($i=$this->p-3;$i<=$this->total;$i++)
{
if($this->is_ajax == 0)
$this->page_html_code .= "<div ".$this->setpaged($this->p,$i)."><a href='".$this->parameter.$i."'>$i</a></div>";
if($this->is_ajax == 1)
$this->page_html_code .= "<div ".$this->setpaged($this->p,$i)."><a href='javascript:void(0);' onclick='".$this->ajax_func."(".$this->parameter.$i.");'>$i</a></div>";
}
}
}
//next 3
if (($this->p-5)%5 == 0)
{
if ($this->p+5 < $this->total)
{
for ($i=$this->p-4;$i<=$this->p+5;$i++)
{
if($this->is_ajax == 0)
$this->page_html_code .= "<div ".$this->setpaged($this->p,$i)."><a href='".$this->parameter.$i."'>$i</a></div>";
if($this->is_ajax == 1)
$this->page_html_code .= "<div ".$this->setpaged($this->p,$i)."><a href='javascript:void(0);' onclick='".$this->ajax_func."(".$this->parameter.$i.");'>$i</a></div>";
}
}
else
{
for ($i=$this->p-4;$i<=$this->total;$i++)
{
if($this->is_ajax == 0)
$this->page_html_code .= "<div ".$this->setpaged($this->p,$i)."><a href='".$this->parameter.$i."'>$i</a></div>";
if($this->is_ajax == 1)
$this->page_html_code .= "<div ".$this->setpaged($this->p,$i)."><a href='javascript:void(0);' onclick='".$this->ajax_func."(".$this->parameter.$i.");'>$i</a></div>";
}
}
}
}
else
{
if ($this->total > 10)
{
for ($i=1;$i<=10;$i++)
{
if($this->is_ajax == 0)
$this->page_html_code .= "<div ".$this->setpaged($this->p,$i)."><a href='".$this->parameter.$i."'>$i</a></div>";
if($this->is_ajax == 1)
$this->page_html_code .= "<div ".$this->setpaged($this->p,$i)."><a href='javascript:void(0);' onclick='".$this->ajax_func."(".$this->parameter.$i.");'>$i</a></div>";
}
}
else
{
for ($i=1;$i<=$this->total;$i++)
{
if($this->is_ajax == 0)
$this->page_html_code .= "<div ".$this->setpaged($this->p,$i)."><a href='".$this->parameter.$i."'>$i</a></div>";
if($this->is_ajax == 1)
$this->page_html_code .= "<div ".$this->setpaged($this->p,$i)."><a href='javascript:void(0);' onclick='".$this->ajax_func."(".$this->parameter.$i.");'>$i</a></div>";
}
}
}
$this->page_html_code .= "<div class='point'>...</div>";
if($this->is_ajax == 0)
$this->page_html_code .= "<div><a href='".$this->parameter.$this->total."'>".$this->total."</a></div>";
if($this->is_ajax == 1)
$this->page_html_code .= "<div><a href='javascript:void(0);' onclick='".$this->ajax_func."(".$this->parameter.$this->total.");'>".$this->total."</a></div>";
//hidden next
if ($this->p < $this->total)
{
if($this->is_ajax == 0)
$this->page_html_code .= "<div><a href='".$this->parameter.$next."'>下一页</a></div>";
if($this->is_ajax == 1)
$this->page_html_code .= "<div><a href='javascript:void(0);' onclick='".$this->ajax_func."(".$this->parameter.$next.");'>下一页</a></div>";
}
return $this->page_html_code;
}
}
?>
上面两段php代码分别保存后,根据你数据库的实际情况修改部分sql语句,最后是可以正常运行的,我已经做过测试了。如果有不清楚的可以联系我,我把实例文件直接发给有需要的网友,欢迎大家和我交流探讨更多php技术问题。