用php编的一个分页的程序,不好用,请教高手!!

chunrain 2001-10-25 03:14:59
to nbufo(网络小生)

<?
连接数据库
$page_size=3; //(每页显示3个)
if($page<=0)
{$page=1;}
$nums=mysql_num_rows($result);
$page_count=ceil($nums/$page_size);//这个是你说的那个程序$page_count=$page*$page_size;
if($page_count>$nums)
$page_count = $nums; //
$offset=($page-1)*$page_size;
$a="select * from shijian order by date desc limit $offset,$page_size"; //(按时间排序)
$result=mysql_query($a) or die("浏览失败");
$prevpage=$page-1;
$nextpage=$page+1;
echo "目前留言板上共有".$nums."条留言,本页列出了第".$offset."至".$page_count."条\n"."<br>"; //($page_count这个变量怎么不变呀,老是显示3,怎么才能让它跟着变呀)
if ($page<=1)
echo "第一页"." ¦ ";
else
echo "<a href='$PATH_self?page=1'>第一页</a>"." ¦ ";
if ($page<2)
echo "上一页"." ¦ ";
else
echo"<a href='$PATH_self?page=$prevpage'>上一页</a>"." ¦ ";
if ($page+1>$page_count)
echo "下一页"."<br>";
else
echo"<a href='$PATH_self?page=$nextpage'>下一页</a>"."\n<br>";
while ($array=mysql_fetch_array($result))
{
echo $array['delid']."\n";
echo $array['yonghu']."\n";
echo $array['mima']."\n";
echo $array['email']."\n";
echo $array['info']."\n"."<br>";
}
?>

照你这个说的做我试了一下,可还是不好使,显示的内容是:

第一页的结果是:目前留言板上共有8条留言,本页列出了第0至3条
第二页的结果是:目前留言板上共有8条留言,本页列出了第3(我想让这个变成4)至6条
第三页的结果是:目前留言板上共有8条留言,本页列出了第6(我想让这个变成7)至8条

而且最后显示的那页的"下一页"链接,还能链接,本应该显示出最后一页就应没了,这个的显示结果是:

目前留言板上共有8条留言,本页列出了第9至8条 (其时已经没有记录了)
(这样就能一直链接下去,这个9会每显示一页自动加3,如:12、15、18这样的变,8是不变的)

我都做好几天了一直没通过,急死我了……


...全文
139 点赞 收藏 9
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
fromyourheart 2001-11-08
我做过这样的 东东,其实就是在程序立定以每一页现实的 数量,在定义一个页面的序号的 变量,每一次检索只要本页索要显示的那些记录就可以了,代码你可以试着写,SQL语句用LIMIT就可以了。
回复
runmin 2001-11-08
AGREE,

其实要想参考的话,PHPMYADMIN不知道你用没用过,这里面的分页做的就不错,而且是象楼上说的用LIMIT做出来的,很不错咯:)
回复
fjqiwei 2001-11-07
$connection = Ora_Logon ("system", "manager");
if ($connection == false)
{
echo Ora_ErrorCode($connection).": ".Ora_Error($connection)."";
exit;
}
$cursor = Ora_Open ($connection);
$query="SELECT serial, ref, id, alias, ip, TO_CHAR(msgdate, 'yyyy/mm/dd hh:mi:ss'), email, msg,sex FROM guestbook1 where flag='1' order by msgdate desc";
ora_parse($cursor, $query) or die;
ora_exec($cursor);
$i=0;
while(ora_fetch($cursor)) {
$guestbook1[$i][0] = ora_getcolumn($cursor,0);
$guestbook1[$i][1] = ora_getcolumn($cursor,1);
$guestbook1[$i][2] = ora_getcolumn($cursor,2);
$guestbook1[$i][3] = ora_getcolumn($cursor,3);
$guestbook1[$i][4] = ora_getcolumn($cursor,4);
$guestbook1[$i][5] = ora_getcolumn($cursor,5);
$guestbook1[$i][6] = ora_getcolumn($cursor,6);
$guestbook1[$i][7] = ora_getcolumn($cursor,7);

$i++;
}
ora_close($cursor);
ora_logoff($connection);

echo "<a href=default.php>继续留言</a><p>\n";

