Home > Backend Development > PHP Tutorial > Sample code of paging class implemented by php+ajax

Sample code of paging class implemented by php+ajax

怪我咯
Release: 2023-03-13 21:20:02
Original
1141 people have browsed it

Paging display is a very common method of browsing and displaying large amounts of data, and is one of the most commonly processed events in web programming. For veterans of web programming, writing this kind of code is as natural as breathing, but for beginners, they are often confused about this issue, so I specially wrote this article to explain this issue in detail.

1. Paging principle:
The so-called paging display means that the result set in the database is artificially divided into sections for display. Two initial parameters are required here:

Each How many records are in the page ($PageSize)?
What page is the current page ($CurrentPageID)?

Now as long as you give me another result set, I can display a specific result.

As for other parameters, such as: previous page ($PReviousPageID), next page ($NextPageID), total number of pages ($numPages), etc., they can all be obtained based on the previous things.

Taking MySQL database as an example, if you want to intercept a certain piece of content from the table, the sql statement can be used: select * from table limit offset, rows. Take a look at the following set of SQL statements and try to find the rules.

The first 10 records: select * from table limit 0,10
The 11th to 20th records: select * from table limit 10,10
The 21st to 30th records: select * from table limit 20,10
......
This set of sql statements is actually the sql statement to fetch data from each page of the table when $PageSize=10. We can summarize such a template:
select * from table limit ($CurrentPageID - 1) * $PageSize, $PageSize
Take this template and substitute the corresponding values ​​​​and compare it with the above set of sql statements to see if that is the case. After solving the most important problem of how to obtain the data, all that is left is to pass the parameters, construct the appropriate SQL statement and then use PHP to obtain the data from the database and display it.

This article mainly introduces the paging class implemented by php+ajax. The final effect is as shown below

Sample code of paging class implemented by php+ajax

The code is as follows

<?php 
//本分页类不处理SQL; 
//大大的加快了分页功能 
//http://blog.csdn.net/fkedwgwy 
//潇湘博客--潇湘 
/** 
演示 
require_once(&#39;../libs/classes/page.class.php&#39;); 
$page=new page(array(&#39;total&#39;=>1000,&#39;perpage&#39;=>20)); 
echo &#39;mode:1<br>&#39;.$page->show(); 
echo &#39;<hr>mode:2<br>&#39;.$page->show(2); 
echo &#39;<hr>mode:3<br>&#39;.$page->show(3); 
echo &#39;<hr>mode:4<br>&#39;.$page->show(4); 
echo &#39;<hr>开始AJAX模式:&#39;; 
$ajaxpage=new page(array(&#39;total&#39;=>1000,&#39;perpage&#39;=>20,&#39;ajax&#39;=>&#39;ajax_page&#39;,&#39;page_name&#39;=>&#39;test&#39;)); 
echo &#39;mode:1<br>&#39;.$ajaxpage->show(); 
*/ 
class Zend_Page 
{ 
/** 
* config ,public 
*/ 
var $page_name="page";//page标签,用来控制url页。比如说xxx.php?PB_page=2中的PB_page 
var $next_page=&#39;>&#39;;//下一页 
var $pre_page=&#39;<&#39;;//上一页 
var $first_page=&#39;First&#39;;//首页 
var $last_page=&#39;Last&#39;;//尾页 
var $pre_bar=&#39;<<&#39;;//上一分页条 
var $next_bar=&#39;>>&#39;;//下一分页条 
var $format_left=&#39;&#39;; 
var $format_right=&#39;&#39;; 
var $is_ajax=false;//是否支持AJAX分页模式 
var $next_ten_page=">>>"; 
var $per_ten_page="<<<"; 

/** 
* private 
* 
*/ 
var $pagebarnum=10;//控制记录条的个数。 
var $totalpage=0;//总页数 
var $ajax_action_name=&#39;&#39;;//AJAX动作名 
var $nowindex=1;//当前页 
var $url="";//url地址头 
var $offset=0; 
var $total=&#39;&#39;; 

/** 
* constructor构造函数 
* 
* @param array $array[&#39;total&#39;],$array[&#39;perpage&#39;],$array[&#39;nowindex&#39;],$array[&#39;url&#39;],$array[&#39;ajax&#39;]... 
*/ 
function Zend_Page($array) 
{ 
if(is_array($array)){ 
if(!array_key_exists(&#39;total&#39;,$array))$this->error(FUNCTION,&#39;need a param of total&#39;); 
$total=intval($array[&#39;total&#39;]); 
$perpage=(array_key_exists(&#39;perpage&#39;,$array))?intval($array[&#39;perpage&#39;]):10; 
$nowindex=(array_key_exists(&#39;nowindex&#39;,$array))?intval($array[&#39;nowindex&#39;]):&#39;&#39;; 
$url=(array_key_exists(&#39;url&#39;,$array))?$array[&#39;url&#39;]:&#39;&#39;; 
}else{ 
$total=$array; 
$perpage=10; 
$nowindex=&#39;&#39;; 
$url=&#39;&#39;; 
} 
if((!is_int($total))||($total<0))$this->error(FUNCTION,$total.&#39; is not a positive integer!&#39;); 
if((!is_int($perpage))||($perpage<=0))$this->error(FUNCTION,$perpage.&#39; is not a positive integer!&#39;); 
if(!empty($array[&#39;page_name&#39;]))$this->set(&#39;page_name&#39;,$array[&#39;page_name&#39;]);//设置pagename 
$this->_set_nowindex($nowindex);//设置当前页 
$this->_set_url($url);//设置链接地址 
$this->totalpage=ceil($total/$perpage); 
$this->total=$total; 
$this->offset=($this->nowindex-1)*$perpage; 
if(!empty($array[&#39;ajax&#39;]))$this->open_ajax($array[&#39;ajax&#39;]);//打开AJAX模式 
} 
/** 
* 设定类中指定变量名的值,如果改变量不属于这个类,将throw一个exception 
* 
* @param string $var 
* @param string $value 
*/ 
function set($var,$value) 
{ 
if(in_array($var,get_object_vars($this))) 
$this->$var=$value; 
else { 
$this->error(FUNCTION,$var." does not belong to PB_Page!"); 
} 

} 
/** 
* 打开倒AJAX模式 
* 
* @param string $action 默认ajax触发的动作。 
*/ 
function open_ajax($action) 
{ 
$this->is_ajax=true; 
$this->ajax_action_name=$action; 
} 
/** 
* 获取显示"下一页"的代码 
* 
* @param string $style 
* @return string 
*/ 
function next_page($style=&#39;&#39;) 
{ 
if($this->nowindex<$this->totalpage){ 
return $this->_get_link($this->_get_url($this->nowindex+1),$this->next_page,$style); 
} 
return &#39;<span class="&#39;.$style.&#39;">&#39;.$this->next_page.&#39;</span>&#39;; 
} 

/** 
* 获取显示“上一页”的代码 
* 
* @param string $style 
* @return string 
*/ 
function pre_page($style=&#39;&#39;) 
{ 
if($this->nowindex>1){ 
return $this->_get_link($this->_get_url($this->nowindex-1),$this->pre_page,$style); 
} 
return &#39;<span class="&#39;.$style.&#39;">&#39;.$this->pre_page.&#39;</span>&#39;; 
} 

/** 
* 获取显示“上十页”的代码 
* 
* @param string $style 
* @return string 
*/ 
function pre_ten_page($style=&#39;&#39;) 
{ 
if(intval($this->nowindex/10)+1>1){ 
return $this->_get_link($this->_get_url(intval($this->nowindex/10)*10-5),$this->pre_ten_page,$style); 
} 
return &#39;<span class="&#39;.$style.&#39;">&#39;.$this->pre_ten_page.&#39;</span>&#39;; 
} 

/** 
* 获取显示"下十页"的代码 
* 
* @param string $style 
* @return string 
*/ 
function next_ten_page($style=&#39;&#39;) 
{ 
if(intval($this->nowindex/10) <intval($this->totalpage/10)){ 
return $this->_get_link($this->_get_url((intval($this->nowindex/10)+1)*10+5),$this->next_ten_page,$style); 
} 
return &#39;<span class="&#39;.$style.&#39;">&#39;.$this->next_ten_page.&#39;</span>&#39;; 
} 

/** 
* 获取显示“首页”的代码 
* 
* @return string 
*/ 
function first_page($style=&#39;&#39;) 
{ 
if($this->nowindex==1){ 
return &#39;<span class="&#39;.$style.&#39;">&#39;.$this->first_page.&#39;</span>&#39;; 
} 
return $this->_get_link($this->_get_url(1),$this->first_page,$style); 
} 

/** 
* 获取显示“尾页”的代码 
* 
* @return string 
*/ 
function last_page($style=&#39;&#39;) 
{ 
if($this->nowindex==$this->totalpage){ 
return &#39;<span class="&#39;.$style.&#39;">&#39;.$this->last_page.&#39;</span>&#39;; 
} 
return $this->_get_link($this->_get_url($this->totalpage),$this->last_page,$style); 
} 

function nowbar($style=&#39;&#39;,$nowindex_style=&#39;&#39;) 
{ 
$plus=ceil($this->pagebarnum/2); 
if($this->pagebarnum-$plus+$this->nowindex>$this->totalpage)$plus=($this->pagebarnum-$this->totalpage+$this->nowindex); 
$begin=$this->nowindex-$plus+1; 
$begin=($begin>=1)?$begin:1; 
$return=&#39;&#39;; 
for($i=$begin;$i<$begin+$this->pagebarnum;$i++) 
{ 
if($i<=$this->totalpage){ 
if($i!=$this->nowindex) 
$return.=$this->_get_text($this->_get_link($this->_get_url($i),$i,$style)); 
else 
$return.=$this->_get_text(&#39;<span class="&#39;.$nowindex_style.&#39;">&#39;.$i.&#39;</span>&#39;); 
}else{ 
break; 
} 
$return.="\n"; 
} 
unset($begin); 
return $return; 
} 
/** 
* 获取显示跳转按钮的代码 
* 
* @return string 
*/ 
function select() 
{ 
$return=&#39;<select name="PB_Page_Select" onchange="location.replace(this.value)" >&#39;; 
for($i=1;$i<=$this->totalpage;$i++) 
{ 
if($i==$this->nowindex){ 
$return.=&#39;<option value=&#39;.$this->_get_url($i).&#39; selected>&#39;.$i.&#39;</option>&#39;; 
}else{ 
$return.=&#39;<option value=&#39;.$this->_get_url($i).&#39;>&#39;.$i.&#39;</option>&#39;; 
} 
} 
unset($i); 
$return.=&#39;</select>&#39;; 
return $return; 
} 

/** 
* 获取mysql 语句中limit需要的值 
* 
* @return string 
*/ 
function offset() 
{ 
return $this->offset; 
} 

/** 
* 控制分页显示风格(你可以增加相应的风格) 
* 
* @param int $mode 
* @return string 
*/ 
function show($mode=1,$url=&#39;&#39;) 
{ 
switch ($mode) 
{ 
case &#39;1&#39;: 
$this->next_page=&#39;下一页&#39;; 
$this->pre_page=&#39;上一页&#39;; 
return $this->pre_page().$this->nowbar().$this->next_page().&#39;第&#39;.$this->select().&#39;页&#39;; 
break; 
case &#39;2&#39;: 
$this->next_page=&#39;下一页&#39;; 
$this->pre_page=&#39;上一页&#39;; 
$this->first_page=&#39;首页&#39;; 
$this->last_page=&#39;尾页&#39;; 
return $this->first_page().$this->pre_page().&#39;[第&#39;.$this->nowindex.&#39;页]&#39;.$this->next_page().$this->last_page().&#39;第&#39;.$this->select().&#39;页&#39;; 
break; 
case &#39;3&#39;: 
$this->next_page=&#39;下一页&#39;; 
$this->pre_page=&#39;上一页&#39;; 
$this->first_page=&#39;首页&#39;; 
$this->last_page=&#39;尾页&#39;; 
return $this->first_page().$this->pre_page().$this->next_page().$this->last_page(); 
break; 
case &#39;4&#39;: 
$this->next_page=&#39;next&#39;; 
$this->pre_page=&#39;last&#39;; 
return $this->pre_page().$this->nowbar().$this->next_page(); 
break; 
case &#39;5&#39;: 
return $this->pre_bar().$this->pre_page().$this->nowbar().$this->next_page().$this->next_bar(); 
break; 
case &#39;6&#39;: 
//启用了“上一页”,“下一页”,“最后一页”。可根据情况启用“第一页”。 
//$this->first_page=&#39;<img src="&#39;.HOSTPATH.&#39;images/propageup.jpg" width="64" height="13" >&#39;; 
$this->pre_page=&#39;<img src="&#39;.HOSTPATH.&#39;images/propageup.jpg" style="width:58px; height:18px;">&#39;; 
$this->next_page=&#39;<img src="&#39;.HOSTPATH.&#39;images/propagedown.jpg" style="width:58px; height:18px;">&#39;; 
$this->last_page=&#39;最后一页&#39;; 
//return "<td width=39 align=&#39;center&#39;>".$this->select()."</td>"."<td><span style=&#39;color:#666666&#39;>页</span></td>"."<td width=72 align=right>".$this->first_page()."</td>"."<td width=72 align=right>".$this->pre_page()."</td>"."<td width=72 align=right>".$this->next_page()."</td>"."<td width=72 align=right>".$this->last_page()."</td>"; 
return "<td align=&#39;right&#39;>共有[".$this->total."]件商品    ".$this->nowindex."/".$this->totalpage."页</td>"."<td width=145>".$this->pre_page()."  ".$this->next_page()."</td>"."<td width=145>".$this->last_page()."  ".$this->select()."  页  "; 
break; 
case &#39;7&#39;: 
$this->next_page=&#39;<img src="&#39;.HOSTPATH.&#39;images/arrpagedown.jpg"/>&#39;; 
$this->pre_page=&#39;<img src="&#39;.HOSTPATH.&#39;images/arrpageup.jpg"/>&#39;; 
$this->first_page=&#39;<img src="&#39;.HOSTPATH.&#39;images/arrhome.jpg"/>&#39;; 
$this->last_page=&#39;<img src="&#39;.HOSTPATH.&#39;images/arrend.jpg"/>&#39;; 
if($this->totalpage==0) 
{$this->nowindex=0;} 
return $this->first_page()."  ".$this->pre_page()."  ".$this->next_page()."  ".$this->last_page()."  ".$this->select(); 
break; 
case &#39;8&#39;: 
//启用了“上一页”,“下一页”,“最后一页”。可根据情况启用“第一页”。 
//$this->first_page=&#39;<img src="&#39;.HOSTPATH.&#39;images/propageup.jpg" width="64" height="13" >&#39;; 
$this->pre_page=&#39;<img src="&#39;.HOSTPATH.&#39;images/propageup.jpg" style="width:58px; height:18px;">&#39;; 
$this->next_page=&#39;<img src="&#39;.HOSTPATH.&#39;images/propagedown.jpg" style="width:58px; height:18px;">&#39;; 
$this->last_page=&#39;最后一页&#39;; 
if($this->totalpage==0) 
{$this->nowindex=0;} 
//return "<td width=39 align=&#39;center&#39;>".$this->select()."</td>"."<td><span style=&#39;color:#666666&#39;>页</span></td>"."<td width=72 align=right>".$this->first_page()."</td>"."<td width=72 align=right>".$this->pre_page()."</td>"."<td width=72 align=right>".$this->next_page()."</td>"."<td width=72 align=right>".$this->last_page()."</td>"; 
return "<td align=&#39;right&#39;>共有[".$this->total."]条信息    ".$this->nowindex."/".$this->totalpage."页</td>"."<td width=145>".$this->pre_page()."  ".$this->next_page()."</td>"."<td width=145>".$this->last_page()."  ".$this->select()."  页  "; 
break; 
} 

} 
/*----------------private function (私有方法)-----------------------------------------------------------*/ 
/** 
* 设置url头地址 
* @param: String $url 
* @return boolean 
*/ 
function _set_url($url="") 
{ 
if(!empty($url)){ 
//手动设置 
$this->url=$url.((stristr($url,&#39;?&#39;))?&#39;&&#39;:&#39;?&#39;).$this->page_name."="; 
}else{ 
//自动获取 
if(empty($_SERVER[&#39;QUERY_STRING&#39;])){ 
//不存在QUERY_STRING时 
$this->url=$_SERVER[&#39;REQUEST_URI&#39;]."?".$this->page_name."="; 
}else{ 
// 
if(stristr($_SERVER[&#39;QUERY_STRING&#39;],$this->page_name.&#39;=&#39;)){ 
//地址存在页面参数 
$this->url=str_replace($this->page_name.&#39;=&#39;.$this->nowindex,&#39;&#39;,$_SERVER[&#39;REQUEST_URI&#39;]); 
$last=$this->url[strlen($this->url)-1]; 
if($last==&#39;?&#39;||$last==&#39;&&#39;){ 
$this->url.=$this->page_name."="; 
}else{ 
$this->url.=&#39;&&#39;.$this->page_name."="; 
} 
}else{ 
// 
$this->url=$_SERVER[&#39;REQUEST_URI&#39;].&#39;&&#39;.$this->page_name.&#39;=&#39;; 
}//end if 
}//end if 
}//end if 
} 

/** 
* 设置当前页面 
* 
*/ 
function _set_nowindex($nowindex) 
{ 
if(empty($nowindex)){ 
//系统获取 

if(isset($_GET[$this->page_name])){ 
$this->nowindex=intval($_GET[$this->page_name]); 
} 
}else{ 
//手动设置 
$this->nowindex=intval($nowindex); 
} 
} 

/** 
* 为指定的页面返回地址值 
* 
* @param int $pageno 
* @return string $url 
*/ 
function _get_url($pageno=1) 
{ 
return $this->url.$pageno; 
} 

/** 
* 获取分页显示文字,比如说默认情况下_get_text(&#39;<a href="">1</a>&#39;)将返回[<a href="">1</a>] 
* 
* @param String $str 
* @return string $url 
*/ 
function _get_text($str) 
{ 
return $this->format_left.$str.$this->format_right; 
} 

/** 
* 获取链接地址 
*/ 
function _get_link($url,$text,$style=&#39;&#39;){ 
$style=(empty($style))?&#39;&#39;:&#39;class="&#39;.$style.&#39;"&#39;; 
if($this->is_ajax){ 
//如果是使用AJAX模式 
return &#39;<a &#39;.$style.&#39; href="javascript:&#39;.$this->ajax_action_name.&#39;(\&#39;&#39;.$url.&#39;\&#39;)">&#39;.$text.&#39;</a>&#39;; 
}else{ 
return &#39;<a &#39;.$style.&#39; href="&#39;.$url.&#39;">&#39;.$text.&#39;</a>&#39;; 
} 
} 
/** 
* 出错处理方式 
*/ 
function error($function,$errormsg) 
{ 
die(&#39;Error in file <b>&#39;.FILE.&#39;</b> ,Function <b>&#39;.$function.&#39;()</b> :&#39;.$errormsg); 
} 
} 
?>
Copy after login

The above is the detailed content of Sample code of paging class implemented by php+ajax. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template