这个简单的PHP分页类,该如何调用?感谢。~
这里是我写的调用方法:header("Content-Type:text/html;charset=utf-8");$link=mysql_connect('localh...
这里是我写的调用方法:
header("Content-Type: text/html; charset=utf-8");
$link = mysql_connect('localhost:3306','root' ,'7758258');
if (!$link) {
die('Could not connect to MySQL: ' . mysql_error());
}
mysql_select_db('wlyz') or die(mysql_error());//选择数据库
mysql_query("set names utf8");//这里设置数据库的编码
// 测试代码
$pageSize = 10;//每页数量
$total = mysql_num_rows(mysql_query("SELECT * FROM `user`")); //查询总记录
$page = new page($total,$pageSize);
$str = $page->getPage();
$sql = "SELECT * FROM `user` order by `ID` Asc limit 0,$pageSize";
$result = mysql_query($sql,$link);
while ($rs = mysql_fetch_assoc($result)) {
echo $rs['ID']."|".$rs['zcsj']."|".$rs['sqjqm']."|".$rs['ssrj']."|".$rs['dqsj']."|".$rs['dlsj']."|".$rs['fengting']."|".'<br>';
}
echo $page->pagenum;
echo $str;
问题代码:
$sql = "SELECT * FROM `user` order by `ID` Asc limit 0,$pageSize";
0我不知到该填写什么参数。limit 0,$pageSize
这里是分页类的代码。
<?php
class page{
public $page; //当前页
public $pagenum; // 页数
public $pagesize; // 每页显示条数
public function __construct($count, $pagesize){
$this->pagenum = ceil($count/$pagesize);
$this->pagesize = $pagesize;
$this->page =(isset($_GET['p'])&&$_GET['p']>0) ? intval($_GET['p']) : 1;
}
/**
* 获得 url 后面GET传递的参数
*/
public function getUrl(){
$url = basename($_SERVER['PHP_SELF']). '?'.http_build_query($_GET);
$url = preg_replace('/[?,&]p=(\w)+/','',$url);
$url .= (strpos($url,"?") === false) ? '?' : '&';
return $url;
}
/**
* 获得分页HTML
*/
public function getPage(){
$url = $this->getUrl();
$start = $this->page-5;
$start=$start>0 ? $start : 1;
$end = $start+9;
$end = $end<$this->pagenum ? $end : $this->pagenum;
$pagestr = '';
if($this->page>5){
$pagestr = "<a href=".$url."p=1".">首页</a> ";
}
if($this->page!=1){
$pagestr.= "<a href=".$url."p=".($this->page-1).">上一页</a>";
}
for($i=$start;$i<=$end;$i++){
$pagestr.= "<a href=".$url."p=".$i.">".$i."</a> ";
}
if($this->page!=$this->pagenum){
$pagestr.="<a href=".$url."p=".($this->page+1).">下一页</a>";
}
if($this->page+5<$this->pagenum){
$pagestr.="<a href=".$url."p=".$this->pagenum.">尾页</a> ";
}
return $pagestr;
}
}
?> 展开
header("Content-Type: text/html; charset=utf-8");
$link = mysql_connect('localhost:3306','root' ,'7758258');
if (!$link) {
die('Could not connect to MySQL: ' . mysql_error());
}
mysql_select_db('wlyz') or die(mysql_error());//选择数据库
mysql_query("set names utf8");//这里设置数据库的编码
// 测试代码
$pageSize = 10;//每页数量
$total = mysql_num_rows(mysql_query("SELECT * FROM `user`")); //查询总记录
$page = new page($total,$pageSize);
$str = $page->getPage();
$sql = "SELECT * FROM `user` order by `ID` Asc limit 0,$pageSize";
$result = mysql_query($sql,$link);
while ($rs = mysql_fetch_assoc($result)) {
echo $rs['ID']."|".$rs['zcsj']."|".$rs['sqjqm']."|".$rs['ssrj']."|".$rs['dqsj']."|".$rs['dlsj']."|".$rs['fengting']."|".'<br>';
}
echo $page->pagenum;
echo $str;
问题代码:
$sql = "SELECT * FROM `user` order by `ID` Asc limit 0,$pageSize";
0我不知到该填写什么参数。limit 0,$pageSize
这里是分页类的代码。
<?php
class page{
public $page; //当前页
public $pagenum; // 页数
public $pagesize; // 每页显示条数
public function __construct($count, $pagesize){
$this->pagenum = ceil($count/$pagesize);
$this->pagesize = $pagesize;
$this->page =(isset($_GET['p'])&&$_GET['p']>0) ? intval($_GET['p']) : 1;
}
/**
* 获得 url 后面GET传递的参数
*/
public function getUrl(){
$url = basename($_SERVER['PHP_SELF']). '?'.http_build_query($_GET);
$url = preg_replace('/[?,&]p=(\w)+/','',$url);
$url .= (strpos($url,"?") === false) ? '?' : '&';
return $url;
}
/**
* 获得分页HTML
*/
public function getPage(){
$url = $this->getUrl();
$start = $this->page-5;
$start=$start>0 ? $start : 1;
$end = $start+9;
$end = $end<$this->pagenum ? $end : $this->pagenum;
$pagestr = '';
if($this->page>5){
$pagestr = "<a href=".$url."p=1".">首页</a> ";
}
if($this->page!=1){
$pagestr.= "<a href=".$url."p=".($this->page-1).">上一页</a>";
}
for($i=$start;$i<=$end;$i++){
$pagestr.= "<a href=".$url."p=".$i.">".$i."</a> ";
}
if($this->page!=$this->pagenum){
$pagestr.="<a href=".$url."p=".($this->page+1).">下一页</a>";
}
if($this->page+5<$this->pagenum){
$pagestr.="<a href=".$url."p=".$this->pagenum.">尾页</a> ";
}
return $pagestr;
}
}
?> 展开
2个回答
展开全部
0代表开始的下标,比如一页显示10条的话,那么第一页就是0,10,第二页就是(2-1)*10,10,第三页就是(3-1)*10,10
所以你那个0 就是写 limit($page-1)*$pagenum,$pagenum
所以你那个0 就是写 limit($page-1)*$pagenum,$pagenum
追问
我吧SQL语句修改为:
$sql = "SELECT * FROM `user` order by `ID` Asc limit($page-1)*$pagenum,$pageSize";
也不行啊。
MYSQL的SQL语句里好像不能直接加减乘除吧?
追答
看错 $pageSize才是每页显示条数
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询