使用分頁鏈接導航時,我無法保留查詢結果。
我寫了一個代碼,它根據搜索表單中選擇的選項創建一個mysql查詢。它它採用的$ _POST結果,其中在這裏建立查詢變量的自提交表單的代碼:
<?php
if (isset($_POST)) {
$find = array();
$area = array();
if (isset($_POST ['location']) && !($_POST ['location'] == "000")) {
$find [] = $_POST ['location'];
$area [] = "location";
}
if (isset($_POST ['sector']) && !($_POST ['sector'] == "000")) {
$find [] = $_POST ['sector'];
$area [] = "sector";
}
if (isset($_POST ['hours']) && !($_POST ['hours'] == "000")) {
$find [] = $_POST ['hours'];
$area [] = "hours";
}
while ((list($key1, $val1) = each($find)) && (list($key2, $val2) = each($area))) {
if ($key1 == 0) {
$result = " WHERE " . strtolower($val2) . "= " . "'" . strtolower($val1) . "'" . " " ;
}
if ($key1 >= 1) {
$result .= "AND " . strtolower($val2) . "= " . "'" . strtolower($val1) . "'" . " " ;
}
}
} else {
$result = NULL ;
}
?>
這樣做的問題是,當我選擇分頁鏈接的一個它刷新頁面,該頁面刪除原始的SQL查詢,這意味着我只是獲取表中的所有記錄,而不是基於搜索的結果。
到目前爲止,我已經嘗試使用cookie來保留搜索表單生成的變量,當用戶提交另一個搜索時取消設置cookie。這在某種程度上是有效的,但是由於某種原因,在選擇分頁鏈接2次後cookie會消失。
這是我追加到所設置的cookie的代碼,你會發現編輯對if語句的底部:
<?php
if (isset($_POST)) {
$find = array();
$area = array();
if (isset($_POST ['location']) && !($_POST ['location'] == "000")) {
$find [] = $_POST ['location'];
$area [] = "location";
}
if (isset($_POST ['sector']) && !($_POST ['sector'] == "000")) {
$find [] = $_POST ['sector'];
$area [] = "sector";
}
if (isset($_POST ['hours']) && !($_POST ['hours'] == "000")) {
$find [] = $_POST ['hours'];
$area [] = "hours";
}
while ((list($key1, $val1) = each($find)) && (list($key2, $val2) = each($area))) {
if ($key1 == 0) {
$result = " WHERE " . strtolower($val2) . "= " . "'" . strtolower($val1) . "'" . " " ;
}
if ($key1 >= 1) {
$result .= "AND " . strtolower($val2) . "= " . "'" . strtolower($val1) . "'" . " " ;
}
}
setcookie("testcookie", "$result", time()-36000);
setcookie("testcookie", $result);
} else {
$result = NULL ;
}
?>
下面是相應的代碼的其餘部分:
我分配cookie到一個變量:
$ ret_result = str_replace('\','',$ _COOKIE [「testcookie」]);
然後進行查詢:生成
與查詢$sql = "SELECT * FROM posting $ret_result ";
我也有問題,我第一次提交表單。在查詢分配給變量之前,我必須提交兩次表單。試圖刪除cookie時也是這種情況!
我在這裏虧本,所以任何指針將不勝感激。
感謝