Welcome Guest, Not a member yet? Register   Sign In
how to read all session_variables with a for
#1

[eluser]dexter21[/eluser]
hi,


i have several productos with session vars:

echo $this->session->set_userdata('productid','2323232');
echo $this->session->set_userdata('description','test1');

echo $this->session->set_userdata('productid','232');
echo $this->session->set_userdata('description','text2 ');


echo $this->session->set_userdata('productid','23111');
echo $this->session->set_userdata('description','test4');


now i want to recover all products_id with a for. How could i read all session variables of the same type with a for ?


i tried:


for ($a=0;$a<sizeof($this->session->userdata);$a++)
{
echo $this->session->userdata('product_id',$a)."<br>";


}

any ideas? many thanks
#2

[eluser]xwero[/eluser]
If you do it like that your values are overwritten because the userdata is a one dimensional array.
You can add an array that has the productid key and then you can do
Code:
$this->session->set_userdata(’productid’,array(2323232));
$this->session->set_userdata(’productid’,array_merge(array(232),$this->session->userdata(’productid’)));
To get all productids you just have to do $this->session->userdata(’productid’)
#3

[eluser]webthink[/eluser]
With the code as you've written it there isn't going to be an array of products. There will only be one userdata variable called productid and one called description containing the very last value you've set (in your case 23111 and test4 respectively).

If you want to store an array in the session you'll have to do something like this.

Code:
$products = array('232323'=>'test1', '232'=>'test2', '23111'=>'test4');
$this->session->set_userdata(’products’,serialize($products));

and then to access the products array...
Code:
$products = unserialize($this->session->userdata(’products’));
foreach ($products as $id=>$description)
{
  echo "ID:".$id." DESC:".$description."<br>";
}




Theme © iAndrew 2016 - Forum software by © MyBB