自己重构的一个分页显示类
最近单位内网要写个网站,发现很多地方用到分页显示,就自己根据自己的需要写了个类,发出来请各位多多指教。本人新手代码质量可能一般,也难免会有bug,不过还是想请各位大侠多多指点,先谢了。 因为我是用的是css布局,这段代码没有采用table布局,各位要使
最近单位内网要写个网站,发现很多地方用到分页显示,就自己根据自己的需要写了个类,发出来请各位多多指教。本人新手代码质量可能一般,也难免会有bug,不过还是想请各位大侠多多指点,先谢了。
因为我是用的是css布局,这段代码没有采用table布局,各位要使用的话,需要自己修改了。
<?php /* 2015 by 秋尽西风 * 数据库操作分页显示类 * 在 wamp 环境下设计 不支持非 mysql 数据库 * 默认采用 UTF8 编码 * */ class DataByPage{ /* 类属性 * $mNumPerPage 默认分页显示每页显示的记录数 * $mNumPage 默认当前页的页码 * $mDataLink 数据库链接 * $mSqlStr 要执行的 SQL 语句 * $mOddCss 输出时奇数行 div 的 css 样式 * $mOddCss 输出时偶数行 div 的 css 样式 * $mHeaderDisplay 设置分页显示是否输出表头 * $mHeaderCss 表头的 CSS 样式 * $mDataConf 数据库的相关设置 * */ private $mNumPerPage = 10; private $mNumPage = 1; private $mDataLink =""; private $mSqlStr; private $mOddCss = "odd"; private $mEvenCss = "even"; private $mHeaderDisplay = true; private $mHeaderCss = "tableheader"; private $mTableName = ""; private $mDataConf = array( "dbHost" => "localhost", "dbLoginName" => "root", "dbPwd" => "", "dbName" => "dbName", "characterSet" => "UTF8" ); /* 构造方法 * 保存数据库信息到数组 $mDataConf * 根据信息创建数据库连接 * $dbName 数据库的名称 * $dbHost 数据库的连接地址 * $dbLoginName 数据库的登录用户名 * $dbPwd 数据库的登录密码 * */ function __construct($dbName,$dbHost="localhost",$dbLoginName="root",$dbPwd=""){ $this->mDataConf["dbHost"] = $dbHost; $this->mDataConf["dbLoginName"] = $dbLoginName; $this->mDataConf["dbPwd"] = $dbPwd; $this->mDataConf["dbName"] = $dbName; $this->mDataLink = mysql_connect($dbHost,$dbLoginName,$dbPwd); if(!$this->mDataLink){die('Could not connect: ' . mysql_error());} } /* 析构方法 * * */ function __destruct(){ mysql_close($this->mDataLink); } /* 手动设置默认分页显示每页显示的记录数 * function SetNumPerPage($numPerPage){ $this->mNumPerPage = $numPerPage; }*/ /* 手动设置要执行的 SQL 语句 * 如果这里手动设置了 SELECT 语句 在调用 PagingDisplay 方法时可以不输入任何参数 * */ function SetMySqlStr($mySqlStr){ $this->mSqlStr = $mySqlStr; } /* function GetNumPerPage(){ return $this->mNumPerPage; } */ /* 生成 SQL SELECT 语句 * $dbTable 要进行查询操作的数据表 必需 * $searchField 要查询的字段 与 $searchKey 同时设置方能生效 * $searchKey 要查询的关键字 与 $searchField 同时设置方能生效 * $sortingField 排序依据的字段 与 $sortRules 同时设置方能生效 * $sortRules 排序规则 ASC/DESC 与 $sortingField 同时设置方能生效 * $numPage 当前需要分页显示的页码 与 $numPerPage 同时设置方能生效 * $numPerPage 分页显示中每页要显示的记录数 与 $numPage 同时设置方能生效 * $tableDisplayField 需要查询/显示的字段 "" 为显示全部字段 * */ private function SetSqlSelectStr($dbTable="",$numPerPage="",$numPage="",$sortingField="",$sortRules="",$tableDisplayField="",$searchField="",$searchKey=""){ //select * from $dbTable where $searchField like %$searchKey% order by $sortingField $sortRules limit ($numPage-1)*$numPerPage,$numPerPage $sql_str = "SELECT "; if($tableDisplayField!=""){ foreach($tableDisplayField as $field){ $sql_str = $sql_str.$field.","; } $sql_str = substr_replace($sql_str," ",-1); $sql_str = $sql_str."FROM"; } else{ $sql_str = "SELECT * FROM"; } //select */$tableDisplayField from if($dbTable=="") {die("please check dbTable");} else {$sql_str = $sql_str." ".$dbTable;} //select */$tableDisplayField from $dbTable if($searchField!="" && $searchKey!="") {$sql_str = $sql_str." WHERE ".$searchField." LIKE %".$searchKey."%";} //select */$tableDisplayField from $dbTable [where $searchField like %$searchKey%] if($sortingField!="" && $sortRules!="") {$sql_str = $sql_str." ORDER BY ".$sortingField." ". $sortRules;} //select */$tableDisplayField from $dbTable where $searchField like %$searchKey% [order by $sortingField $sortRules] if($numPerPage!="" && $numPage!="") {$sql_str = $sql_str." LIMIT ".($numPage-1)*$numPerPage.",".$numPerPage;} ////select */$tableDisplayField from $dbTable where $searchField like %$searchKey% order by $sortingField $sortRules [limit ($numPage-1)*$numPerPage,$numPerPage] $this->mSqlStr = $sql_str; } /* 分页显示查询结果 * $dbTable 要进行查询操作的数据表 如果为空则需要事先手动设置 $mSqlStr:要执行的 SQL 语句 * $searchField 要查询的字段 * $searchKey 要查询的关键字 * $sortingField 排序依据的字段 * $sortRules 排序规则 ASC/DESC * $numPage 当前需要分页显示的页码 * $numPerPage 分页显示中每页要显示的记录数 * $tableDisplayField 需要查询/显示的字段 为一维数组类型 "" 为显示全部字段 * 分页显示的样式使用 CSS 控制 CSS 使用 class 选择器 奇数行的 CSS 样式为:$mOddCss 偶数行的CSS样式为:$mEvenCss 每个字段的 CSS 样式为字段名 * 表头各个字段的 CSS 样式为 字段名 + header * 表头整行 DIV 的 CSS 样式为 tableheader * */ function PagingDisplay($dbTable="",$numPage="",$numPerPage="",$sortingField="",$sortRules="",$tableDisplayField="",$searchField="",$searchKey=""){ if($dbTable!=""){ $this->mTableName = $dbTable; if($numPerPage==""){$numPerPage = $this->mNumPerPage;} else{$this->mNumPerPage = $numPerPage;} if($numPage==""){$numPage = $this->mNumPage;} else{$this->mNumPage = $numPage;} $this->SetSqlSelectStr($dbTable,$numPerPage,$numPage,$sortingField,$sortRules,$tableDisplayField,$searchField,$searchKey); } if($numPerPage!="" && $numPage!=""){ $this->mNumPerPage = $numPerPage; $this->mNumPage = $numPage; } if($this->mSqlStr==""){die("please check mSqlStr");} mysql_select_db($this->mDataConf["dbName"],$this->mDataLink); mysql_query(("SET NAMES '".$this->mDataConf["characterSet"]."'"),$this->mDataLink); echo $this->mSqlStr; $results = mysql_query($this->mSqlStr); $i = 1; while($row=mysql_fetch_assoc($results)){ if($i==1 && $this->mHeaderDisplay){ echo "<div class='".$this->mHeaderCss."'>"; foreach($row as $field=>$value){ echo "<div class='".$field."header'>".$field."</div>"; } echo "</div>"; //mysql_data_seek($results,0); } if($i++%2==1){ echo "<div class='".$this->mOddCss."'>"; } else{ echo "<div class='".$this->mEvenCss."'>"; } foreach($row as $field=>$value){ echo "<div class='".$field."'>".$value."</div>"; } echo "</div>"; } } /* 显示翻页控制 共xxx条记录 首页 上一页 下一页 末页 第x/x页 GO * $actionPage 处理链接的页面 * $numPage 分页现实的当前页码 * $searchField 如果是对搜索结果分页显示 这里填写搜索的字段 * $searchKey 如果是对搜索结果分页显示 这里填写搜索的关键字 **/ function PageingControl($actionPage,$numPage="",$searchField="",$searchKey=""){ mysql_select_db($this->mDataConf["dbName"],$this->mDataLink); mysql_query(("SET NAMES '".$this->mDataConf["characterSet"]."'"),$this->mDataLink); $temp_result = mysql_query("SELECT * FROM ".$this->mTableName,$this->mDataLink); $num_record = mysql_num_rows($temp_result); $num_page_total = ceil($num_record/$this->mNumPerPage); if($num_page_total<1){$num_page_total = 1;} if($numPage==""){$numPage = $this->mNumPage;} if($numPage<1 || $numPage>$num_page_total){$numPage = 1;} if($searchField=="" || $searchKey==""){ echo "共".$num_record."条记录 "; echo "<a href='".$actionPage."?numPage=1'>首页 </a>"; if($numPage>1) {echo "<a href='".$actionPage."?numPage=".($numPage-1)."'>上一页 </a>";} if($numPage<$num_page_total) {echo "<a href='".$actionPage."?numPage=".($numPage+1)."'>下一页 </a>";} echo "<a href='".$actionPage."?numPage=".$num_page_total."'>末页 </a>"; echo "<form action='".$actionPage."' method='get'>第<input name='numPage' type='text' value=".$numPage." />/".$num_page_total."页<input type=submit value='GO' /></form>"; } else{ echo "共".$num_record."条记录 "; echo "<a href='".$actionPage."?numPage=1&searchField=".$searchField."&searchKey=".$searchKey."'>首页 </a>"; if($numPage>1) {echo "<a href='".$actionPage."?numPage=".($numPage-1)."&searchField=".$searchField."&searchKey=".$searchKey."'>上一页 </a>";} if($numPage<$num_page_total) {echo "<a href='".$actionPage."?numPage=".($numPage+1)."&searchField=".$searchField."&searchKey=".$searchKey."'>下一页 </a>";} echo "<a href='".$actionPage."?numPage=".$num_page_total."&searchField=".$searchField."&searchKey=".$searchKey."'>末页 </a>"; echo "<form action='".$actionPage."' method='get'>第<input name='numPage' type='text' value=".$numPage." />/".$num_page_total."页<input name='searchField' type='hidden' value='".$searchField."'/><input name='searchKey' type='hidden' value='".$searchKey."'/><input type=submit value='GO' /></form>"; } } /* function test(){ echo $this->mSqlStr; if($this->mDataLink==""){echo "no";} $results = mysql_query($this->mSqlStr,$this->mDataLink); } */ } //?>
<?php /**/ require 'databypage.class.php'; $ot = new DataByPage("ws"); $ot->PagingDisplay("ws_video",1,10); $ot->PageingControl("this.php"); ?>

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

學c語言的網站:1、C語言中文網;2、菜鳥教學;3、C語言論壇;4、C語言帝國;5、腳本之家;6、天極網;7、紅黑聯盟;8、51自學網;9、力扣;10、C Programming。詳細介紹:1、C語言中文網,這是一個專門為初學者提供C語言學習資料的網站,內容豐富,包括基礎語法、指針、數組、函數、結構體等多個模組;2、菜鳥教程,這是一個綜合性的程式設計學習網站等等。

顯示桌面佈局已鎖定是怎麼回事在使用電腦的過程中,有時我們可能會遇到顯示桌面佈局已鎖定的情況。這個問題意味著我們無法自由地調整桌面圖示的位置或改變桌面背景等操作。那麼,顯示桌面佈局已鎖定到底是怎麼回事?一、理解桌面佈局和鎖定功能首先,我們需要了解桌面佈局和桌面鎖定這兩個概念。桌面佈局是指桌面上各元素的排列方式,包括捷徑、資料夾和小工具等。我們可以自由

MyBatis是一個優秀的持久層框架,它支援基於XML和註解的方式操作資料庫,簡單易用,同時也提供了豐富的插件機制。其中,分頁插件是使用頻率較高的插件之一。本文將深入探討MyBatis分頁外掛的原理,並結合具體的程式碼範例進行說明。一、分頁外掛原理MyBatis本身並沒有提供原生的分頁功能,但可以藉助外掛程式來實現分頁查詢。分頁插件的原理主要是透過攔截MyBatis

遠端桌面連線使用的使用者非常多,很多使用者在使用的時候都會遇到些小問題,例如沒有顯示對方的任務欄,其實很可能是對方的設定問題,下面就來看看解決方法吧。遠端桌面連線怎麼顯示對方的工作列:1、先點選「設定」。 2、然後再開啟「個人化」。 3、隨後選擇左側的「工作列」4、將圖中隱藏工作列選項關閉即可。

在Linux系統中,要顯示目前路徑可以使用pwd指令。 pwd指令是PrintWorkingDirectory的縮寫,用來顯示目前工作目錄的路徑。在終端機中輸入以下指令即可顯示目前路徑:pwd執行指令後,終端機會顯示目前工作目錄的完整路徑,如:/home/user/Documents。另外,還可以使用一些其他選項來增強pwd指令的功能,例如-P選項可以顯示出

WIFI密碼不用常常輸入,所以忘了也很正常,今天我教大家一個最簡單的方法來查到自己家WIFI的密碼,3秒鐘搞定。 WIFI密碼那就是用微信的掃一掃來查看,本方法的前提是:要有一台手機能連上過WIFI。好了,下面開始操作教學:步驟1、我們進入手機,從手機頂部下拉,調出狀態欄,WIFI圖標步驟2、長按WIFI圖標,進入WLAN設定;長按WIFI圖標步驟3、點擊已經連接上的自己家的WIFI名稱,點擊分享密碼,它會彈出來二維碼;分享WIFI密碼步驟4、我們截圖保存此二維碼;步驟5、桌面長按微信圖標,點擊掃

PHP陣列分頁有兩種最常見的方式:使用array_slice()函數:計算要跳過的元素數量,然後提取指定範圍的元素。使用內建迭代器:實作Iterator接口,rewind()、key()、current()、next()和valid()方法用於遍歷指定範圍內的元素。

在Go語言程式開發中,函數重構技巧是十分重要的一環。透過優化和重構函數,不僅可以提高程式碼品質和可維護性,還可以提升程式的效能和可讀性。本文將深入探討Go語言中的函數重構技巧,結合具體的程式碼範例,幫助讀者更能理解和應用這些技巧。 1.程式碼範例1:提取重複程式碼片段在實際開發中,經常會遇到重複使用的程式碼片段,這時就可以考慮將重複程式碼提取出來作為一個獨立的函數,以
