您好,登錄后才能下訂單哦!
怎么在php中利用SQLite實(shí)現(xiàn)一個(gè)分頁功能?很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
代碼:
<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();//可以調(diào)用他的操作方法了 } function entrance($table_name,$page_size,$tj='')//sql中不包含limit page_size為每頁顯示條數(shù) { // 首先獲取當(dāng)前頁 // 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,則設(shè)置為默認(rèn)第一頁 } else{ $this->current_page=$_GET['page']; } if ($this->current_page>$this->total_page) {//當(dāng)當(dāng)前頁數(shù)目大于總頁數(shù),則設(shè)置當(dāng)前頁數(shù)為總頁數(shù) $this->current_page=$this->total_page; } if ($this->current_page<1) {//當(dāng)當(dāng)前頁數(shù)目大于總頁數(shù),則設(shè)置當(dāng)前頁數(shù)為總頁數(shù) $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) == '') { //有問號,但是后面沒有跟任何參數(shù) $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 {//有問號,并且有參數(shù) if (isset($_GET['page'])) {//如果參數(shù)中包含page參數(shù),則注銷這個(gè)參數(shù) 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{// 如果沒有問號(也就是說后面沒有任何參數(shù),則直接跟) $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.'" rel="external nofollow" >首頁</a> <a href="'.$pre_urls.'" rel="external nofollow" >上一頁</a> <a href="'.$next_urls.'" rel="external nofollow" >下一頁</a> <a href="'.$end_urls.'" rel="external nofollow" >末頁</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(); ?>
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。