系統標題:複雜檢索資料並分頁顯示的處理方法
系統功能:利用暫存資料表檢索資料庫資料,然後分頁顯示的方法:
處理方法:採用暫存資料表存放資料中間結果,根據中間結果顯示資料
資料的顯示採用隔行的方式處理
處理優點:對於複雜的查詢,特別是涉及到多表的資料查詢,如果直接使用查詢條件,系統的
開銷將很大,利用暫存資料表把資料先儲存,然後處理。這樣對資料庫的查詢只要開銷一次。
使用方法:只要把串連資料庫的使用者資訊和資料表改變即可使用
//串連資料庫
$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 '
'; print '
';
if ($offset <> 1) { //如果位移量是0,不顯示前一頁的連結
$newoffset=$offset - 1;
print "前一頁";
} else {
print "前一頁";
print " ";
}
//顯示所有的頁數
for ($i=1; $i <= $pages; $i++) {
$temps = "".$i."";
print $temps;
print " ";
}
//檢查是否是最後一頁
if ($pages!=0 && $offset!=$pages) {
$newoffset=$offset+1;
print "下一頁";
} else print "下一頁";
print ' | '; print '
';
print "當前頁:".$offset." 共".$pages."頁";
print ' | '; print "
";
//顯示查詢資訊
print '
'; print '
'; print '
| 查詢結果資訊 | '; print '
'; $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) {
$li_number = 1; } else {
$li_number = 0; } $tempstr = "".$row['lk_title'].""; print '
| '.$tempstr.' | '; print '
'; } print "
";
?>
----------------------------
歡迎訪問:zhangcg.oso.com.cn
http://www.bkjia.com/PHPjc/315848.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/315848.htmlTechArticle系統標題:複雜檢索資料並分頁顯示的處理方法 系統功能:利用暫存資料表檢索資料庫資料,然後分頁顯示的方法: 處理方法:採用暫存資料表存放...