您的位置:首页技术文章

复杂检索数据并分页显示的处理方法

【字号: 日期:2023-09-13 19:20:23浏览:19作者:馨心
系统标题:复杂检索数据并分页显示的处理方法系统功能:利用临时表检索数据库数据,然后分页显示的方法:处理方法:采用临时表存放数据中间结果,根据中间结果显示数据数据的显示采用隔行的方式处理处理优点:对于复杂的查询,特别是涉及到多表的数据查询,如果直接使用查询条件,系统的开销将很大,利用临时表把数据先保存,然后处理。这样对数据库的查询只要开销一次。使用方法:只要把连接数据库的用户信息和数据表改变即可使用<?//连接数据库$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 class="text"> <td width="50%"> <div align="center">';if ($offset <> 1) { //如果偏移量是0,不显示前一页的链接 $newoffset=$offset - 1; print "<a href='https://www.uahao.com/bcjs/$PHP_SELF?offset=$newoffset'>前一页</a>"} else {print "前一页";print " ";}//显示所有的页数 for ($i=1; $i <= $pages; $i++) {$temps = "<a href='https://www.uahao.com/bcjs/".$PHP_SELF.'?offset='.$i."'>".$i."</a>";print $temps; print " ";} //检查是否是最后一页 if ($pages!=0 && $offset!=$pages) {$newoffset=$offset+1; print "<a href='https://www.uahao.com/bcjs/$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 class="text"> ';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='https://www.uahao.com/bcjs/".$row[lk_link]."'>".$row['lk_title']."</a>";print '<td width="100%" height="15" class="text">&nbsp;'.$tempstr.'</td>';print '</tr>';}print "</table>";?>
标签: PHP