实现“上一页”和“下一页amp;quot;按钮

系统标题:复杂检索数据并分页显示的处理方法
系统功能:利用临时表检索数据库数据,然后分页显示的方法:
处理方法:采用临时表存放数据中间结果,根据中间结果显示数据
数据的显示采用隔行的方式处理
处理优点:对于复杂的查询,特别是涉及到多表的数据查询,如果直接使用查询条件,系统的
开销将很大,利用临时表把数据先保存,然后处理。这样对数据库的查询只要开销一次。
使用方法:只要把连接数据库的用户信息和数据表改变即可使用 ? //连接数据库
$dbh = mysql_connect(localhost:3306,root,); mysql_select_db(test);
//把数据检索的结果保存到临时表中 $ls_sql = create temporary table temps
; $ls_sql .= select lk_title,lk_link from lk_t_content ; $ls_sql
.= ” where lk_title like %”.$searchcontent.”% “; $res =
mysql_query($ls_sql, $dbh); //得到检索数据的总数 $ls_sql = select
count(*) as rcnt_con from temps ; $res = mysql_query($ls_sql, $dbh);
$rcon = $row[“rcnt_con”]; $pages=ceil($rcon / 20);
//$pages变量现在总的页数 if (empty($offset)) { $offset=1; $curline = 0;
} else $curline = ($offset – 1) * 20; //打印表头 print table
width=”100%” border=”0″; print tr td width=”50%” div align=”center”; if
($offset 1) { //如果偏移量是0,不显示前一页的链接 $newoffset=$offset –
1; print “a href=$PHP_SELF?offset=$newoffset前一页/a”; } else { print
“前一页”; print ” “; } //显示所有的页数 for ($i=1; $i = $pages; $i++) {
$temps = “a href=”.$PHP_SELF.?offset=.$i.””.$i.”/a”; print $temps;
print ” “; } //检查是否是最后一页 if ($pages!=0 $offset!=$pages) {
$newoffset=$offset+1; print “a href=$PHP_SELF?offset=$newoffset/a”; }
else print “”; print /div /td; print td width=”50%” div align=”center”;
print “当前页:”.$offset.” 共”.$pages.”页”; print /div /td; print
“/table”; //显示查询信息 print table width=”100%” border=”1″; print tr ;
print td width=”100%” div align=”center”查询结果信息/div /td; print /tr;
$query = “select lk_title,lk_link from temps order by lk_title desc
LIMIT “.$curline.”,20″; $res = mysql_query($query, $dbh); $li_num = 0;
while ($row = mysql_fetch_array($res)) {
//采用隔行显示的方法显示信息内容 if ($li_number == 0) { tr
bgcolor=”#dedede” $li_number = 1; } else { tr bgcolor=”#ededed”
$li_number = 0; } $tempstr = “a
href=”.$row[lk_link].””.$row[lk_title].”/a”; print td width=”100%”
height=”15″ nbsp;.$tempstr./td; print /tr; } print “/table”; ?

