我有冗長的搜索表單,我試圖保持會話中的表單值。我使用codeigniter分頁,所以我需要在會話中存儲搜索表單中輸入的值(我試圖避免將它們存儲在數據庫中)codeigniter會話值丟失在第二頁
這個問題困擾了我幾天,我無法解決它。這是代碼。爲了清楚起見,我爲我的每一行代碼的註釋:
// my submit button named search_button has been clicked
if ($this->input->post('search_button')) {
// all the values from the form are stored in array
$formValues = $this->input->post(NULL, TRUE);
// i assign the values of $formValues array to session
$this->session->set_userdata('formValues', $formValues);
// i store the values in local variable
$mySessData = $this->session->userdata('formValues');
// i count the values of the $formValues to be sure how many fields i get back
// reason for that is that my form is built dynamically
// returns 19 if the form has been submitted.
// when i submit the form, i get 19
echo "Count of form values is now: " . count($formValues);
// i print the $formValues array, to be sure that it return the form values
// and yes, it does if the form has been submitted.
// see the $formValues returns section bellow to see what is returned
echo "<pre>";
print_r($formValues);
echo "</pre>";
// i print my local variable to ensure that variable $mySessData is not empty
// see the section $mySessData retruns (1) to see returned values
echo "Confirm that sess data has been set up";
echo "<pre>";
print_r($mySessData);
echo "</pre>";
} else {
// else, means that form has not been submitted but that controller has been called from the next link
// in the pagination links. This always return empty array, that is nothing...
echo "<pre>";
print_r($mySessData);
echo "</pre>";
}
如果表單已經提交,則$ formValues返回如下:
Array
(
[addtypeid] =>
[isnew] =>
[orderby] =>
[geographicareaid] =>
[catid] => 1
[catid2] =>
[manufacturerid] =>
[modelid] =>
[yearofmanufacturing_from] =>
[yearofmanufacturing_to] =>
[hoursused_from] =>
[hoursused_to] =>
[horsepowers_from] =>
[horsepowers_to] =>
[price_from] =>
[price_to] =>
[colorid] =>
[isdamaged] =>
[search_button] => Submit‚
)
我的變量$ mySessData返回dataif形式完全相同已提交:
Array
(
[addtypeid] =>
[isnew] =>
[orderby] =>
[geographicareaid] =>
[catid] => 1
[catid2] =>
[manufacturerid] =>
[modelid] =>
[yearofmanufacturing_from] =>
[yearofmanufacturing_to] =>
[hoursused_from] =>
[hoursused_to] =>
[horsepowers_from] =>
[horsepowers_to] =>
[price_from] =>
[price_to] =>
[colorid] =>
[isdamaged] =>
[search_button] => Submit‚
)
而就結束,如果我點擊分頁鏈接
$config['base_url'] = site_url('/searchresults/display/'.$limit.'/');
,它指向相同的控制器和相同的方法,well..i得到一個空數組。
任何幫助將不勝感激。
的問候,約翰
這只是一個想法,可能不正確。您可能需要將您的CI會話存儲在數據庫表中,因爲每個Cookie只能存儲4kb的數據。另一個需要注意的是,你確定代碼正在通過正確的if/else部分。例如,如果它正在經歷'if'部分,那麼會話將因爲您分配'formValues'會話的方式而被重置。 – mic
如果聲明你會如何重寫?在這段時間被困在這個代碼中之後,我已經沒有想法......關於4kb限制..在這種情況下,我猜這些值不會存儲在會話中的第一部分,如果這是問題 – user2417624
在application/config/config.php中有一個打開DB會話的選項,會話數據庫表SQL可以在CI手冊中找到。 – mic