2014-02-25 48 views
0

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

任何帮助将不胜感激。

的问候,约翰

+0

这只是一个想法,可能不正确。您可能需要将您的CI会话存储在数据库表中,因为每个Cookie只能存储4kb的数据。另一个需要注意的是,你确定代码正在通过正确的if/else部分。例如,如果它正在经历'if'部分,那么会话将因为您分配'formValues'会话的方式而被重置。 – mic

+0

如果声明你会如何重写?在这段时间被困在这个代码中之后,我已经没有想法......关于4kb限制..在这种情况下,我猜这些值不会存储在会话中的第一部分,如果这是问题 – user2417624

+0

在application/config/config.php中有一个打开DB会话的选项,会话数据库表SQL可以在CI手册中找到。 – mic

回答

0

我认为这将是更好的,如果使用方法=表单中的“获取”。因为您不需要维护任何会话,也不需要在数据库中存储值,所以在搜索表单的情况下,发布表单时使用get而不是post会更可取。您可以轻松地访问使用

$this ->input->get('fild_name');

您可以优化你的代码这样一个费尔德随时随地你不需要维护会话变量,你将永远有搜索费尔德valies在您的网址。使用会话另一个问题是取消设置会话变量。

我个人不认为创建会话变量只是为了维护分页不是一个好主意。而且在搜索表单中不需要安全性,因此您可以使用“获取”内容发送“post”

+0

我正在尝试实现数据库解决方案现在,我会看看如果这将对我有用......无论如何......太糟糕了,得到的选择永远不会跨过我的脑海......我们将看到 – user2417624

+0

是的,它发生了。实际上我几天前面临同样的问题。没问题让我知道如果你的想法工作.. :) – Mohan

+0

嗨。我尝试了数据库解决方案,它并没有为我工作。我也尝试你的解决方案,但我得到空的结果集。你有15分钟试试这个聊天吗? – user2417624