封装的类: page_size=$ppage_size; $this->start_index=0; } /********************************************* 构造函数:__destruct() 输入参数: ***********************************************/ function __destruct() { } /********************************************* get函数:__get() ***********************************************/ function __get($property_name) { if(isset($this->$property_name)) { return($this->$property_name); } else { return(null); } } /********************************************* set函数:__set() ***********************************************/ function __set($property_name, $value) { $this->$property_name = $value; } /********************************************* 函数名:read_data 功能: 根据sql查询语句从表中读取相应的记录 返回值:属性二维数组result[记录号][字段名] ***********************************************/ function read_data() { $psql=$this->sql; //查询数据,数据库链接等信息应在类调用的外部实现 $result=mysql_query($psql) or die(mysql_error()); $this->total_records=mysql_num_rows($result); //利用limit关键字获取本页所要显示的记录 if($this->total_records>0) { $this->start_index = ($this->current_page-1)*$this->page_size; $psql=$psql. limit .$this->start_index. , .$this->page_size; $result=mysql_query($psql) or die(mysql_error()); $this->current_records=mysql_num_rows($result); //将查询结果放在result数组中 $i=0; while($row=mysql_fetch_array($result)) { $this->result[$i]=$row; $i++; } } //获取总页数、当前页信息 $this->total_pages=ceil($this->total_records/$this->page_size); $this->first=1; $this->prev=$this->current_page-1; $this->next=$this->current_page+1; $this->last=$this->total_pages; } /********************************************* 函数名:standard_navigate() 功能: 显示首页、下页、上页、未页 ***********************************************/ function standard_navigate() { echo ; echo ; echo 第.$this->current_page.页/共.$this->total_pages.页; echo ; echo 跳到current_page.'>页; echo ; //生成导航链接 if ($this->current_page > 1) { echo first.>首页|; echo prev.>上一页|; } if( $this->current_page total_pages) { echo next.>下一页|; echo last.>末页; } echo ; echo
; } /********************************************* 函数名:full_navigate() 功能: 显示首页、下页、上页、未页 生成导航链接 如1 2 3 ... 10 11 ***********************************************/ function full_navigate() { echo ; echo ; echo 第.$this->current_page.页/共.$this->total_pages.页; echo ; echo 跳到current_page.'>页; echo ; //生成导航链接 如1 2 3 ... 10 11 $front_start = 1; if($this->current_page > $this->display_count){ $front_start = $this->current_page - $this->display_count; } for($i=$front_start;$icurrent_page;$i++){ echo [.$i .] ; } echo [.$this->current_page.]; $displaycount = $this->display_count; if($this->total_pages > $displaycount&&($this->current_page+$displaycount)total_pages){ $displaycount = $this->current_page+$displaycount; }else{ $displaycount = $this->total_pages; } for($i=$this->current_page+1;$i echo [.$i .] ; } //生成导航链接 if ($this->current_page > 1) { echo first.>首页|; echo prev.>上一页|; } if( $this->current_page total_pages) { echo next.>下一页|; echo last.>末页; } echo ; echo
; } } ?>
复制代码
写在php页面里面的代码: __set(current_page,$current_page); } else { $pagesupport->__set(current_page,1); } $pagesupport->__set(sql,select * from article ); $pagesupport->read_data();//读数据 if ($pagesupport->current_records > 0) //如果数据不为空,则组装数据 { for ($i=0; $icurrent_records; $i++) { $title = $pagesupport->result[$i][title]; $content = $pagesupport->result[$i][content]; $part=substr($content,0,400); //循环输出每条数据 echo ' '.$title.'
'.$part.'
update delet
'; } } $pagesupport->standard_navigate(); //调用类里面的这个函数,显示出分页html //关闭数据库 mysql_close($con); ?>
复制代码
来自:http://blog.csdn.net/phpfenghuo/article/details/23207099
分页, php, mysql