php基于SQLite實(shí)現(xiàn)的分頁功能示例
本文實(shí)例講述了php基于SQLite實(shí)現(xiàn)的分頁功能。分享給大家供大家參考,具體如下:
這里操作數(shù)據(jù)庫文件使用的是前面文章《PHP基于PDO實(shí)現(xiàn)的SQLite操作類【包含增刪改查及事務(wù)等操作】》中的SQLite數(shù)據(jù)庫操作類。廢話不說,直接上代碼:
<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) == '')
{ //有問號(hào),但是后面沒有跟任何參數(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 {//有問號(hào),并且有參數(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{// 如果沒有問號(hào)(也就是說后面沒有任何參數(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();
?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP基于pdo操作數(shù)據(jù)庫技巧總結(jié)》、《php+Oracle數(shù)據(jù)庫程序設(shè)計(jì)技巧總結(jié)》、《PHP+MongoDB數(shù)據(jù)庫操作技巧大全》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP+JQuery+Ajax實(shí)現(xiàn)分頁方法詳解
這篇文章主要介紹了PHP+JQuery+Ajax實(shí)現(xiàn)分頁的方法,結(jié)合實(shí)例形式詳細(xì)分析了php數(shù)據(jù)查詢、分頁設(shè)置及ajax交互的相關(guān)技巧,并總結(jié)了分頁的相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-08-08
使用systemd設(shè)置PHP程序?yàn)榉?wù)的配置步驟
在現(xiàn)代?Linux?系統(tǒng)中,systemd?是用于管理和控制服務(wù)的標(biāo)準(zhǔn)工具,通過?systemd,我們可以輕松地將?PHP?程序配置為后臺(tái)運(yùn)行的系統(tǒng)服務(wù),本文將介紹如何為?PHP?程序設(shè)置?systemd?服務(wù)單元,并涵蓋相關(guān)配置步驟,需要的朋友可以參考下2024-10-10
php安裝dblib擴(kuò)展,連接mssql的具體步驟
下面小編就為大家?guī)硪黄猵hp安裝dblib擴(kuò)展,連接mssql的具體步驟。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03
PHP連接SQL server數(shù)據(jù)庫測(cè)試腳本運(yùn)行實(shí)例
這篇文章主要介紹了PHP連接SQL server數(shù)據(jù)庫測(cè)試腳本運(yùn)行實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
php通過Chianz.com獲取IP地址與地區(qū)的方法
這篇文章主要介紹了php通過Chianz.com獲取IP地址與地區(qū)的方法,是解析IP地址與地區(qū)非常實(shí)用的技巧,需要的朋友可以參考下2015-01-01

