《PHP實例:php實現分頁功能的3種方法第1/3頁》要點:
本文介紹了PHP實例:php實現分頁功能的3種方法第1/3頁,希望對您有用。如果有疑問,可以聯系我們。
PHP實例直接上代碼,希望大家仔細閱讀.
PHP實例辦法一:講sql查詢進行分頁進行,需要調用幾個函數,具體見腳本:
1.pager.class.php
PHP實例
<?php
class pager {
public $sql; //SQL查詢語句
public $datanum; //查詢所有的數據總記錄數
public $page_size; //每頁顯示記錄的條數
protected $_errstr;
protected $_conn;
protected $_query_id;
public function query($query)///這個函數有問題,暫時可以不消
{
$ret = false;
if (!empty($query)) {
if ($this->_conn === false || !is_resource($this->_conn)) {
warningLog(__METHOD__ . ': query sql with no connection', true);
return false;
}
$this->_query_id = @mysql_query($query, $this->_conn);
if ($this->_query_id === false) {
$this->_errstr = @mysql_error();
$ret = false;
} else {
$this->_errstr = 'SUCCESS';
$ret = $this->_query_id;
}
}
$msg = ($ret === false) ? 'false' : strval($ret);
debugLog(__METHOD__.": [$msg] returned for sql query [$query]");
return $ret;
}
function __construct($sql,$page_size) {
$result = mysql_query($sql);
$datanum = mysql_num_rows($result);
$this->sql=$sql;
$this->datanum=$datanum;
$this->page_size=$page_size;
}
//當前頁數
public function page_id() {
if($_SERVER['QUERY_STRING'] == ""){
return 1;
}elseif(substr_count($_SERVER['QUERY_STRING'],"page_id=") == 0){
return 1;
}else{
return intval(substr($_SERVER['QUERY_STRING'],8));
}
}
//剩余url值
public function url() {
if($_SERVER['QUERY_STRING'] == ""){
return "";
}elseif(substr_count($_SERVER['QUERY_STRING'],"page_id=") == 0){
return "&".$_SERVER['QUERY_STRING'];
}else{
return str_replace("page_id=".$this->page_id(),"",$_SERVER['QUERY_STRING']);
}
}
//總頁數
public function page_num() {
if($this->datanum == 0){
return 1;
}else{
return ceil($this->datanum/$this->page_size);
}
}
//數據庫查詢的偏移量
public function start() {
return ($this->page_id()-1)*$this->page_size;
}
//數據輸出
public function sqlquery() {
return $this->sql." limit ".$this->start().",".$this->page_size;
}
//獲取當前文件名
private function php_self() {
return $_SERVER['PHP_SELF'];
}
//上一頁
private function pre_page() {
if ($this->page_id() == 1) { //頁數等于1
return "<a href=".$this->php_self()."?page_id=1".$this->url().">上一頁</a> ";
}elseif ($this->page_id() != 1) { //頁數不等于1
return "<a href=".$this->php_self()."?page_id=".($this->page_id()-1).$this->url().">上一頁</a> ";
}
}
//顯示分頁
private function display_page() {
$display_page = "";
if($this->page_num() <= 10){ //小于10頁
for ($i=1;$i<=$this->page_num();$i++) //循環顯示出頁面
$display_page .= "<a href=".$this->php_self()."?page_id=".$i.$this->url().">".$i."</a> ";
return $display_page;
}elseif($this->page_num() > 10){ //大于10頁
if($this->page_id() <= 6){
for ($i=1;$i<=10;$i++) //循環顯示出頁面
$display_page .= "<a href=".$this->php_self()."?page_id=".$i.$this->url().">".$i."</a> ";
return $display_page;
}elseif(($this->page_id() > 6) && ($this->page_num()-$this->page_id() >= 4)){
for ($i=$this->page_id()-5;$i<=$this->page_id()+4;$i++) //循環顯示出頁面
$display_page .= "<a href=".$this->php_self()."?page_id=".$i.$this->url().">".$i."</a> ";
return $display_page;
}elseif(($this->page_id() > 6) && ($this->page_num()-$this->page_id() < 4)){
for ($i=$this->page_num()-9;$i<=$this->page_num();$i++) //循環顯示出頁面
$display_page .= "<a href=".$this->php_self()."?page_id=".$i.$this->url().">".$i."</a> ";
return $display_page;
}
}
}
//下一頁
private function next_page() {
if ($this->page_id() < $this->page_num()) { //頁數小于總頁數
return "<a href=".$this->php_self()."?page_id=".($this->page_id()+1).$this->url().">下一頁</a> ";
}elseif ($this->page_id() == $this->page_num()) { //頁數等于總頁數
return "<a href=".$this->php_self()."?page_id=".$this->page_num().$this->url().">下一頁</a> ";
}
}
// 設置分頁信息
public function set_page_info() {
$page_info = "共".$this->datanum."條 ";
$page_info .= "<a href=".$this->php_self()."?page_id=1".$this->url().">首頁</a> ";
$page_info .= $this->pre_page();
$page_info .= $this->display_page();
$page_info .= $this->next_page();
$page_info .= "<a href=".$this->php_self()."?page_id=".$this->page_num().$this->url().">尾頁</a> ";
$page_info .= "第".$this->page_id()."/".$this->page_num()."頁";
return $page_info;
}
}
?>
PHP實例2.腳本2:
PHP實例
<?php
//類的用法
// 讀取分頁類
include("pager.class.php");
// 數據庫連接初始化
// $db = new mysql();
$impeach_host = '10.81.43.139';
$impeach_usr = 'vmtest15';
$impeach_passwd = 'vmtest15';
$impeach_name = 'ufeature';
$impeach_con = mysql_connect($impeach_host, $impeach_usr, $impeach_passwd) or
die("Can't connect ".mysql_error());
mysql_select_db($impeach_name, $impeach_con);
// 這是一個sql查詢語句,并得到查詢結果
$sql = "select word from ufeature.spam_accuse_word_list where flag='0'";
// 分頁初始化
$page = new pager($sql,20);
// 20是每頁顯示的數量
// $res_1 = mysql_query($sql) or
// die("Can't get result ".mysql_error());
$result=mysql_query($page->sqlquery());
while($info = mysql_fetch_array($result,MYSQL_ASSOC)){
// while($info = mysql_fetch_array($res_1, MYSQL_ASSOC)){
echo $info["word"]."<br/>";
}
// 頁碼索引條
echo $page->set_page_info();
?>
PHP實例辦法二:使用ajax的辦法
1、首先了解SQL語句中的limit用法
PHP實例
SELECT * FROM table …… limit 開始位置 , 操作條數 (其中開始位置是從0開始的)
PHP實例例子
取前20條記錄:SELECT * FROM table …… limit?? 0 , 20
從第11條開始取20條記錄:SELECT * FROM table …… limit?? 10 , 20?
LIMIT n 等價于 LIMIT 0,n.
如select * from table LIMIT 5; //返回前5行,和 select * from table LIMIT 0,5一樣?
2、分頁原理
?所謂分頁顯示,也便是講數據庫中的結果集,一段一段顯示出來
怎么分段,當前在第幾段 (每頁有幾條,當前再第幾頁)
前10條記錄:select * from table limit 0,10
第11至20條記錄:select * from table limit 10,10
第21至30條記錄:select * from table limit 20,10
分頁公式:
(當前頁數 - 1 )X 每頁條數 , 每頁條數
PHP實例
Select * from table limit ($Page- 1) * $PageSize, $PageSize
PHP實例3、$_SERVER["REQUEST_URI"]函數
預定義服務器變量的一種,所有$_SERVER開頭的都叫做預定于服務器變量.
REQUEST_URI的作用是取得當前URI,也就除域名外后面的完整的地址路徑.
例子:
當前頁為:http://www.test.com/home.php?id=23&cid=22
echo $_SERVER["REQUEST_URI"]
結果為:/home.php?id=23&cid=22?
4、parse_url()解析URL函數
?parse_url() 是講URL解析成有固定鍵值的數組的函數?
例子
PHP實例
$ua=parse_url("http://username:password@hostname/path?arg=value#anchor");
print_r($ua);
PHP實例結果:
PHP實例
Array
(
[scheme] => http ;協議
[host] => hostname ;主機域名
[user] => username ;用戶
[pass] => password ;暗碼
[path] => /path ;路徑
[query] => arg=value ;取參數
[fragment] => anchor ;
)
?PHP實例5、代碼實例
?這個一個留言的分頁,分為3個部分,一個是數據庫設計,一個是連接頁面,一個是顯示頁面.
(1)設計數據庫
?設計數據庫名為bbs,有一個數據表為message,里面包含title,lastdate,user,content等字段,分別表示留言標題,留言日前,留言人,留言的內容
(2)連接頁面
PHP實例
<?php
$conn = @ mysql_connect("localhost", "root", "123456") or die("數據庫鏈接差錯");
mysql_select_db("bbs", $conn);
mysql_query("set names 'GBK'"); //使用GBK中文編碼;
//將空格,換行轉換為HTML可解析
function htmtocode($content) {
$content = str_replace("\n", "<br>", str_replace(" ", "?", $content)); //兩個str_replace嵌套
return $content;
}
//$content=str_replace("'","‘",$content);
//htmlspecialchars();
?>
PHP實例(3)顯示頁面
《PHP實例:php實現分頁功能的3種方法第1/3頁》是否對您有啟發,歡迎查看更多與《PHP實例:php實現分頁功能的3種方法第1/3頁》相關教程,學精學透。維易PHP學院為您提供精彩教程。
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/8464.html