if ($QUERY_STRING!="") {
$page = $QUERY_STRING;
} else {
$page = 0;
}

$i=count($guestbook1);
$msgnum=5; // 每页5笔
$start = $page * $msgnum;
$end = $start + $msgnum;
if ($end > $i) $end=$i;
$totalpage=$i/$msgnum;

$pagestr="";
if ($page>0) $pagestr=$pagestr."<a href=list.php?".($page-1)."><上页</a> - ";
$pagestr=$pagestr."[第 ";
for ($i=0; $i<$totalpage; $i++) {
if ($i!=$page) {
$pagestr = $pagestr."<a href=list.php?$i>".($i+1)."</a> ";
} else {
$pagestr = $pagestr.($i+1)." ";
}
}
$pagestr=$pagestr." 页] ";
if ($page<($totalpage-1)) $pagestr=$pagestr."- <a href=list.php?".($page+1).">下页></a> ";

$pagestr="<div align=center>$pagestr</div>";
echo "<p>".$pagestr."<hr><p>\n";

for ($i=$start; $i<$end; $i++) {
echo "<p><hr><p>\n";
echo "<p>\n<font color=e06060>".$guestbook1[$i][5]."</font>   ";
if ($guestbook1[$i][6]!="") echo "<a href=mailto:".$guestbook1[$i][6].">";
echo "<strong>".$guestbook1[$i][3]."</strong>";
if ($guestbook1[$i][6]!="") echo "</a>";
echo "<br>\n";
$msg=$guestbook1[$i][7];

$msg=nl2br($msg);
echo $msg;
echo "<p>\n";
if ($WebmasterIP) echo "<a href=delete.php?aaa=".$guestbook1[$i][0].">删除</a>\n ";
if ($WebmasterIP) echo "<a href=update_Page.php?aaa=".$guestbook1[$i][3]."&ccc=".$guestbook1[$i][6]."&ddd=".$guestbook1[$i][7]."&eee=".$guestbook1[$i][4]."&fff=".$guestbook1[$i][14].">修改</a>\n ";
}

echo "<p><hr><p>\n";
echo $pagestr;

