最近单位内网要写个网站,发现很多地方用到分页显示,就自己根据自己的需要写了个类,发出来请各位多多指教。本人新手代码质量可能一般,也难免会有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"); ?>

Python作为一门高级编程语言,在软件开发中得到了广泛应用。虽然Python有许多优点,但很多Python程序员经常面临的问题是,代码的可维护性较差。Python代码的可维护性包括代码的易读性、可扩展性、可重用性等方面。在本篇文章中,我们将着重讨论如何解决Python代码的可维护性差的问题。一、代码的易读性代码可读性是指代码的易读程度,它是代码可维护性的核

作为世界上最流行的编程语言之一,Java已成为许多企业和开发者的首选语言。然而,代码的重构对于保持代码质量以及开发效率至关重要。Java代码由于其复杂性,随着时间的推移可能会变得越来越难以维护。本文将讨论如何进行Java代码的重构,以提高代码质量和可维护性。了解重构的原则Java代码重构的目的在于改进代码的结构、可读性和可维护性,而不是简单的“改变代码”。因

Python是一门简单易学高效的编程语言,但是当我们在编写Python代码时,可能会遇到一些代码复杂度过高的问题。这些问题如果不解决,会使得代码难以维护,容易出错,降低代码的可读性和可扩展性。因此,在本文中,我们将讨论如何解决Python代码中的代码复杂度过高错误。了解代码复杂度代码复杂度是一种度量代码难以理解和维护的性质。在Python中,有一些指标可以用

Go语言是一门相对年轻的编程语言,虽然从语言本身的设计来看,其已经考虑到了很多优化点,使得其具备高效的性能和良好的可维护性,但是这并不代表着我们在开发Go应用时不需要优化和重构,特别是在长期的代码积累过程中,原来的代码架构可能已经开始失去优势,需要通过优化和重构来提高系统的性能和可维护性。本文将分享一些在Go语言中优化和重构的方法,希望能够对Go开发者有所帮

在Go语言程序开发中,函数重构技巧是十分重要的一环。通过优化和重构函数,不仅可以提高代码质量和可维护性,还可以提升程序的性能和可读性。本文将深入探讨Go语言中的函数重构技巧,结合具体的代码示例,帮助读者更好地理解和应用这些技巧。1.代码示例1:提取重复代码片段在实际开发中,经常会遇到重复使用的代码片段,这时就可以考虑将重复代码提取出来作为一个独立的函数,以

Python开发经验分享:如何进行代码重构和优化引言:随着软件开发的不断发展,代码的重构和优化已成为开发过程中不可或缺的一环。而Python作为一门动态、简洁的高级编程语言,也同样需要进行代码重构和优化来提高程序的性能和可维护性。本文将分享一些Python代码重构和优化的经验,帮助开发者写出更高效、更可靠的Python代码。第一部分:代码重构代码重构是指对已

随着软件开发的不断深入和代码的不断积累,代码重构已经成为了现代软件开发过程中不可避免的一部分。它是一种对系统的既定代码进行修改,以改善其结构、性能、可读性或其他相关方面的过程。在本文中,我们将探讨如何在Go语言中进行代码重构。定义好重构的目标在开始代码重构之前,我们应该制定一个清晰的重构目标。我们需要问自己一些问题,比如这段代码存在哪些问题?我们要通过重构

答案:PHP代码重构遵循提高解耦性、可读性、可维护性、减少复杂性的原则。实践:使用命名空间组织代码。用依赖注入容器解耦组件。重构冗余代码。分解大型类。使用现代代码风格。


Heiße KI -Werkzeuge

Undresser.AI Undress
KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover
Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool
Ausziehbilder kostenlos

Clothoff.io
KI-Kleiderentferner

AI Hentai Generator
Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

Heiße Werkzeuge

MantisBT
Mantis ist ein einfach zu implementierendes webbasiertes Tool zur Fehlerverfolgung, das die Fehlerverfolgung von Produkten unterstützen soll. Es erfordert PHP, MySQL und einen Webserver. Schauen Sie sich unsere Demo- und Hosting-Services an.

PHPStorm Mac-Version
Das neueste (2018.2.1) professionelle, integrierte PHP-Entwicklungstool

VSCode Windows 64-Bit-Download
Ein kostenloser und leistungsstarker IDE-Editor von Microsoft

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

Herunterladen der Mac-Version des Atom-Editors
Der beliebteste Open-Source-Editor
