標籤:php sqlite
操作資料庫檔案使用的是上一個操作類。可以參考上一篇文章。本人文采很差,廢話不說,直接上代碼:
<meta charset='utf-8'><?php class SqlitePage{public function __construct(){$this->table_name='';$this->tj='';$this->page_size='';$this->current_page='';$this->total_page='';include_once 'sqlite_db.php';$this->db=new SqliteDB();//可以調用他的操作方法了}function entrance($table_name,$page_size,$tj='')//sql中不包含limit page_size為每頁顯示條數{// 首先擷取當前頁// sql = "select * from tab where "+條件+" order by "+排序+" limit "+要顯示多少條記錄+" offset "+跳過多少條記錄;$this->page_size=$page_size;$this->table_name=$table_name;$this->tj=$tj;$this->total_page=ceil($this->db->total($this->table_name,$this->tj)/$this->page_size);if (!isset($_GET['page'])) {$this->current_page=1;//如果沒有page,則設定為預設第一頁}else{$this->current_page=$_GET['page'];}if ($this->current_page>$this->total_page) {//噹噹前頁數目大於總頁數,則設定當前頁數為總頁數$this->current_page=$this->total_page;}if ($this->current_page<1) {//噹噹前頁數目大於總頁數,則設定當前頁數為總頁數$this->current_page=1;}$tj=$this->tj.' limit '.$this->page_size.' offset '.($this->current_page-1)*$this->page_size;$result=$this->db->query($this->table_name,$tj);return $result;}function page_bar(){$old_url = $_SERVER["REQUEST_URI"]; $check = strpos($old_url, '?'); $pre_urls='test';if ($check) {//如果urls中有?if(substr($old_url, $check+1) == '') { //有問號,但是後面沒有跟任何參數$first_urls=$old_url.'page=1';//首頁$pre_urls=$old_url.'page='.($this->current_page-1);//上一頁;$next_urls=$old_url.'page='.($this->current_page+1);//下一頁;$end_urls=$old_url.'page='.$this->total_page;//末頁} else {//有問號,並且有參數if (isset($_GET['page'])) {//如果參數中包含page參數,則登出這個參數unset($_GET['page']);$old_url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.http_build_query($_GET);}$first_urls=$old_url.'&page=1';//首頁$pre_urls=$old_url.'&page='.($this->current_page-1);//上一頁;$next_urls=$old_url.'&page='.($this->current_page+1);//下一頁;$end_urls=$old_url.'&page='.$this->total_page;//末頁}}else{// 如果沒有問號(也就是說後面沒有任何參數,則直接跟)$first_urls=$old_url.'?page=1';$first_urls=$old_url.'?page=1';//首頁$pre_urls=$old_url.'?page='.($this->current_page-1);//上一頁;$next_urls=$old_url.'?page='.($this->current_page+1);//下一頁;$end_urls=$old_url.'?page='.$this->total_page;//末頁}// echo $this->table_name.'table_name';return '<div class="page"><a>【共'.$this->total_page.'頁,第'.$this->current_page.'頁】</a><a href="'.$first_urls.'">首頁</a><a href="'.$pre_urls.'">上一頁</a><a href="'.$next_urls.'">下一頁</a><a href="'.$end_urls.'">末頁</a></div>';}public function get_total_page(){return ceil($this->total_record/$this->page_size);}}// $page=new PrePage();// $res=$page->entrance('log',10);// echo "<hr />";// foreach ($res as $key => $row) {// echo $row['urls'].'<br />';// }// echo $page->page_bar();?>
php sqlite分頁。