//此页面为say.php ? include signup/mysql.inc; if($id1){ $sql=select *
from $table where id=’$id1′;//显示回复题目 $result=mysql_query($sql)or
die(mysql_error()); $row=mysql_fetch_array($result);
$title=RE:.$row[title]; } $ip=$REMOTE_ADDR; $sql=select * from
user_stus where ip=’$ip’;//查询当前用户名 $result=mysql_query($sql)or
die(mysql_error()); $row=mysql_fetch_array($result);
if(!$row)//判断用户是否登陆 { echo htmldiv id=Layer2 z-index:1BRBRBRBR;
echo div id=Layer3 z-index:2img src=pic/3.gif width=15 height=15/div;
echodiv ;echo pfont size=2 color=#FF33CC嘻嘻!! 请先登陆/font/p;
echopfont size=2 color=#FF33CCa
href=’.$PHP_SELF.?p=1&&table=$table’返回/a/font/p /div /html; exit(); }
else $userid=$row[userid]; if($ok) { if($title==||$content==) { echo
BR请您添完整后提交; } else{ $id=time();//提交文章 $date1=date(Y/m/d);
$title=htmlspecialchars($title); $content=htmlspecialchars($content);
$sql=select * from .$table; $result=mysql_query($sql)or
die(mysql_error()); $row1=mysql_num_rows($result); $row1++; if($id1){
$sql=insert into bbs_re
(id,userid,date1,title,content)values(‘$id’,’$userid’,’$date1′,’$title’,’$content’);
mysql_query($sql)or die(mysql_error()); $sql=select * from .$table.
where id=’$id1′; $result=mysql_query($sql)or die(mysql_error());
$lin=mysql_fetch_array($result); for($i=1;$i6;$i++) { $re=r.$i;
if(!$lin[$re]) break; } $sql=update .$table. set .$re.=’$id’ where
id=’$id1′; } else$sql=insert into .$table.
(id,userid,date1,title,content,row)values(‘$id’,’$userid’,’$date1′,’$title’,’$content’,’$row1′);
$result=mysql_query($sql)or die(mysql_error()); if($result) {
header(Location:php3.php?p=1&&table=$table); exit ; } } } ? html head
titlexiaoyang/title meta content=text/html; charset=gb2312 script
language=JavaScript1.2 src=js/menu_comment.js/script script
language=JavaScript1.2 src=js/fw_menu.js/script script
language=JavaScript !– function MM_reloadPage(init) {//reloads the
window if Nav4 resized if (init==true) with (navigator) {if
((appName==Netscape)&&(parseInt(appVersion)==4)) {
document.MM_pgW=innerWidth; document.MM_pgH=innerHeight;
onresize=MM_reloadPage; }} else if (innerWidth!=document.MM_pgW ||
innerHeight!=document.MM_pgH) location.reload(); }
MM_reloadPage(true); // — /script /head body bgcolor=#FFFFFF
text=#000000 div id=Layer1 style=position:absolute; left:31px; top:1px;
width:560px; height:46px; z-index:10 script
language=JavaScript1.2fwLoadMenus();/script/div div id=Layer6
style=position:absolute; left:409px; top:29px; width:118px; height:17px;
z-index:6/div div id=Layer5 style=position:absolute; left:86px;
top:63px; width:464px; height:303px; z-index:7form name=form1
method=post action=? echo $PHP_SELF ? table width=98% border=1
cellspacing=0 cellpadding=0 bordercolor=#FF99FF trtd width=26%
height=42div align=centerfont size=2 color=#FF33FF题目/font/div /td td
width=74% height=42input type=text name=title size=40 value=?echo $title
;? onMouseover=this.document.form1.title.focus();return true input
type=hidden name=table value=?echo $table ;? input type=hidden name=id1
value=? echo $id1 ;? br /td /tr trtd width=26% height=172div
align=centerfont size=2 color=#FF33FF内容/font/div /td td width=74%
height=172textarea name=content cols=40 rows=10 wrap=physical
value=?echo $content ;?
onMouseOver=this.document.form1.content.focus()/textarea /td /tr tr td
width=26% div align=center input type=reset name=Submit2 value=重写 /div
/td td width=74% div align=center input type=submit name=ok value=发表
/div /td /tr /table p/p /form /div div id=back style=position:absolute;
left:455px; top:30px; width:85px; height:3px; z-index:8hr color=#ff9999
noshade /div div id=goback style=position:absolute; left:461px;
top:23px; width:120px; height:16px; z-index:9div align=centera
href=php3.php?p=1&&table=?echo $table ;? target=_self
onMouseover=window.status=” ;return truefont size=2
color=#FF33FF返回/font/a/div /div p/p p/p /body /html

?php //本例子摘自phpbuilder.com //稍加翻译 //sprming@netease.com
$limit=20; // 每页显示的行数 $numresults=mysql_query(“select * from
TABLE where YOUR CONDITIONAL HERE order by
WHATEVER”);//换成你所需要的sql语句
$numrows=mysql_num_rows($numresults); // next determine if offset has
been passed to script, if not use 0 if (empty($offset)) { $offset=1; }
// 得到查询结果 $result=mysql_query(“select id,name,phone “. “from
TABLE where YOUR CONDITIONAL HERE “. “order by WHATEVER limit
$offset,$limit”); // 现在显示查询结果 while
($data=mysql_fetch_array($result)) { //
在这里插入您要显示的结果以及样式 } // 显示按钮 if ($offset!=1) { //
bypass PREV link if offset is 1 $prevoffset=$offset-20; print “a
href=”$PHP_SELF?offset=$prevoffset”/a nbsp; “; } // 计算页面数
$pages=intval($numrows/$limit); // $pages now contains int of pages
needed unless there is a remainder from division if ($numrows%$limit) {
// has remainder so add one page $pages++; } for ($i=1;$i=$pages;$i++) {
// 显示页数 $newoffset=$limit*($i-1); print “a
href=”$PHP_SELF?offset=$newoffset”$i/a nbsp; “; } // check to see if
last page if (!(($offset/$limit)==$pages) $pages!=1) { // not last page
so give NEXT link $newoffset=$offset+$limit; print “a
href=”$PHP_SELF?offset=$newoffset”/ap”; } ?

You can leave a response, or trackback from your own site.

Leave a Reply

网站地图xml地图