?>
回复
yw_ly 2001-11-01
<?session_start();
if($is_ok!="ok"){
echo "你还没登陆!!";
print "<a href=http://www.syepi.edu.cn/js983/index.html>点这儿登陆.....</a>\n";
exit;
}
?>
<html>
<head>
<style>
<!--
TD {font-size:16px;color:#0101a1}
.font { font-size: 9pt}
-->
</style>
<title>查看留言</title>
</head>
<p align="center"><b><u><font color="#008000"><font size="4">计算九八三班留言版</font><font size="1">
查看留言</font></font></u></b></p>
<?
/*联接数据库*/
$id=mysql_connect('localhost','root','210802');
mysql_select_db('liuyang',$id);
//如果有留言
if(!empty($note)) {
$l_face="mood".$R1.".gif";
$l_ip=$REMOTE_ADDR;
$l_datetime=date("Y-m-d G:i:s");
$sql="insert into guestbook(login_name,login_ip,login_face,login_datetime,content,flag)".
" values('".$l_name."','".$l_ip."','".$l_face."','".$l_datetime."','".$note."','1')";
if (mysql_query($sql,$id)<=0) { echo "Failed!"; }
}
$sql="select login_id from guestbook";
if (($result_r=mysql_query($sql,$id))<=0)
{
echo "Sql Query is Failed!";
}
/*查询总留言数*/
$numrows=mysql_num_rows($result_r);
/*设置每页显示数*/
$limit=3;
$how_page=intval($numrows/$limit);
/*计算总页数*/
$how_page=$numrows%$limit?++$how_page:$how_page;
/*如果传来的参数为空那么是第一页*/
if (empty($offset)) $offset=1;
$curr_page=$offset;
/*求出显示的初始条数*/
$offset=($offset-1)*$limit;
/*取出要显示的数据*/
$result_r=mysql_query("select * from guestbook order by login_id desc limit $offset,$limit");
$numrows=mysql_num_rows($result_r);
/*设置向前页*/
if ($curr_page!=1) {$up_page=$curr_page-1;
//$up_page=$up_page<10?"0".$up_page:$up_page;
print "<a href=\"$PHP_SELF?offset=$up_page\"><font size='2'>上一页</font></a> \n";}
if ($curr_page!=$how_page) {$down_page=$curr_page+1;
//$down_page=$down_page<10?"0".$down_page:$down_page;
print "<a href=\"$PHP_SELF?offset=$down_page\"><font size=2>下一页</font></a> \n";}
echo " ";
$curr_page=$curr_page<10?"0".$curr_page:$curr_page;
$how_page=$how_page<10?"0".$how_page:$how_page;
echo "<font size=2>当前第".$curr_page."/".$how_page."页</font>";
echo "   ";
print "<font size='2'><a href='write.php'>我要留言</a></font>\n";
echo "   ";
print "<font size='2'><a href='edit.php'>修改个人信息</a></font>\n";
echo "   ";
print "<font size='2'><a href='logout.php'>退出/重置</a></font>\n";
while($data=mysql_fetch_array($result_r))
{
?>
<HR>
<table border="0" width="100%" height="70" cellspacing=0 >
<tr>
<td width="8%" height="28">
<?
echo "<img bgcolor='#DDEEFF' align='center' border='1' src='gif/".$data["login_face"]."' width='49' height='28'></td> ";
echo "<td bgcolor='#DDEEFF' align='center' class='font' width='29%' height='28'>第".$data["login_id"]."位留言者:<a href=user_info.php?l_name=".$data['login_name'].">".$data['login_name']."</a></td>";
echo "<td bgcolor='#DDEEFF' align='center' class='font' width='15%' height='28'>[IP:".$data["login_ip"]."]</td>";
echo "<td bgcolor='#DDEEFF' align='center' class='font' height='28'>留言时间:".$data["login_datetime"]."</td>";
?>

</tr>
<tr>
<td align='center' class='font' height="40" bgcolor='#DDEEFF'>内容: </td>
<td valign='top' align='left' bgcolor='#FFFFCC' class='font' colspan="4" height="40">
<?echo $data["content"];?>
</td>
</tr>
</table>
<hr>
<?
}
/*echo "<font size='2'>页号</font>";*/
for($i=1;$i<=$how_page;$i++)
{
if ($i==$curr_page) {
$i=$i<10?"0".$i:$i;
print "<font size=2><b>$i </b></font>";}
else
{
$j=$i;
$i=$i<10?"0".$i:$i;
print "<font size='2'><a href=\"$PHP_SELF?offset=$j\"><b>$i</b></a></font>\n";
}
if ($i%30==0) echo "<br>";
}
?>
</html>
回复
sstock 2001-10-30
我做了一个类如果有兴趣可以到3w.open.edu.cn 上看看。要记得给我分数俄
调用如下;我的email : cyh.yz@263.net
<?
include ("../../include/config.inc");
$db = new DB;
$db->connect();
$sql = "select * from Visitor_Info_T order by VisitTime Desc";
$CPage = new CPAGE;
if(!$CPage->init($sql,$db,$url,8,"ID"));
{
$CPage->setAcol(1,'用户IP');
$CPage->setAcol(2,'机器名');
$CPage->setAcol(3,'端口号');
$CPage->setAcol(4,'浏览器');
$CPage->setAcol(5,'操作系统');
$CPage->setAcol(7,'访问时间');
$CPage->changeval(7,'showtime');
}
function showtime($t)
{
return date("Y年m月d日H点i分",$t);
}
function isorno($str)
{
if ($str == 1){$str = "已验证";return $str;}
if ($str == 0){$str = "未验证";return $str;}
}
?>
<html>
<head>
<title>最新一百个访问者信息</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<link rel="stylesheet" href="/css/css.css" type="text/css">
<body bgcolor="#005090" text="#CCFF00" link="#FFff00" vlink="#FFFF00" alink="#999999">
<?
echo "<p align = 'center'>最新一百个访问者列表!</p>";
$CPage->showbutton();
$CPage->start();
?>
</body>
</html>
回复
zxyufan 2001-10-28
<?
//分页控制类
//宇凡 8月15日
?>
<?
class PageControl
{
var $PageAmount; //总页数
var $RecordAmount; //总记录数
var $Page; //当前页码
var $RecordRow_Per_aPage = 5; //每页显示的记录条数,事先预定,不通过程序更改
var $NextPage; //下一页的页码
var $PriorPage; //上一页的页码
var $StartIndex; //相当于每页显示的起始位置的索引
var $UsingIndex; //当前使用的数据的索引
var $arrIDList; //保存ID的数组

function PageControl($IDList,$PageNow)
{
if (($PageNow == "") || (round($PageNow) <= 0) )
{
$PageNow=1;
}
$this->arrIDList = $IDList;
$this->Page = $PageNow;
$this->RecordAmount = count($IDList);

if ($this->RecordAmount % $this->RecordRow_Per_aPage == 0) {
$this->PageAmount = $this->RecordAmount / $this->RecordRow_Per_aPage;
} else {
$this->PageAmount = ceil($this->RecordAmount / $this->RecordRow_Per_aPage);
}

if ($this->Page > $this->PageAmount)
{
$this->Page = $this->PageAmount;
}
$this->StartIndex = ($this->Page - 1) * $this->RecordRow_Per_aPage;
$this->UsingIndex = $this->StartIndex - 1; //这里减一的目的:得到数据的普遍方法是先Next()再Get数据,比如数据集.

if ($this->Page != $this->PageAmount) {
$this->NextPage = $this->Page + 1;
} else {
$this->NextPage = $this->PageAmount;
}

if ($this->Page != 1) {
$this->PriorPage = $this->Page - 1;
} else {
$this->PriorPage = 1;
}
}

function next_record()
{
$result = false;
if (($this->UsingIndex != $this->RecordAmount - 1) && ($this->UsingIndex - $this->StartIndex < ($RecordRow_Per_aPage - 1))) {
$this->UsingIndex += 1;
$result = true;
} else {
$result = false;
}
return $result;
}

function get()
{
$result = $this->arrIDList[$this->UsingIndex];
return $result;
}

function show_NowPage()
{
echo "<font color=red>".$this->Page."</font>/".$this->PageAmount;
}

function show_FirstLink()
{
echo " <a href=".$PHP_SELF."?p=1>最前页</a> ";
}

function show_PriorLink()
{
echo " <a href=".$PHP_SELF."?p=".$this->PriorPage.">上一页</a> ";
}

function show_NextLink()
{
echo " <a href=".$PHP_SELF."?p=".$this->NextPage.">下一页</a> ";
}

function show_LastLink()
{
echo " <a href=".$PHP_SELF."?p=".$this->PageAmount.">最后页</a> ";
}

function show_JumpForm()
{
echo "<FORM METHOD=POST ACTION=".$PHP_SELF.">直接到<INPUT TYPE=text NAME=p size=2 class=input>页 <INPUT TYPE=submit value=GOGOGO class=bottem></FORM>";
}

}
?>
回复
eastxiang 2001-10-27
你的 $page_count 是總頁數.肯定不變了!
你要有一個變量表示當前顯示的記錄..
例如;$currentRec = $offset + $page_size;
if ($currentRec > $num )
$currentRec = $num;

echo "目前留言板上共有".$nums."条留言,本页列出了第".$offset."至".$currentRec."条\n"."<br>";
回复
chunrain 2001-10-25
to c_crazyren(楚狂人) 

非常感谢你能回贴,但你的这段程序我试了,和上面的运行结果一样
回复
c_crazyren 2001-10-25
<?
$prev_page=$page-1;
$next_page=$page+1;
if($page<=1){
echo"首页 | ";
}
else{
echo"<a href='self.php?page=1'>首页</a> | ";
}
if($prev_page<1){
echo"上一页 | ";
}
else{
echo"<a href='self.php?page=$prev_page'>上一页</a> | ";
}
if($next_page>$page_count){
echo"下一页 | ";
}
else{
echo"<a href='self.php?page=$next_page'>下一页</a> | ";
}
if($page>=$page_count){
echo"末页";
}
else{
echo"<a href='self.php?page=$page_count'>末页</a>";
}
?>

你看看这个就明白了。
回复
相关推荐
发帖
基础编程
创建于2007-09-28

2.1w+

社区成员

从PHP安装配置,PHP入门,PHP基础到PHP应用
申请成为版主
帖子事件
创建了帖子
2001-10-25 03:14
社区公告
暂无公告