2014-02-25 53 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