phpはページング表示機能を実現する


Sql式:select*from cz_goods_type order by type_id limit 1,2;
Offset:オフセット
Pagesize:ページごとに表示されるバーの数
ここでoffsetは変化しており、現在存在するページ数(current)、各ページに表示されるバー数に関係している(pagesize)
Offset=(current-1) * pagesize
最終式:select*from cz_goods_type order by type_id limit (current-1)*pagesize, pagesize;
 
ページング手順:
1、クエリーデータベース取得記録
2、ページングクラスを使用してページング情報を出力する
ページングクラスはページング情報のみを出力し、データベースの問合せを担当せず、ページングクラス:
total = $total;
		$this->pagesize = $pagesize;
		$this->current = $current;
		$this->pagenum = ceil( $total / $pagesize );
		//index.php?p=admin&c=brand&a=index&page=2
		//new    new Page(10,3,2,'index.php',array('p'=>'admin','c'=>'brand','a'=>'index'))
		$temp = array();
		foreach ($params as $k =>$v){
			//      p=admin c=brand a=index    ,        
			$temp[] = "$k=$v";
		}
		$str = implode("&", $temp); // p=admin&c=brand&a=index
		$this->url = "$script?{$str}&page="; //    ,  ,  ,   page    ,    
		$this->first = $this->getFirst(); //       
		$this->last = $this->getLast();
		$this->prev = $this->getPrev();
		$this->next = $this->getNext();
	}

	//       
	private function getFirst(){
		//           
		if ($this->current == 1) {
			//        
			return "[  ]";
		}else {
			//     
			return "[トップページ]";
		}
	}
	//       
	private function getLast(){
		//         
		if ($this->current == $this->pagenum ){
			return "[  ]";
		} else {
			return "[  ページ]";
		}
	}
	//        
	private function getPrev(){
		//        
		if ($this->current == 1){
			return "[   ]";
		} else {
			return "[ ページ]";
		}
	}
	//        
	private function getNext(){
		//       
		if ($this->current == $this->pagenum){
			return "[   ]";
		} else {
			return "[ ページ]";
		}
	}
	//   ,      
	public function showPage(){
		if ($this->pagenum >= 1){
			return "   {$this->total}    ,     {$this->pagesize}    ,
			    {$this->current}/{$this->pagenum} {$this->first} {$this->prev} 
			{$this->next} {$this->last}";
		} else {
			return "";
		}
	}
}

 
使用方法:
Modelのコード:
//          
public function getPageTypes($offset, $pagesize){
	$sql = "select * from {$this->table} order by type_id limit $offset, $pagesize";
	return $this->db->getAll($sql);
}

 
Controllerで使用するコード:
public function indexAction(){
	//1.         
	$typeModel = new TypeModel("goods_type");
	//    
	//      ,  url   (page)
	$current = isset($_GET['page']) ? $_GET['page'] : 1;
	//          
	$pagesize = 2;
	$offset = ($current - 1) * $pagesize;
	$types = $typeModel->getPageTypes($offset, $pagesize); 
	//       
	$where = "";  //     
	$total = $typeModel->total($where);
	$this->library("Page");
	$page = new Page($total, $pagesize, $current, 'index.php', array('p'=>'admin', 'c'=>'type', 'a'=>'index'));
	$pageinfo = $page->showPage();

	//2.     
	include CUR_VIEW_PATH . "goods_type_list.html";
}

 
Viewで使用する方法: