哪位帮忙看下这个分页类如何调用,小弟我说的是在查询语句下
哪位帮忙看下这个分页类怎么调用,我说的是在查询语句下
网上找到一个比较好看的分页类,尽管作者已经说明怎么调用了,但是对我来说还是不太明白,主要是在sql语句里面怎么调用,下面是分页类代码:
<br /><?php<br />/**<br /> * 可以灵活定制的分页类<br /> * <br /> * 可以定制的选项包括,分页链接显示效果,当前页码链接按钮的样式,URL中获取分页值的名字,可以随意带自己的参数<br /> * <br /> * 使用方法:<br /> * 1、初始化类的时候需要传入参数,类型为数组。<br /> * array(<br /> * (必填)'totalRows'=>'100', 需要显示的数据的总条数;<br /> * (必填)'pageSize'=>'2', 每页需要显示的代码数量;<br /> * (必填)'currentPage'=>$_GET['p'], 当前页码,默认可以通过$_GET['p']获取,其中名字p可以定制<br /> * (必填)'baseUrl'=>'/welcome?id=3',你当前页面的链接地址,比如为http://www.xxx.com/test.php(或者/test.php),如果后面带有参数则可以为http://www.xxx.com/test?id=8<br /> * (选填,默认为3)'offset'=>'3', 当前页码的左右偏移量,比如当前页码为5,则在5的左右各显示几个数字链接,默认为3个,则效果为2,3,4,5,6,7,8<br /> * (选填,默认为p)'pageString'=>'p',通过$_GET['p']获取当前页码时候的名字,默认为p<br /> * (选填,默认为here)'className'=>'here',当前页码链接按钮的样式,默认样式名为here,所以你可以这样写css样式.here{background:#FF4500;} )<br /> * <br /> * 2、可以使用的方法。<br /> * A、初始化类后,需要调用pagination([$style = '1'][,$output=TRUE])方法产生分页链接<br /> * 关于参数的说明:<br /> * @param $style (默认为 1,可不填写) :获取链接全部组件,即 首页+上一页+数字链接+下一页+尾页<br /> * @param $style == 2 :仅获取数字链接<br /> * @param $style == 3 :仅获取上一页+下一页<br /> * @param $style == 4 :仅获取上一页+数字链接+下一页,(不包含首尾页)<br /> * <br /> * @param $output (默认为TRUE),返回分页链接<br /> * @param $output 为FALSE时,直接输出分页链接<br /> * <br /> * B、getCurrentPage()获取当前页码,经过真伪判断后的,防止用户自行输入错误,比如http://www.xxx.com/test?p=-100;此时通过此方法获取当前页码为1<br /> * <br /> * C、pageAmount()获取总的页码数量<br /> * <br /> * @author 星空幻颖<br /> * @link http://blog.sina.com.cn/yanyinghq<br /> *<br /> */<br />class Page<br />{<br /> private $pageSize; //您的网站每一页显示的列表条数<br /> private $totalRows; //通过数据库查询返回的总的记录条数<br /> private $url; //基准URL<br /> private $pageAmount; //页码的总数<br /> private $currentPage; //当前的页码<br /> private $offset = 4; //页码偏移量<br /> private $pageString = 'p'; //页码在URL中的名字<br /> private $classHere = 'class="here"'; //当前页链接的class样式类名,默认为here<br /> <br /> //初始化当前页码,记录总条数,每页多少条记录<br /> public function __construct($param)<br /> {<br /> $this->pageSize = $param['pageSize'];<br /> $this->totalRows = $param['totalRows'];<br /> $this->url = $param['baseUrl'];<br /> $this->offset = !empty($param['offset'])?$param['offset']:$this->offset;<br /> $this->pageString = !empty($param['pageString'])?$param['pageString']:$this->pageString;<br /> $this->classHere = !empty($param['className'])?$param['className']:$this->classHere;<br /> $this->currentPage = (int)$param['currentPage'];<br /> }<br /> <br /> /**<br /> * 创建分页链接<br /> * <br /> * @param $style 默认为 1 :获取链接全部组件<br /> * @param $style == 2 :仅获取数字链接<br /> * @param $style == 3 :仅获取上一页,下一页<br /> * @param $style == 4 :仅获取上一页、下一页、数字链接,不包含首尾页<br /> * <br /> * @param $output 为TRUE时,返回分页链接<br /> * @param $output 为FALSE时,直接输出分页链接<br /> * <br /> */<br /> public function pagination($style = '1',$output=TRUE)<br /> {<br /> $this->baseUrl();<br /> $this->pageAmount();<br /> $this->currentPage();<br /> <br /> //获取全部组件<br /> if($style == '1')<br /> {<br /> $page = $this->indexPage().$this->prevPage().$this->pageNumber().$this->nextPage().$this->endPage();<br /> }<br /> else if($style == '2')<br /> {<br /> //获取纯数字链接<br /> $page = $this->pageNumber();<br /> }<br /> else if($style == '3')<br /> {<br /> //只获取上一页下一页<br /> $page = $this->prevPage().$this->nextPage();<br /> }<br /> else if($style =='4')<br /> {<br /> //上一页、下一页、数字链接<br /> $page = $this->prevPage().$this->pageNumber().$this->nextPage();<br /> }<br /> <br /> if($output)<br /> {<br /> return $page;<br /> }<br /> else<br /> {<br /> echo $page;<br /> }<br /> }<br /> <br /> /**<br /> * 获取当前页码<br /> * <br /> * @return 当前页码,经过真伪判断的<br /> */<br /> public function getCurrentPage()<br /> {<br /> $this->pageAmount();<br /> $this->currentPage();<br /> return $this->currentPage;<br /> }<br /> <br /> /**<br /> * 计算出所有的页数<br /> * <br /> * 可以类外面直接调用此方法返回页码总数<br /> * <br /> * @return 页码的总数<br /> */<br /> public function pageAmount()<br /> {<br /> $this->pageAmount = ceil( $this->totalRows / $this->pageSize);<br /> if($this->pageAmount <= 0)<br /> {<br /> $this->pageAmount = '1';<br /> }<br /> return $this->pageAmount;<br /> }<br /> <br /> /**<br /> * 判断基准链接是否携带参数<br /> * <br /> * 基准链接为用户提交当前页码链接<br /> * <br /> * 如果携带参数,则在链接之后加&p=<br /> * <br /> * 如果不携带参数,则直接加?p=<br /> */<br /> private function baseUrl()<br /> {<br /> if(preg_match('/\?/', $this->url))<br /> {<br /> $this->url = $this->url.'&'.$this->pageString.'=';<br /> }<br /> else<br /> {<br /> $this->url = $this->url.'?'.$this->pageString.'=';<br /> }<br /> }<br /> <br /> /**<br /> * 验证当前页码的真伪性<br /> * <br /> * 如果当前页码小于1或者没有,则默认当前页码为1<br /> * <br /> * 如果当前页码大于页码总数,则默认当前页码为页码总数<br /> * <br /> */<br /> private function currentPage()<br /> {<br /> if($this->currentPage < 1 || !$this->currentPage)<br /> {<br /> $this->currentPage = 1;<br /> }<br /> else if(($this->currentPage > $this->pageAmount))<br /> {<br /> $this->currentPage = $this->pageAmount;<br /> }<br /> }<br /> <br /> /**<br /> * 首页链接<br /> */<br /> private function indexPage()<br /> {<br /> if($this->currentPage == 1) return;<br /> return '<a href="'.$this->url.'1">首页</a>';<br /> }<br /> <br /> /**<br /> * 尾页链接<br /> */<br /> private function endPage()<br /> {<br /> if($this->currentPage == $this->pageAmount) return;<br /> return '<a href="'.$this->url.$this->pageAmount.'">尾页</a>';<br /> }<br /> <br /> /**<br /> * 上一页<br /> */<br /> private function prevPage()<br /> {<br /> if($this->currentPage == 1) return;<br /> return '<a href="'.$this->url.( $this->currentPage - 1 ).'">上一页</a>';<br /> }<br /> <br /> /**<br /> * 下一页<br /> */<br /> private function nextPage()<br /> {<br /> if($this->currentPage == $this->pageAmount) return;<br /> return '<a href="'.$this->url.( $this->currentPage + 1 ).'">下一页</a>';<br /> }<br /> <br /> /**<br /> * 中间页码的链接<br /> * <br /> */<br /> private function pageNumber()<br /> {<br /> $left ="";<br /> $right = "";<br /> <br /> //如果总记录的条数“大于”所有链接的数量时候<br /> if($this->pageAmount > ($this->offset * 2 + 1))<br /> {<br /> //当前页码距离首页的距离<br /> $leftNum = $this->currentPage - 1;<br /> <br /> //当前页码距离尾页的距离<br /> $rightNum = $this->pageAmount - $this->currentPage;<br /> <br /> //当当前页码距离首页距离不足偏移量offset时候,在右边补齐缺少的小方块<br /> if( $leftNum < $this->offset)<br /> {<br /> //左边的链接<br /> for($i = $leftNum; $i >= 1 ; $i--)<br /> {<br /> $left .= '<a href="'.$this->url.( $this->currentPage - $i ).'">'.( $this->currentPage - $i ).'</a>';<br /> }<br /> <br /> //右边的链接<br /> for($j = 1; $j <= ($this->offset * 2 - $leftNum); $j++)<br /> {<br /> $right .= '<a href="'.$this->url.( $this->currentPage + $j ).'">'.( $this->currentPage + $j ).'</a>';<br /> }<br /> }<br /> else if($rightNum < $this->offset)<br /> {<br /> //左边的链接<br /> for($i = ($this->offset * 2 - $rightNum); $i >= 1 ; $i--)<br /> {<br /> $left .= '<a href="'.$this->url.( $this->currentPage - $i ).'">'.( $this->currentPage - $i ).'</a>';<br /> }<br /> <br /> //右边的链接<br /> for($j = 1; $j <= $rightNum; $j++)<br /> {<br /> $right .= '<a href="'.$this->url.( $this->currentPage + $j ).'">'.( $this->currentPage + $j ).'</a>';<br /> }<br /> }<br /> else<br /> {<br /> //当前链接左边的链接<br /> for($i = $this->offset; $i >= 1 ; $i--)<br /> {<br /> $left .= '<a href="'.$this->url.( $this->currentPage - $i ).'">'.( $this->currentPage - $i ).'</a>'; <br /> }<br /> <br /> //当前链接右边的链接<br /> for($j = 1; $j <= $this->offset; $j++)<br /> {<br /> $right .= '<a href="'.$this->url.( $this->currentPage + $j ).'">'.( $this->currentPage + $j ).'</a>';<br /> }<br /> }<br /> <br /> return $left.'<a href="'.$this->url.$this->currentPage.'" class="here">'.$this->currentPage.'</a>'.$right;<br /> }<br /> else<br /> {<br /> $allLink='';<br /> //当页码总数小于需要显示的链接数量时候,则全部显示出来<br /> for($j = 1; $j <= $this->pageAmount; $j++)<br /> {<br /> $allLink.='<a href="'.$this->url.$j.'" '.($j == $this->currentPage?$this->classHere:'').'>'.$j.'</a>';<br /> }<br /> return $allLink;<br /> }<br /> }<br /> <br />}<br />
------解决思路----------------------
这个类只负责分页条的产生
唯一可能与数据库查询有关的是参数数组 $param['totalRows'] 项
因为待分页的总行数是查询得到的,所以这个查询应在 $param 赋值之前完成
------解决思路----------------------
楼主也够懒的了,我这里有个给你,样式都写好了
<br /><?php<br />class Page { <br />private $total; //数据表中总记录数<br />private $listRows; //每页显示行数<br />private $limit;<br />private $uri; <br />private $pageNum; //页数<br />private $config=array('header'=>"个记录", "prev"=>"上一页", "next"=>"下一页", "first"=>"首 页", "last"=>"尾 页"); <br />private $listNum=8;<br />/* $total 11. * $listRows 12. */ <br />public function __construct($total, $listRows=10, $pa=""){ <br /> $this->total=$total; <br /> $this->listRows=$listRows; <br /> $this->uri=$this->getUri($pa); <br /> $this->page=!empty($_GET["page"]) ? $_GET["page"] : 1; <br /> $this->pageNum=ceil($this->total/$this->listRows); <br /> $this->limit=$this->setLimit(); <br /> } <br />private function setLimit(){ <br /> return "Limit ".($this->page-1)*$this->listRows.", {$this->listRows}"; <br /> } <br />private function getUri($pa){ <br /> $url=$_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"], '?')?'':"?").$pa; <br /> $parse=parse_url($url); <br /> if(isset($parse["query"])){ <br /> parse_str($parse['query'],$params); <br /> unset($params["page"]); <br /> $url=$parse['path'].'?'.http_build_query($params); <br /> } <br /> return $url; <br /> } <br />public function __get($args){ <br /> if($args=="limit") <br /> return $this->limit; <br /> else <br /> return null; <br /> } <br />private function start(){ <br /> if($this->total==0) <br /> return 0; <br /> else <br /> return ($this->page-1)*$this->listRows+1; <br /> } <br />private function end(){ <br /> return min($this->page*$this->listRows,$this->total);<br /> } <br />private function first(){ <br /> if($this->page==1) <br /> $html.=''; <br /> else <br /> $html.="<li><a href='{$this->uri}&page=1'>{$this->config["first"]}</a></li>"; <br /> return $html; <br /> } <br />private function prev(){<br /> if($this->page==1)<br /> $html.='';<br /> else <br /> $html.="<li><a href='{$this->uri}&page=".($this->page-1)."'>{$this->config["prev"]}</a></li>"; <br /> return $html;<br /> } <br />private function pageList(){<br /> $linkPage=""; <br /> $inum=floor($this->listNum/2); <br /> for($i=$inum; $i>=1; $i--){ <br /> $page=$this->page-$i;<br /> if($page<1)<br /> continue; <br /> $linkPage.="<li><a href='{$this->uri}&page={$page}'>{$page}</a></li>";<br /> } <br /> $linkPage.="<li><a href='#' class='here'>{$this->page}</a></li>";<br />for($i=1; $i<=$inum; $i++){ <br />$page=$this->page+$i; <br />if($page<=$this->pageNum) <br />$linkPage.="<li><a href='{$this->uri}&page={$page}'>{$page}</a></li>";<br />else <br />break; <br />}<br />return $linkPage;<br />}<br />private function next(){ <br />if($this->page==$this->pageNum)<br />$html.=''; <br />else <br />$html.="<li><a href='{$this->uri}&page=".($this->page+1)."'>{$this->config["next"]}</a></li>"; <br />return $html;<br />}<br />private function last(){ <br />if($this->page==$this->pageNum)<br />$html.='';<br />else<br />$html.="<li><a href='{$this->uri}&page=".($this->pageNum)."'>{$this->config["last"]}</a></li>";<br />return $html;<br />}<br />/*private function goPage(){<br />return ' <input type="text" class="inputall input50" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>'.$this->pageNum.')?'.$this->pageNum.':this.value;location=\''.$this->uri.'&page=\'+page+\'\'}" value="'.$this->page.'" style="width:25px"> <input type="button" value="GO" style="width:45px;height:30;line-height:30px;border-radius:4px;" onclick="javascript:var page=(this.previousSibling.value>'.$this->pageNum.')?'.$this->pageNum.':this.previousSibling.value;location=\''.$this->uri.'&page=\'+page+\'\'"> ';<br />}*/<br />private function goPage(){<br />return ' <input type="text" class="inputall input50" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>'.$this->pageNum.')?'.$this->pageNum.':this.value;location=\''.$this->uri.'&page=\'+page+\'\'}" value="'.$this->page.'" style="width:25px"><input type="button" value="GO" style="width:45px;height:30;line-height:30px;border-radius:4px;" onclick="javascript:var page=(this.previousSibling.value>'.$this->pageNum.')?'.$this->pageNum.':this.previousSibling.value;location=\''.$this->uri.'&page=\'+page+\'\'"> ';<br />}<br />function fpage($display=array(0,1,2,3,4,5,6,7,8)){<br />$html[0]="共有<b>{$this->total}</b>{$this->config["header"]} ";<br />$html[1]=" 每页显示<b>".($this->end()-$this->start()+1)."</b>条,本页<b>{$this->start()}-{$this->end()}</b>条 "; <br />$html[2]=" <b>{$this->page}/{$this->pageNum}</b>页 ";<br />$html[3]=$this->first();<br />$html[4]=$this->prev();<br />$html[5]=$this->pageList();<br />$html[6]=$this->next();<br />$html[7]=$this->last();<br />$html[8]=$this->goPage();<br />$fpage='';<br />foreach($display as $index){ <br />$fpage.=$html[$index];<br />} <br />return $fpage;<br />} <br />}<br />?><br />
具体调用方法:
$num=15; //每页显示数
$page=new page($total,$num);
$result=$db->query("select * from ".$db->table('article').$where." order by addtime desc {$page->limit}");
循环
fpage(array(3,4,5,6,7,0,1,2,8));?> //php分页类调用,这些数字可以去类文件里面看
-
fpage(array(3,4,5,6,7,8));?>
CSS样式文件
.fenye li{float:left; font-family:Arial, Helvetica, sans-serif; margin-left:6px; display:inline; line-height:30px;}
.fenye a{display:block;height:30px; min-width:30px; text-align:center; font-size:14px; border:1px solid #d6d6d6; float:left; margin-left:3px; padding:3px 5px;line-height:30px;text-decoration:none;color:#666;}
.fenye a:hover{background:#FF4500;border-color:#FF4500; color:#FFF;}
.fenye a.here{background:#FF4500;border-color:#FF4500; color:#FFF;}
.fenye .sel{background:#E5EDF2; color:#333; font-weight:bold; border:1px #C2D5E3 solid; padding:0 12px; border-radius:4px}
下面上效果图:


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible
