★★★★分页技术★★★★

alexlee002 2003-08-19 11:03:20
下面是源代码,我的问题是,当我查询的时候,比如我查询用户级别,出来的结果有几页的时候,我点击下一页或者填入要跳转的页面的时候,跳转的结果不是查询结果的下一页,而是所有用户列表的下一页,怎么解决这个问题??
救命!!

<TABLE cellSpacing=1 cellPadding=2 width="100%" align=center bgColor=#000000 border=0>
<tr>
<td colspan="6" align="center" bgcolor="#00CCFF"> 用户管理</td>
</tr>
<form name="form1" method="post" action="<? $PHP_SELF ?>">
<tr>
<td colspan="6" align="center" bgcolor="#00CCFF">
用户查询
<select name="select">
<option selected>请选择查询方式</option>
<option value="user_name">用户名</option>
<option value="user_addr">所在校区</option>
<option value="user_class">用户级别</option>
</select>
<input name="user_query" type="text" id="user_query" value="请填入相应的关键字">
<input type="submit" name="Submit" value="查询">
</td>
</tr>
</form>

<?
include("../connect.php");

if($_POST[select]&&$_POST[select]!="请选择查询方式"&&$_POST[user_query]!="请填入相应的关键字")
{
$sql=mysql_query("select * from lug_user where $_POST[select]=\"$_POST[user_query]\"");
$user_sum=mysql_num_rows($sql);

if(empty($_GET[offset]))
{
$_GET[offset]=0;
}
$pg_sum=ceil($user_sum/1); //总页数;


$endoffset=$pg_sum*1-1;

if(isset($_POST[jump]))
{
if($_POST[jump]<=$pg_sum)
{
$jumpto=$_POST[jump]*1-1;
$_GET[offset]=$jumpto;
}
else
{
$_GET[offset]=$endoffset;
}
}
$query=mysql_query("select * from lug_user where $_POST[select]='$_POST[user_query]' order by user_id desc limit $_GET[offset],1");

echo'
<tr>
<td colspan="6" bgcolor="#FFFFFF"><div align="center">共查询到<font color=red>'.$user_sum.'</font>位用户符合条件</div></td>
</tr>
<tr>
<td bgcolor="#00CCFF" width="12%">用户id</td>
<td bgcolor="#00CCFF" width="20%">用户名</td>
<td bgcolor="#00CCFF" width="17%">所在校区</td>
<td bgcolor="#00CCFF" width="17%">用户级别</td>
<td bgcolor="#00CCFF" width="17%">修改</td>
<td bgcolor="#00CCFF" width="17%">删除</td>
</tr>';

while($array=mysql_fetch_array($query))
{
echo'
<tr>
<td bgcolor="#FFFFFF" width="12%">'.$array[user_id].'</td>
<td bgcolor="#FFFFFF" width="20%"><a href=user_edit.php?id='.$array[user_id].'&&act=query>'.$array[user_name].'</a></td>
<td bgcolor="#FFFFFF" width="17%">'.$array[user_addr].'</td>
<td bgcolor="#FFFFFF" width="17%">'.$array[user_class].'</td>
<td bgcolor="#FFFFFF" width="17%"><a href=user_edit.php?id='.$array[user_id].'&&act=update>修改</a></td>
<td bgcolor="#FFFFFF" width="17%"><a href=user_edit.php?id='.$array[user_id].'&&act=del>删除</a></td>';
}

?>
</tr>
<tr>
<td height="27" colspan="6" bgcolor="#FFFFFF"> </td>
</tr>

<form name="form2" method="post" action="<? $PHP_SELF ?>">
<tr align="center" bgcolor="#00CCFF">
<td colspan="6"> <a href="<? $PHP_SELF ?> ?offset=0">首页 </a> <a href="<? $PHP_SELF ?> ?offset=<? echo $endoffset ?>">尾页
</a>
<?
if($_GET[offset])
{
$preoffset=$_GET[offset]-1;
echo"<a href='$PHP_SELF?offset=$preoffset'>上一页<a>";
}
if($pg_sum!=0&&(($_GET[offset]+1)/1)!=$pg_sum)
{
$nextoffset=$_GET[offset]+1;
echo"<a href='$PHP_SELF?offset=$nextoffset'>下一页<a>";
}
$pg_no=$_GET[offset]/1+1;
?>
页次:<? echo" <font color=red>$pg_no</font>/$pg_sum";?>   转到第
<input name="jump" type="text" id="jump" size="3">

<input type="submit" name="Submit2" value="GO"> </td>
</tr>
</form>
</table>
<?

}

