検索
ホームページphp教程php手册自己重构的一个分页显示类

自己重构的一个分页显示类

Jun 06, 2016 pm 07:38 PM
イントラネットページネーションユニット見せるWebサイト自分のリファクタリング

最近单位内网要写个网站,发现很多地方用到分页显示,就自己根据自己的需要写了个类,发出来请各位多多指教。本人新手代码质量可能一般,也难免会有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");
?>
声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

EditPlus 中国語クラック版

EditPlus 中国語クラック版

サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強力な PHP 統合開発環境