[請益] 多條件搜尋下之分頁功能...

看板PHP作者 (屁)時間16年前 (2009/10/22 13:05), 編輯推噓3(3012)
留言15則, 2人參與, 最新討論串1/2 (看更多)
搜尋條件有多筆,是利用表單傳遞 而分頁功能是用url傳遞 查詢結果第一頁可以正常顯示, 但如果超過一頁,要按「下一頁」或「上一頁」, 就搜尋不到資料, 應該是變數傳遞的問題?? 不知有什麼解決方法,試了好幾種都沒用... 以下是處理分頁的程式碼--- $maxRows_house_qry = 6; $pageNum_house_qry = 0; if (isset($_REQUEST['pageNum_house_qry'])) { $pageNum_house_qry = $_REQUEST['pageNum_house_qry']; } $startRow_house_qry = $pageNum_house_qry * $maxRows_house_qry; if (isset($_REQUEST['totalRows_house_qry'])) { $totalRows_house_qry = $_REQUEST['totalRows_house_qry']; } else { $all_house_qry = mysql_query($sql); $totalRows_house_qry = mysql_num_rows($all_house_qry); } $totalPages_house_qry = ceil($totalRows_house_qry/$maxRows_house_qry)-1; --------------------------- (以下是分頁連結) <?php if ($pageNum_house_qry > 0) {?> <a href="<?php @printf("?pageNum_house_qry=%d", 0); ?>">第一頁&nbsp;</a> <?php } ?> <?php if ($pageNum_house_qry > 0) { ?> <a href="<?php @printf("?pageNum_house_qry=%d", max(0,$pageNum_house_qry - 1)); ?>">▲ 上ㄧ頁&nbsp;</a> <?php } ?> <?php if ($pageNum_house_qry < $totalPages_house_qry) { ?> <a href="<?php @printf("?pageNum_house_qry=%d", min($totalPages_house_qry, $pageNum_house_qry + 1)); ?>">下一頁 ▼&nbsp;</a> <?php } ?> <?php if ($pageNum_house_qry < $totalPages_house_qry) { ?> <a href="<?php @printf("?pageNum_house_qry=%d", $totalPages_house_qry); ?>">最後頁</a> <?php } ?> <? echo "<br>(第".($pageNum_house_qry+1)."頁&nbsp;&nbsp;共".($totalPages_house_qry+1)."頁)";?> ----------------------------- (附上SQL敘述句) $sql="select * from house_info where H_ID is not null "; $sql_nums=""; if($_REQUEST['Style']!="請選擇"){ $sql.= " and Style = '".$_REQUEST['Style']."' "; //$sql_nums.="Style='".$_REQUEST['Style']."' "; } if($_REQUEST['Area']!="請選擇"){ $sql.=" and Area = '".$_REQUEST['Area']."' "; //$sql_nums.="& Area='".$_REQUEST['Area']."' "; } if($_REQUEST['Stall']!="請選擇"){ $sql.=" and Stall = '".$_REQUEST['Stall']."' "; //$sql_nums.="& Stall='".$_REQUEST['Stall']."'"; } if($_REQUEST['Addr']!=""){ $sql.=" and Addr like '%".stripslashes($_REQUEST['Addr'])."%' "; //$sql_nums.="& Addr= '".$_REQUEST['Addr']."'"; } if($_REQUEST['Room_start']!="" && $_REQUEST['Room_end']!=""){ $sql.=" and Room between ".stripslashes($_REQUEST['Room_start'])." and ".stripslashes($_REQUEST['Room_end'])." "; //$sql_nums.="& Room_start= '".$_REQUEST['Room_start']."' & Room_end = '". $_REQUEST['Room_end']."'"; } if($_REQUEST['Place_start']!="" && $_REQUEST['Place_end']!=""){ $sql.=" and Place between ".stripslashes($_REQUEST['Place_start'])." and ".stripslashes($_REQUEST['Place_end'])." "; //$sql_nums.="& Place_start = '".$_REQUEST['Place_start']."' & Place_end = '".$_REQUEST['Place_end']."' "; } if($_REQUEST['Build_start']!="" && $_REQUEST['Build_end']!=""){ $sql.=" and Building between ".stripslashes($_REQUEST['Build_start'])." and ".stripslashes($_REQUEST['Build_end'])." "; //$sql_nums.="& Build_start = '".$_REQUEST['Build_start']."' & Build_end = '".$_REQUEST['Build_end']."'"; } if($_REQUEST['Price_start']!="" && $_REQUEST['Price_end']!=""){ $sql.=" and Price between ".stripslashes($_REQUEST['Price_start'])." and ".stripslashes($_REQUEST['Price_end'])." "; //$sql_nums.="& Price_start = '".$_REQUEST['Price_start']."' & Price_end = '".$_REQUEST['Price_end'] ."'"; } $totalPages_house_qry = ceil($totalRows_house_qry/$maxRows_house_qry)-1; $sql_qry = sprintf(" %s LIMIT %d, %d",$sql , $startRow_house_qry, $maxRows_house_qry); $house_qry = mysql_query($sql_qry, $link) or die(mysql_error()); //echo $sql; -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.40.74.246 ※ 編輯: sss5 來自: 114.40.74.246 (10/22 13:09) ※ 編輯: sss5 來自: 114.40.74.246 (10/22 13:19)

10/22 14:29, , 1F
這種程式碼即使能run,日後維護的話恐怕就巴不得砍掉重練.X(
10/22 14:29, 1F

10/22 16:20, , 2F
樓上可以明顯指出是哪方面的問題嗎?我只是枚初心者,謝謝!
10/22 16:20, 2F

10/22 17:01, , 3F
搜尋不到資料最有可能是跟sql構句有關,但是你的程式碼當中只
10/22 17:01, 3F

10/22 17:01, , 4F
看到一個 $sql 卻看不到其內容以及其如何與所傳遞的參數的交
10/22 17:01, 4F

10/22 17:02, , 5F
互作用. 這無從幫起呀~
10/22 17:02, 5F

10/22 17:06, , 6F
而且'分頁連結'的部份,php code跟html code夾雜的很厲害,這
10/22 17:06, 6F

10/22 17:06, , 7F
對於日後維護(若有的話)肯定是一大負擔.
10/22 17:06, 7F

10/22 17:08, , 8F
不過這也只是說說罷了,應該大多數的php coder都會遇過這個問
10/22 17:08, 8F

10/22 17:08, , 9F
題..
10/22 17:08, 9F
※ 編輯: sss5 來自: 114.33.190.170 (10/22 17:41)

10/22 17:44, , 10F
謝謝你的說明,已補上$sql部分..
10/22 17:44, 10F

10/22 18:18, , 11F
我幫你檢查的結果$sql的結構應該沒問題. 那麼剩下的就是超連
10/22 18:18, 11F

10/22 18:19, , 12F
結的部份.你在翻頁的超連結有個totalRows_house_qry的參數要
10/22 18:19, 12F

10/22 18:19, , 13F
每次都傳才行.除非你有開session保存,不然就得每次都傳.
10/22 18:19, 13F

10/22 18:25, , 14F
在程式碼的部份裏,totalRows_house_qry即使是空值,也會被用
10/22 18:25, 14F

10/22 18:25, , 15F
來計算$totalPages_house_qry 這明顯有問題.
10/22 18:25, 15F
文章代碼(AID): #1At-W7lp (PHP)
文章代碼(AID): #1At-W7lp (PHP)