
php 中分页的问题 代码如下:求助!
<body><?php$conn=mysql_connect("localhost","root");mysql_query("SETNAMES'gb2312'");if...
<body>
<?php
$conn=mysql_connect("localhost","root");
mysql_query("SET NAMES 'gb2312'");
if (!$conn)
{ echo "不能连接数据库";}
$db=mysql_select_db("test",$conn);
$page=$_get['page']
if($page=="")
{ $page=1;}
$pagesize=2;
$sql="select count(*) as total from say";
$result=mysql_query($sql);
$messagecount=mysql_result($result,0,"total");
$pagecount=ceil($messagecount/$pagesize);
$offset=($page-1)*$pagesize;
$sql="select * from say limit $offset,$pagesize";
$result=mysql_query($sql);
while ($row=mysql_fetch_array($result))
{
?>
<div>
<p align="left">用户: <?php echo $row[uname]?></p>
<p align="left">时间:<?php echo $row[tim]?></p>
<p align="left">留言:<?php echo $row[txt]?></p>
<hr />
<p align="left"> </p>
</div>
<?php }
mysql_close($conn);
?>
<?php }?>
<?php
if($page!=1)
{
echo "<a href=say.php?page=1>首页</a>";
echo "<a href=say.php?page=".($page-1).">前一页 </a>";
}
if($page<$page_count){
echo "<a href=say.php?page=".($page+1). ">后一页</a>";
echo "<a href=say.php?page=".$pagecount.">尾页</a>";
}
?>
<p> </p>
</body> 展开
<?php
$conn=mysql_connect("localhost","root");
mysql_query("SET NAMES 'gb2312'");
if (!$conn)
{ echo "不能连接数据库";}
$db=mysql_select_db("test",$conn);
$page=$_get['page']
if($page=="")
{ $page=1;}
$pagesize=2;
$sql="select count(*) as total from say";
$result=mysql_query($sql);
$messagecount=mysql_result($result,0,"total");
$pagecount=ceil($messagecount/$pagesize);
$offset=($page-1)*$pagesize;
$sql="select * from say limit $offset,$pagesize";
$result=mysql_query($sql);
while ($row=mysql_fetch_array($result))
{
?>
<div>
<p align="left">用户: <?php echo $row[uname]?></p>
<p align="left">时间:<?php echo $row[tim]?></p>
<p align="left">留言:<?php echo $row[txt]?></p>
<hr />
<p align="left"> </p>
</div>
<?php }
mysql_close($conn);
?>
<?php }?>
<?php
if($page!=1)
{
echo "<a href=say.php?page=1>首页</a>";
echo "<a href=say.php?page=".($page-1).">前一页 </a>";
}
if($page<$page_count){
echo "<a href=say.php?page=".($page+1). ">后一页</a>";
echo "<a href=say.php?page=".$pagecount.">尾页</a>";
}
?>
<p> </p>
</body> 展开
3个回答
展开全部
2楼的 没改根本不对。
3楼太麻烦。没耐心看....
——————————————————————————
要分页很简单你先研究下这个function: parse_url()
php 帮助文件里有具体用法。找不到可以 联系我 哈哈!~~
++++++++++++++++++++++++++++++++++++++++++
<?php
include("conn.php");//连接数据库
$pagesize=5;//每页显示的条数:
$url=$_SERVER["REQUEST_URI"];//获取本页地址-网址
$url=parse_url($url);// 解析网址--得到的是一数组
$url=$url[path];//得到解析网址的 具体信息
$numq=mysql_query("SELECT * FROM `say`");
$num = mysql_num_rows($numq);//获取总条数
if($_GET[page]){
$pageval=$_GET[page];
$page=($pageval-1)*$pagesize;
$page.=',';///懂了select 语句中limit的用法:SELECT * FROM table …… limit 10 , 20----从11条到20条。这个你懂的...吧^^
}
if($num > $pagesize){
if($pageval<=1)$pageval=1;///第0页 时出现错误
echo "共 $num 条".
" <a href=$url?page=".($pageval-1).">上一页</a> <a href=$url?page=".($pageval+1).">下一页</a>";//这是带连接的 上一页 下一页。。。 你该懂吧
}
echo $SQL="SELECT * FROM `test` limit $page $pagesize ";
$query=mysql_query($SQL);
while($row=mysql_fetch_array($query)){////while 循环 这个是必须的
echo "<hr><b>".$row[name]." | ".$row[id];
}
?>
-----就这么简单..................................
3楼太麻烦。没耐心看....
——————————————————————————
要分页很简单你先研究下这个function: parse_url()
php 帮助文件里有具体用法。找不到可以 联系我 哈哈!~~
++++++++++++++++++++++++++++++++++++++++++
<?php
include("conn.php");//连接数据库
$pagesize=5;//每页显示的条数:
$url=$_SERVER["REQUEST_URI"];//获取本页地址-网址
$url=parse_url($url);// 解析网址--得到的是一数组
$url=$url[path];//得到解析网址的 具体信息
$numq=mysql_query("SELECT * FROM `say`");
$num = mysql_num_rows($numq);//获取总条数
if($_GET[page]){
$pageval=$_GET[page];
$page=($pageval-1)*$pagesize;
$page.=',';///懂了select 语句中limit的用法:SELECT * FROM table …… limit 10 , 20----从11条到20条。这个你懂的...吧^^
}
if($num > $pagesize){
if($pageval<=1)$pageval=1;///第0页 时出现错误
echo "共 $num 条".
" <a href=$url?page=".($pageval-1).">上一页</a> <a href=$url?page=".($pageval+1).">下一页</a>";//这是带连接的 上一页 下一页。。。 你该懂吧
}
echo $SQL="SELECT * FROM `test` limit $page $pagesize ";
$query=mysql_query($SQL);
while($row=mysql_fetch_array($query)){////while 循环 这个是必须的
echo "<hr><b>".$row[name]." | ".$row[id];
}
?>
-----就这么简单..................................
展开全部
没问题 你这代码有什么问题?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
给你改了改 我最近正好写了个通用翻页类 你可以看看
<?php
//分页通用类开始
class Page{
function __construct($Num,$PageSize)
{
$this->Num = $Num;
$this->PageSize = $PageSize;
$this->Page = isset($_GET['Page'])?$_GET['Page']:1; //取当前页
$this->Total = ceil($this->Num/$this->PageSize); //获取总页
$this->Page = $this->Page>$this->Total && $this->Total>0?$this->Total:$this->Page; //当前页大于总页则为总页
$this->offset = ($this->Page-1)*$this->PageSize;
}
function OffSet()
{
return $this->offset;
}
function Total()
{
return $this->Total;
}
function Page()
{
return $this->Page;
}
function PageVal()
{
$URL = $_SERVER['REQUEST_URI'];
$Parse_Url = parse_url($URL);
if(isset($Parse_Url['query']))
{
$ParseQuery = ereg_replace("(^|&)Page=".$this->Page(), "", $Parse_Url['query']);
$URL = str_replace($Parse_Url['query'], $ParseQuery, $URL);
if($ParseQuery)
{
$URL = $URL."&Page=";
}
else
{
$URL = $URL."Page=";
}
}
else
{
$URL = $URL."?Page=";
}
$PageVal = '
<style>
.PageBlue{
margin-left:5px;
float:left;
width:29px;
text-align:center;
padding-top:5px;
padding-bottom:5px;
border:1px solid blue;
display:inline;}
.PageRed{
margin-left:5px;
float:left;
width:29px;
text-align:center;
padding-top:5px;
padding-bottom:5px;
background:red;
border:1px solid red;
display:inline;
font-weight:bold;}
.PageNoBorder{
float:left;
width:29px;
padding-top:5px;
padding-bottom:5px;
display:inline;
text-align:right;}
</style>
';
$PageVal .= '<div class="PageBlue" onmouseover=this.className="PageRed",this.style.cursor="pointer" onmouseout=this.className="PageBlue" name="'.$URL.'1">首</div>';
if($this->Page<4)
{
$Count = $this->Total>=7 ? 7 : $this->Total();
for($i=1;$i<=$Count;$i++)
{
if($i>0 && $i<=$this->Total)
{
if($this->Page==$i)
{
$PageVal .= '<div class="PageRed">'.$i.'</div>';
}
else
{
$PageVal .= '<div class="PageBlue" onmouseover=this.className="PageRed",this.style.cursor="pointer" onmouseout=this.className="PageBlue" name="'.$URL.$i.'">'.$i.'</div>';
}
}
}
}
elseif(($this->Total-3)>$this->Page)
{
for($i=$this->Page-3;$i<=$this->Page+3;$i++)
{
if($i>0 && $i<=$this->Total)
{
if($this->Page==$i)
{
$PageVal .= '<div class="PageRed">'.$i.'</div>';
}
else
{
$PageVal .= '<div class="PageBlue" onmouseover=this.className="PageRed",this.style.cursor="pointer" onmouseout=this.className="PageBlue" name="'.$URL.$i.'">'.$i.'</div>';
}
}
}
}
else
{
for($i=$this->Total-6;$i<=$this->Total;$i++)
{
if($i>0 && $i<=$this->Total)
{
if($this->Page==$i)
{
$PageVal .= '<div class="PageRed">'.$i.'</div>';
}
else
{
$PageVal .= '<div class="PageBlue" onmouseover=this.className="PageRed",this.style.cursor="pointer" onmouseout=this.className="PageBlue" name="'.$URL.$i.'">'.$i.'</div>';
}
}
}
}
if($this->Page!=$this->Total && $this->Total>6 && $this->Total()-3>$this->Page)
{
$PageVal .= '<div class="PageNoBorder">...</div>';
$PageVal .= '<div class="PageBlue" onmouseover=this.className="PageRed",this.style.cursor="pointer" onmouseout=this.className="PageBlue" name="'.$URL.$this->Total.'">'.$this->Total.'</div>';
}
$PageVal .= '<div class="PageBlue" onmouseover=this.className="PageRed",this.style.cursor="pointer" onmouseout=this.className="PageBlue" name="'.$URL.$this->Total.'">尾</div>';
return $PageVal;
}
}
//分页通用类结束
$conn=mysql_connect("localhost","root") or die("不能连接数据库");
$db=mysql_select_db("test",$conn);
mysql_query("SET NAMES 'gb2312'");
$pagesize=2;
$sql="select count(*) as total from `say`";
$result=mysql_query($sql);
$result = mysql_fetch_assoc($result);
$messagecount = $result['total'];
$page = new Page($messagecount,$pagesize); //Page(数据总数,每页显示数)
$offset=$page->OffSet();
$sql="select * from say limit $offset,$pagesize";
$result=mysql_query($sql);
while($row = mysql_fetch_assoc($result))
{
?>
<div>
<p align="left">用户: <?php echo $row[uname]?></p>
<p align="left">时间:<?php echo $row[tim]?></p>
<p align="left">留言:<?php echo $row[txt]?></p>
<hr />
<p align="left"> </p>
</div>
<div style="width:600px;overflow:hidden; height:60px;">
<?php
}
echo $page->PageVal();
?>
</div>
</body>
<!--
http://localhost/index.php?page=1 可翻页
http://localhost/index.php?a=3&page=2 翻页后 可保留a=3的参数
http://localhost/index.php?a=3&b=4&page=3 翻页后保留a=3 b=4的参数
http://localhost/index.php?a=3&page=3&b=4 翻页后保留a=3 b=4的参数
-->
<?php
//分页通用类开始
class Page{
function __construct($Num,$PageSize)
{
$this->Num = $Num;
$this->PageSize = $PageSize;
$this->Page = isset($_GET['Page'])?$_GET['Page']:1; //取当前页
$this->Total = ceil($this->Num/$this->PageSize); //获取总页
$this->Page = $this->Page>$this->Total && $this->Total>0?$this->Total:$this->Page; //当前页大于总页则为总页
$this->offset = ($this->Page-1)*$this->PageSize;
}
function OffSet()
{
return $this->offset;
}
function Total()
{
return $this->Total;
}
function Page()
{
return $this->Page;
}
function PageVal()
{
$URL = $_SERVER['REQUEST_URI'];
$Parse_Url = parse_url($URL);
if(isset($Parse_Url['query']))
{
$ParseQuery = ereg_replace("(^|&)Page=".$this->Page(), "", $Parse_Url['query']);
$URL = str_replace($Parse_Url['query'], $ParseQuery, $URL);
if($ParseQuery)
{
$URL = $URL."&Page=";
}
else
{
$URL = $URL."Page=";
}
}
else
{
$URL = $URL."?Page=";
}
$PageVal = '
<style>
.PageBlue{
margin-left:5px;
float:left;
width:29px;
text-align:center;
padding-top:5px;
padding-bottom:5px;
border:1px solid blue;
display:inline;}
.PageRed{
margin-left:5px;
float:left;
width:29px;
text-align:center;
padding-top:5px;
padding-bottom:5px;
background:red;
border:1px solid red;
display:inline;
font-weight:bold;}
.PageNoBorder{
float:left;
width:29px;
padding-top:5px;
padding-bottom:5px;
display:inline;
text-align:right;}
</style>
';
$PageVal .= '<div class="PageBlue" onmouseover=this.className="PageRed",this.style.cursor="pointer" onmouseout=this.className="PageBlue" name="'.$URL.'1">首</div>';
if($this->Page<4)
{
$Count = $this->Total>=7 ? 7 : $this->Total();
for($i=1;$i<=$Count;$i++)
{
if($i>0 && $i<=$this->Total)
{
if($this->Page==$i)
{
$PageVal .= '<div class="PageRed">'.$i.'</div>';
}
else
{
$PageVal .= '<div class="PageBlue" onmouseover=this.className="PageRed",this.style.cursor="pointer" onmouseout=this.className="PageBlue" name="'.$URL.$i.'">'.$i.'</div>';
}
}
}
}
elseif(($this->Total-3)>$this->Page)
{
for($i=$this->Page-3;$i<=$this->Page+3;$i++)
{
if($i>0 && $i<=$this->Total)
{
if($this->Page==$i)
{
$PageVal .= '<div class="PageRed">'.$i.'</div>';
}
else
{
$PageVal .= '<div class="PageBlue" onmouseover=this.className="PageRed",this.style.cursor="pointer" onmouseout=this.className="PageBlue" name="'.$URL.$i.'">'.$i.'</div>';
}
}
}
}
else
{
for($i=$this->Total-6;$i<=$this->Total;$i++)
{
if($i>0 && $i<=$this->Total)
{
if($this->Page==$i)
{
$PageVal .= '<div class="PageRed">'.$i.'</div>';
}
else
{
$PageVal .= '<div class="PageBlue" onmouseover=this.className="PageRed",this.style.cursor="pointer" onmouseout=this.className="PageBlue" name="'.$URL.$i.'">'.$i.'</div>';
}
}
}
}
if($this->Page!=$this->Total && $this->Total>6 && $this->Total()-3>$this->Page)
{
$PageVal .= '<div class="PageNoBorder">...</div>';
$PageVal .= '<div class="PageBlue" onmouseover=this.className="PageRed",this.style.cursor="pointer" onmouseout=this.className="PageBlue" name="'.$URL.$this->Total.'">'.$this->Total.'</div>';
}
$PageVal .= '<div class="PageBlue" onmouseover=this.className="PageRed",this.style.cursor="pointer" onmouseout=this.className="PageBlue" name="'.$URL.$this->Total.'">尾</div>';
return $PageVal;
}
}
//分页通用类结束
$conn=mysql_connect("localhost","root") or die("不能连接数据库");
$db=mysql_select_db("test",$conn);
mysql_query("SET NAMES 'gb2312'");
$pagesize=2;
$sql="select count(*) as total from `say`";
$result=mysql_query($sql);
$result = mysql_fetch_assoc($result);
$messagecount = $result['total'];
$page = new Page($messagecount,$pagesize); //Page(数据总数,每页显示数)
$offset=$page->OffSet();
$sql="select * from say limit $offset,$pagesize";
$result=mysql_query($sql);
while($row = mysql_fetch_assoc($result))
{
?>
<div>
<p align="left">用户: <?php echo $row[uname]?></p>
<p align="left">时间:<?php echo $row[tim]?></p>
<p align="left">留言:<?php echo $row[txt]?></p>
<hr />
<p align="left"> </p>
</div>
<div style="width:600px;overflow:hidden; height:60px;">
<?php
}
echo $page->PageVal();
?>
</div>
</body>
<!--
http://localhost/index.php?page=1 可翻页
http://localhost/index.php?a=3&page=2 翻页后 可保留a=3的参数
http://localhost/index.php?a=3&b=4&page=3 翻页后保留a=3 b=4的参数
http://localhost/index.php?a=3&page=3&b=4 翻页后保留a=3 b=4的参数
-->
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询