思路:
通过给页面url传递get参数,来控制每页的sql查询(mysql关键词:limit),实现分页查询
代码:
1 class getpage{ 2 3 public $pagenum; 4 public $pagecontents; 5 public $sql; 6 public $url; 7 public function __construct($a){ 8 9 $this->pagecontents = $a; 10 $this->url = ‘http://‘.$_SERVER[‘HTTP_HOST‘].$_SERVER[‘REQUEST_URI‘]; 11 } 12 //获取分页数 13 public function createpage($sql){ 14 15 $this->sql = $sql; 16 $conn = mysql_connect(‘localhost‘,‘root‘,‘root‘); 17 if(!$conn){ 18 19 die(mysql_error()); 20 } 21 mysql_select_db(‘class‘,$conn)or die(mysql_error()); 22 mysql_query("set names ‘utf8‘"); 23 $handle = mysql_query($sql); 24 $row = array(); 25 while($r = mysql_fetch_row($handle)){ 26 27 $row[] = $r; 28 } 29 //假如除的结果为小数,则进1来保证分页数量 30 $pagenum = count($row)/$this->pagecontents; 31 $this->pagenum = ceil($pagenum); 32 33 } 34 //替换page参数 35 private function replacestr(){ 36 if(isset($_GET[‘page‘])){ 37 return str_replace("?page="[email protected]$_GET[‘page‘],"", $this->url); 38 }else{ 39 @$_GET[‘page‘] = 0; 40 } 41 } 42 //页面分页显示 43 public function createstyle(){ 44 45 for($i=0;$i<$this->pagenum;$i++){ 46 47 echo "<button><a href=‘".$this->replacestr($this->url)."?page=".$i*$this->pagecontents."‘>".(1+$i)."</a></button>"; 48 } 49 } 50 public function getdata(){ 51 52 $where = " limit ".(@$_GET[‘page‘]).",".$this->pagecontents; 53 $this->sql .= $where; 54 $handle = mysql_query($this->sql); 55 $arr = array(); 56 while($r = mysql_fetch_assoc($handle)){ 57 $arr[] = $r; 58 } 59 return $arr; 60 } 61 62 }
时间: 2024-10-25 17:57:34