«
PHP实现翻页处理的类

时间:2008-5-31    作者:Deri    分类: 分享


   <p>  PHP实例源代码:PHP实现翻页处理的类</p><code><?php<br />class Page{<br />var $CountAll; //共有纪录数<br />var $CountPage; //每页显示记录数<br />var $Link; //显示 完整的分页信息<br />var $ForPage; //上一页<br />var $NextPage; //下一页<br />var $FirstPage; //第一页<br />var $LastPage; //最后一页<br />var $CurrPage; //第几页<br />var $PageNum; //共有多少页<br />var $Parameter; //参数<br />var $LimitNum; //不是统计全部记录,而是显示部分记录,例如共有100条记录,但是只统计显示前50条<br />function Page($sql, $num=30){<br />//初始化,统计记录数<br />$this->CountPage = $num;<br />global $mysql;<br />$sql = base64_decode($sql);<br />$result = $mysql->Query($sql);<br />if (0 != $mysql->AffectedRows()){<br />$row = $mysql->FetchArray($result);<br />$this->CountAll = $row[0];<br />}<br />else{<br />$this->CountAll = 0;<br />}<br />//print "共有 $this->CountAll <br>";<br />}<br />function ListPage($sql, $page=0,$sql_all,$other){<br />//查询,定义变量,获取数据<br />global $mysql;<br />//print "sql sql<br>";<br />if (isset($this->LimitNum) && $this->CountAll > $this->LimitNum){<br />$this->CountAll = $this->LimitNum;<br />}//更新总浏览记录数<br />$sql_src = $sql;<br />//if ($page > 0){<br />$sql = base64_decode($sql);<br />$sql_all = base64_decode($sql_all);<br />$sql_src = $sql;<br />//}<br />//echo $sql;<br />if (($this->CountAll % $this->CountPage) == 0)//统计共有多少页<br />$pagecount = (integer)($this->CountAll/$this->CountPage);<br />else<br />$pagecount = (integer)($this->CountAll/$this->CountPage)+1;<br />$this->ageNum = $pagecount;<br />if ($page > $this->ageNum)//如果页码超过页码总数则设为最大页码<br />$page = $this->ageNum;<br />if ($page <= 0)//如果页码小于等于零则将页码设置为1<br />$page = 1;<br />if ($this->CountAll == 0)<br />{<br />$this->CurrPage = 0;<br />}else{<br />$this->CurrPage = $page;<br />}<br />$first_start = ($page-1)*$this->CountPage;<br />$sql = $sql." limit ".$first_start.", ".$this->CountPage;<br />//print "2sql<br>";<br />$result = $mysql->Query($sql);<br />if (0 != $mysql->AffectedRows()){<br />$i = 0;<br />while($row = $mysql->FetchArray($result)){<br />$array[$i] = $row;<br />//print "name:".$array[$i][Name]."<br>";<br />$i++;<br />}<br />}<br />$sql = base64_encode($sql_src);<br />$sql_all = base64_encode($sql_all);<br />if ($pagecount >1){<br />if($page == 1){<br />$nextpage = $page+1;<br />$forpage = 1;<br />$this->Link = "<button onClick="javascript:location.href='?query_sql=$sql&query_page=$nextpage".$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/next.gif" align="absmiddle"> </button><button onClick="javascript:location.href='?query_sql=$sql&query_page=$pagecount".$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/prev_end.gif" align="absmiddle"> </button>";<br />$this->NextPage = "<button onClick="javascript:location.href='?query_sql=$sql&query_page=$nextpage".$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/next.gif" align="absmiddle"> </button>";<br />$this->LastPage = "<button onClick="javascript:location.href='?query_sql=$sql&query_page=$pagecount".$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/prev_end.gif" align="absmiddle"> </button>";<br />}<br />else if(($page > 1)&&($page < $pagecount)) {<br />$forpage = $page-1;<br />$nextpage = $page+1;<br />$this->Link = "<button onClick="javascript:location.href='?query_sql=$sql&query_page=1".$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/prev_top.gif" align="absmiddle"> </button><button onClick="javascript:location.href='?query_sql=$sql&query_page=$forpage".$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/prev.gif" align="absmiddle"> </button><button onClick="javascript:location.href='?query_sql=$sql&query_page=$nextpage".$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/next.gif" align="absmiddle"> </button><button onClick="javascript:location.href='?query_sql=$sql&query_page=$pagecount".$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/prev_end.gif" align="absmiddle"> </button>";<br />$this->ForPage = "<button onClick="javascript:location.href='?query_sql=$sql&query_page=$forpage".$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/prev.gif" align="absmiddle"> </button>";<br />$this->NextPage = "<button onClick="javascript:location.href='?query_sql=$sql&query_page=$nextpage".$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/next.gif" align="absmiddle"> </button>";<br />$this->FirstPage = "<button onClick="javascript:location.href='?query_sql=$sql&query_page=1".$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/prev_top.gif" align="absmiddle"> </button>";<br />$this->LastPage = "<button onClick="javascript:location.href='?query_sql=$sql&query_page=$pagecount".$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/prev_end.gif" align="absmiddle"> </button>";<br />}<br />else if ($page = $pagecount){<br />$forpage = $page-1;<br />$nextpage = 1;<br />$this->Link = "<button onClick="javascript:location.href='?query_sql=$sql&query_page=1".$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/prev_top.gif" align="absmiddle"> </button><button onClick="javascript:location.href='?query_sql=$sql&query_page=$forpage".$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/prev.gif" align="absmiddle"> </button>";<br />$this->FirstPage = "<button onClick="javascript:location.href='?query_sql=$sql&query_page=1".$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/prev_top.gif" align="absmiddle"> </button>";<br />$this->ForPage = "<button onClick="javascript:location.href='?query_sql=$sql&query_page=$forpage".$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/prev.gif" align="absmiddle"> </button>";<br />}<br />}<br />else{<br />$this->Link = ' ';<br />}<br />return $array;//$array;<br />}<br />}<br />/***************<br />使用例子<br />include("class.config.php");<br />include("class.mysql.php");<br />include("class.page.php");<br />global $mysql;<br />$config = new Config;<br />$mysql = new TDatabase($config);<br />$query_all = "select count(*) from user";<br />$page_object = new Page($query_all,20);<br />//new Page('统计记录个数语句',每页记录个数)<br />if(empty($query_page))<br />$query_sql = "select * from user";<br />//注意这里的变量名必须为 $query_sql $query_page ,因为下一页的连接参数默认为 query_sql query_page<br />$list = $page_object->ListPage($query_sql,$query_page);<br />//ListPage('没有limit的前一部分,系统自动根据补齐',察看的页数)<br />$page_object->Parameter = '&action=view';<br />//这是传送的Url 所带的其它参数,如果有就修改变量 Parameter ,系统自动将她补在后面<br />//显示数据<br />for ($i=0;$i< $page_object->CountPage;$i++)<br />print $list[$i][ID]."->".$list[$i][UserName]."<br>";<br />//返回的数据为二维哈西(关联)数组,一维为纪录的标识ID号,二维为哈西(关联)数组,取值标识建议采取用数据库中字段名的方法,例如list[0][UserName]。<br />//显示其他相关数据<br />echo $page_object->CountAll;//纪录总数<br />echo $page_object->CountPage;//每页显示数据个数<br />echo $page_object->Link;//显示完整的分页信息<br />echo $page_object->FirstPage;//第一页<br />echo $page_object->NextPage;//下一页<br />echo $page_object->ForPage;//上一页<br />echo $page_object->LastPage;//最后一页<br />echo $page_object->CurrPage;//第几页<br />echo $page_object->PageNum;//共有多少页<br />$mysql->DatabaseClose();<br />****************/<br />?></code></p>