else{
$sql=mysql_query("select * from lug_user");
$user_sum=mysql_num_rows($sql);


if(empty($_GET[offset]))
{
$_GET[offset]=0;
}
$pg_sum=ceil($user_sum/1); //总页数;


$endoffset=$pg_sum*1-1;

if(isset($_POST[jump]))
{
if($_POST[jump]<=$pg_sum)
{
$jumpto=$_POST[jump]*1-1;
$_GET[offset]=$jumpto;
}
else
{
$_GET[offset]=$endoffset;
}
}
$query=mysql_query("select * from lug_user order by user_id desc limit $_GET[offset],1");

echo'
<tr>
<td colspan="6" bgcolor="#FFFFFF"><div align="center">共有<font color=red>'.$user_sum.'</font>位用户</div></td>
</tr>
<tr>
<td bgcolor="#00CCFF" width="12%">用户id</td>
<td bgcolor="#00CCFF" width="20%">用户名</td>
<td bgcolor="#00CCFF" width="17%">所在校区</td>
<td bgcolor="#00CCFF" width="17%">用户级别</td>
<td bgcolor="#00CCFF" width="17%">修改</td>
<td bgcolor="#00CCFF" width="17%">删除</td>
</tr>';

while($array=mysql_fetch_array($query)){
echo'
<tr>
<td bgcolor="#FFFFFF" width="12%">'.$array[user_id].'</td>
<td bgcolor="#FFFFFF" width="20%"><a href=user_edit.php?id='.$array[user_id].'&&act=query>'.$array[user_name].'</a></td>
<td bgcolor="#FFFFFF" width="17%">'.$array[user_addr].'</td>
<td bgcolor="#FFFFFF" width="17%">'.$array[user_class].'</td>
<td bgcolor="#FFFFFF" width="17%"><a href=user_edit.php?id='.$array[user_id].'&&act=update>修改</a></td>
<td bgcolor="#FFFFFF" width="17%"><a href=user_edit.php?id='.$array[user_id].'&&act=del>删除</a></td>'; }
?>
</tr>
<tr>
<td height="27" colspan="6" bgcolor="#FFFFFF"> </td>
</tr>

<form name="form2" method="post" action="<? $PHP_SELF ?>">
<tr align="center" bgcolor="#00CCFF">
<td colspan="6"> <a href="<? $PHP_SELF ?> ?offset=0">首页 </a> <a href="<? $PHP_SELF ?> ?offset=<? echo $endoffset ?>">尾页
</a>
<?
if($_GET[offset])
{
$preoffset=$_GET[offset]-1;
echo"<a href='$PHP_SELF?offset=$preoffset'>上一页<a>";
}
if($pg_sum!=0&&(($_GET[offset]+1)/1)!=$pg_sum)
{
$nextoffset=$_GET[offset]+1;
echo"<a href='$PHP_SELF?offset=$nextoffset'>下一页<a>";
}
$pg_no=$_GET[offset]/1+1;
?>
页次:<? echo" <font color=red>$pg_no</font>/$pg_sum";?>   转到第
<input name="jump" type="text" id="jump" size="3">

<input type="submit" name="Submit2" value="GO"> </td>
</tr>
</form>
</table>
<? }?>
...全文
24 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
mrsun 2003-08-21
  • 打赏
  • 举报
回复
网上有很多写好的分页类,拿一个就可以了,呵呵
AgathaZ 2003-08-21
  • 打赏
  • 举报
回复
点击跳转页面时,所查询的关键字也要转上去,否则关键字就为空,当然显示的是所有的页面啦!
causky 2003-08-21
  • 打赏
  • 举报
回复
//判断CGI方式
if (isset($_POST["select"]))
{
$select = $_POST["select"];
}
else if (isset($_GET["select"]))
{
$select = $_GET["select"];
}
if (isset($_POST["user_query"]))
{
$user_query = $_POST["user_query"];
}
else if (isset($_GET["user_query"]))
{
$user_query = $_GET["user_query"];
}
if (isset($_GET[offset]))
{
if ( intval($_GET[offset]) ==0)
{
$offset = 1;
}
else
{
$offset = $_GET[offset];
}
}
if ( $offset == 1 )
{
//无上一页
}
else
{
$preview = $offset - 1;
}
if ( $offset >= $page_num)
{
//无下一页
}
else
{
$nextview = $offset +1;
}
//计算分页
$perpage = 20; //每页20条
$query = "select count(*) as total from table where .....";
$result = mysql_query( $query);
.....
$row = mysql_fetch_array( $result);
$total = $row["total"];
$page_num = ceil( intval($total)/$perpage);
if ( $offset <= $page_num)
{
$thepos = $offset * $perpage;
$query = "select * from table where ..... limit $thepos,$perpage";
......................
}
/****************************************************************************************************
*link:<a href=$_SERVER["PHP_SELF"]?select=$select&user_query=$user_query&offset=$preview>上一页</a> *
*<a href=$_SERVER["PHP_SELF"]?select=$select&user_query=$user_query&offset=$nextview>上一页</a> *
****************************************************************************************************/
causky 2003-08-21
  • 打赏
  • 举报
回复
$sql=mysql_query("select * from lug_user where $_POST[select]=\"$_POST[user_query]\"");
$user_sum=mysql_num_rows($sql);
if(empty($_GET[offset]))
{
$_GET[offset]=0;
}
$pg_sum=ceil($user_sum/1); //总页数;
这种计算页数的方法是不正确的、很危险的,假如单表数据很的时候,效率很慢,甚至有可能会对系统造成严重损害,正确方式SQL因该这样select count(*) as total from lug_user where 。。。。
以这样的方式计算总数才是安全的做法
causky 2003-08-21
  • 打赏
  • 举报
回复
$_POST[]和$_GET[]造成的
分页传递的CGI value是GET方式
alexlee002 2003-08-20
  • 打赏
  • 举报
回复
1.请问你的每页假设是一个记录的吗?($pagesize=1)
-------------当然不是,这只是我测试用的(懒得家那么多用户了)

2。bluemeteor(挂月||╭∩╮(︶_︶)╭∩╮)

我按你说的作了,,但是不行啊,郁闷阿
bluemeteor 2003-08-20
  • 打赏
  • 举报
回复
楼主你点击连接再refresh本页面的时候select和user_query参数就是空了,所以就搜索全库了,你需要在点击连接的时候把select和user_query再传递一下....
<?php
$link_url=$PHP_SELF."?".select=$_POST[select]&user_query=$_POST[user_query]
?>

然后把所有的 href="<? $PHP_SELF ?> ?offset=0"> 替换为$link_url."&offset="

<form name="form2" method="post" action="<? $PHP_SELF ?>">
<tr align="center" bgcolor="#00CCFF">
<td colspan="6"> <a href="<? echo $link_url?> ?offset=0">首页 </a> <a href="<? echo $link_url ?> ?offset=<? echo $endoffset ?>">尾页
</a>
DFlyingchen 2003-08-20
  • 打赏
  • 举报
回复
(接上一贴)
取得下一页的记录值
DFlyingchen 2003-08-20
  • 打赏
  • 举报
回复
将查询语句传递到下一页,并在下一页中进行重新查询
xuzuning 2003-08-20
  • 打赏
  • 举报
回复
缓存
$_POST[select]和$_POST[user_query]
alexlee002 2003-08-20
  • 打赏
  • 举报
回复
各位,,帮帮忙啊
phpro 2003-08-20
  • 打赏
  • 举报
回复
请问你的每页假设是一个记录的吗?($pagesize=1)

$endoffset=$pg_sum*1-1;

$query=mysql_query("select * from lug_user order by user_id desc limit $_GET[offset],1");

21,882

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