我有冗长的搜索表单,我试图保持会话中的表单值。我使用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