0

我在存储会话值时遇到问题;我无法通过 AJAX Jquery 调用增加会话计数变量,并将一些值传递给函数,我之前设置的存储会话的数组也用新的数组更改。

这里有什么问题:

1>当我使用一个控制器功能时

startOnlineTest($testid=0)
 i set session countnew as 0
$this->session->set_userdata('countnew',0);
  and in view i use jquery to pass data to other function of same controller
  public function ResponseOnline($qid,$response)

使用jquery的变化效果

 echo "[removed]$(document).ready(function(){";
         foreach($question['childQuestion'] as $q=>$aq){  // 
echo "\$(\"input:radio[name=qid-$q]\").live('change',function(){
var sr=\$(\"input:radio[name=qid-$q]:checked\").map(function() {
          return $(this).val();
          }).get().join(); 
       var qid = \$(\"input:radio[name=qid-$q]\").attr(\"qaid\");
       "; echo "\$.get('"; echo base_url();echo "testpapers/ResponseOnline/'+qid+'/'+sr,{},function(data)
 {\$('#res').html(data)});
 });";}
 echo"});[removed]" ;// this script is working fine

现在的问题是,尽管我使用数组我的 ResponseOnline 代码是,但我得到了当前值覆盖的会话值

public function ResponseOnline($qid,$response)
  {
  echo "this" .$this->session->userdata('countnew'); // this is not echo as set above by function  startOnlineTest($testid=0)[/color]
     i set session countnew as
     $this->session->set_userdata('countnew',0)

 echo $d=$qid; // i know its no use but to save time as tested on $d

 $s=$response;

if($this->session->userdata('countnew')==0)   //  algo for this function i check the                                       
                                               //countnew session varaible   if 0 do this 
{                                                
$sc=$this->session->userdata('countnew');     // store the countnew variable
echo $sc=$sc+1;                               // increment the counter variable
$this->session->set_userdata('countnew',$sc);  // store it in session 
echo "this is incrementes session value";     
echo $this->session->userdata('countnew');
$r2[$d]=$s;                               // store array value $r2 with key $d and value $s
$this->session->set_userdata('res',$r2);  //set this array value in session 
}
else{                                  // if session countnew!=0 then do this
$r2=$this->session->userdata('res');  // first store $r2 as array return from session
 $r2[$d]=$s;                         //then add value to this array                      

 $this->session->set_userdata('res',$r2); // storing this array to session
}
echo '<pre>';
print_r($r2);       // printing the array


}

我得到的结果说第一次调用很好,但第二次调用我的值是覆盖会话显示Array([32323]=>23))如果我通过函数(32323,23)如果我通过了(33,42)Array([33]=>42)的旧值被破坏。

4

1 回答 1

0

您可能应该在 php 中使用常规会话,因为 codeigniter 中的会话仅允许您存储单个项目。像这样的东西:

$_SESSION['item'][] = $array;

或者,也许您仍然可以通过执行以下操作来使用 CodeIgniter 会话:

$items = $this->session->userdata('items');
array_push($items, $new_item);
$this->session->set_userdata('items', $items);

如果您还想定义键:

$items = $this->session->userdata('items');
$items['your_key'] = $new_item; 
$this->session->set_userdata('items', $items);
于 2012-07-07T06:42:30.687 回答