系統標題:復雜檢索數據并分頁顯示的處理方法 系統功能:利用臨時表檢索數據庫數據,然后分頁顯示的方法: 處理方法:采用臨時表存放數據中間結果,根據中間結果顯示數據 數據的顯示采用隔行的方式處理 處理優點:對于復雜的查詢,特別是涉及到多表的數據查詢,如果直接使用查詢條件,系統的 開銷將很大,利用臨時表把數據先保存,然后處理。這樣對數據庫的查詢只要開銷一次。 使用方法:只要把連接數據庫的用戶信息和數據表改變即可使用 <? //連接數據庫 $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='$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 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='".$row[lk_link]."'>".$row['lk_title']."</a>"; print '<td width="100%" height="15" class="text"> '.$tempstr.'</td>'; print '</tr>'; } print "</table>"; ?> ---------------------------- 歡迎訪問:zhangcg.oso.com.cn
|