Welcome Guest, Not a member yet? Register   Sign In
CI Session Array Problem
#1

[eluser]mohsin917[/eluser]
Hi.
This is the thing that i do in simple code. Every time when this code runs it create new index of basket in session array and when i want to get data i just do it with simple loop.
Now i want to do it in CI using session class

Code:
$product_array = array(
                       "id"            => $id,
                       "pName"         => $_POST['pName'],
                       "pImage"     => $_POST['pImage'],
                       "pCode"         => $_POST['pCode'],
                       "price1"        => $_POST['price1'],
                       "price2"        => $_POST['price2'],
                       "price3"        => $_POST['price3'],
                       "price4"     => $_POST['price4'],
                       "quantity"    => 1
                       );
    $_SESSION['basket'][] = $product_array;

simply array is working in CI Session class. But can't add the session auto incremented array. Even i also try to do this.

Code:
$basket[] = array(
                       "id"            => '1',
                       "pName"         => "name",
                       "quantity"    => 1
                       );
                      
        $this->session->set_userdata($basket);

but not helpful.

Any idea??
#2

[eluser]nggakbiasa[/eluser]
try :
Code:
$basket = array("basket" =>
                                array(
                                     array(
                                                "id"            => '1',
                                               "pName"         => "name",
                                               "quantity"    => 1
                                               ),
                      
                                    array(
                                               "id"            => '2',
                                               "pName"         => "name",
                                               "quantity"    => 1
                                               )
                                            
                                    )
                      
                       );
                      
    $this->session->set_userdata($basket);
#3

[eluser]mohsin917[/eluser]
No its not working. Problem is not assigning the values to array. Problem is how to automatically increase the index value of array:
Like:

Code:
for($x = 0; $x < 10; $x++)
{
    $ary[] = $x;
}

now the $ary has 10 indexes with different values. How to implement it into the session class of CI..

If there is no solution I have to use the simple code that i described already Sad
#4

[eluser]nggakbiasa[/eluser]
sorry, i don't get what you mean. This is the output when i do "print_r" on $this->session->userdata
Code:
[basket] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [pName] => name
                    [quantity] => 1
                )

            [1] => Array
                (
                    [id] => 2
                    [pName] => name
                    [quantity] => 1
                )

        )

to add more i do :
Code:
$basket['basket'] []=array(
                            "id"            => 'this is new',
                            "pName"         => "oke",
                            "quantity"    => 1
                            );

is it something like that ?
#5

[eluser]mohsin917[/eluser]
Well very close suggestion. Appreciate for help. but not working as i am expecting. The new working code is
Code:
$basket['basket'][0] = array(
                            "id"            => 'this is new',
                            "pName"         => "oke",
                            "quantity"    => 1
                            );
        $basket['basket'][1] = array(
                            "id"            => '1',
                            "pName"         => "asdf",
                            "quantity"    => 3
                            );  
        
        $this->session->set_userdata($basket);
        
        $x = $this->session->userdata('basket');
        
        foreach($x as $r)
        {
            foreach($r as $r2)
            {
                echo $r2.'<br />';
            }
        }

Problem is i have to check how many indexes of $basket['basket'][] is registered and when control goes through the above code again then make 1+.

Actually $basket['basket'][] this should add another index automatically. Don't understand why it isn't.
#6

[eluser]smilie[/eluser]
Hm, I could be completely wrong on this one as I do not grasp the array's fully myself - but;

Is it possible that your code 'overwrites' the array? Meaning - when you store new array values, the old ones get replaced?

Could you try:

Code:
# New basket array:
$new_basket = array(
                            "id"            => 'this is new',
                            "pName"         => "oke",
                            "quantity"    => 1
                            );

# Existing one is $basket['basket'];
# Then - merge:
$basket = array_merge($basket,$new_basket);

# This should get them both in same $basket array
# In case that $basket array does not yet exist, do
if(!isset($basket['basket']))
{
   $basket['basket'] = array();
}
else
{
   # code from above
}

$this->session->set_userdata($basket);
$x = $this->session->userdata('basket');
foreach($x as $r)
{
   echo $r2.'<br />';
}

Cheers,
Smilie




Theme © iAndrew 2016 - Forum software by © MyBB