Welcome Guest, Not a member yet? Register   Sign In
Pass multi-dimensional array to hidden form
#1

[eluser]rochellecanale[/eluser]
Hello I have a problem. The thing is I want to pass my information which is in my multidimensional array to the next page using hidden form. How can i create a loop that can store my information in an array to have their own hidden form. I tried also using sessions. But my problem in session it extract the last value in my array. Help me guys please.
#2

[eluser]Mischievous[/eluser]
Not exactly sure what your trying to attempt but you could json_encode() the multi-dimensional array and put it into an hidden input value then when next page submits json_decode() the value and you have your information. It would be more secure to put it in an encrypted session cookie however.
#3

[eluser]rochellecanale[/eluser]
Is json like jquery?
#4

[eluser]CroNiX[/eluser]
How are you trying to store the multidimensional array in session? Are you serializing it first?
#5

[eluser]rochellecanale[/eluser]
Yes i am serializing it but my problem is i only get the last values in my multidimensional array. Here is my sample code.

In my process page
Code:
$product_id = $chunks[0];   //get product id
                 $product_qty = $chunks[1]; //get qty
                 $product_code = $code; //get code
                 $product_name = $name;//get name
                 $product_desc = $desc; //get description
                 $product_price = $price; //get price
                 $product_amount = $product_price*$product_qty; //compute
                
                 //get selected product and assign to array
                 $transact = array(
                    $product_id => array(
                        'product_name' => $product_name,
                        'product_desc' => $product_desc,
                        'product_price' => $product_price,
                        'product_qty' => $product_qty,
                        'product_amount' => $product_amount,
                        'product_code' => $product_code
                    )
                 );
                        //assign array to session
                        foreach($transact as $value){
                            $serialize = serialize($value);
                            $this->session->set_userdata('product_list',$serialize);
                        }

In my result page
Code:
echo "<pre>";
  $unserialize = unserialize($this->session->userdata('product_list'));
  print_r($unserialize);

My other problem is the id in my array it does not viewed. What should i put in the index field?
#6

[eluser]CroNiX[/eluser]
In your loop, you are storing the array over and over using the same key. So in the end, only the last value from the array will be stored...

Just serialize $transact (no loop) and store that. Or better yet, just store the $product_id array (no need for the extra array layer there...)

Code:
$product_id => array(
  'product_name' => $product_name,
  'product_desc' => $product_desc,
  'product_price' => $product_price,
  'product_qty' => $product_qty,
  'product_amount' => $product_amount,
  'product_code' => $product_code
);

$this->session->set_userdata('product_list', serialize($product_id));
#7

[eluser]rochellecanale[/eluser]
It doesnt work also I end up to the last product maybe it is in my loop. By the way here's my full code about getting the selected products. Hope you can help me.

Code:
foreach($product_list as $product){
       foreach($cart_product->result() as $row){
        
            $code = $row->code;
            $id = $row->product_id;
            $name = $row->product_name;
            $desc = $row->product_desc;
            $price = $row->product_price;
            $chunks = explode('|',$product);
            $chunks1 = $chunks[0];
            
            if($chunks1 == $row->product_id){
                 $product_id = $chunks[0];
                 $product_qty = $chunks[1];
                 $product_code = $code;
                 $product_name = $name;
                 $product_desc = $desc;
                 $product_price = $price;
                 $product_amount = $product_price*$product_qty;
                
                
                 $product_id = array(
                        'product_name' => $product_name,
                        'product_desc' => $product_desc,
                        'product_price' => $product_price,
                        'product_qty' => $product_qty,
                        'product_amount' => $product_amount,
                        'product_code' => $product_code
                
                 );
                 $this->session->set_userdata('product_list',serialize($product_id));
                 //$serialize = serialize($transact);
                 //$this->session->set_userdata('product_list',$serialize);                    
          }
       }
      // calculate the subtotal
      $sub_total = $sub_total + $product_amount;

Do you think i should put the array outside my loops?


#8

[eluser]CroNiX[/eluser]
Well, you didn't show all your code the first time around so I didn't know about the other loop. The exact same problem exists in this loop now. You are storing the array over and over in session using the same key, product_list.

You're basically doing this in the loop.

Code:
$var['product_loop'] = 1;
$var['product_loop'] = 3;
$var['product_loop'] = 2;

What's $var['product_loop']? It's 2, the last thing you saved it as.

You want to be doing:
Code:
$var['product_loop'][] = 1;
$var['product_loop'][] = 3;
$var['product_loop'][] = 2;
Now what's the value of $var['product_loop']?

Code:
array(
  [0] => 1,
  [1] => 3,
  [2] => 2
)
and then store the serialized array $var outside of the loop. Also define $var = array(); before you start the loop.

This has nothing to do with sessions or storing data in sessions. It's how you are storing your array values constantly overwriting the previous value instead of adding it to an array.
#9

[eluser]rochellecanale[/eluser]
Thanks i will try it. Hope it will work. So i need to modify my $transact array like in the array you showed to me?
#10

[eluser]rochellecanale[/eluser]
Yes it actually works!! Thank You Very Much I replace my code to this:

Code:
$transact[$product_id]['product_name'] = $product_name;
  $transact[$product_id]['product_desc'] = $product_desc;
  $transact[$product_id]['product_price'] = $product_price;
  $transact[$product_id]['product_qty'] = $product_qty;
  $transact[$product_id]['product_amount'] = $product_amount;
  $transact[$product_id]['product_code'] = $product_code;




Theme © iAndrew 2016 - Forum software by © MyBB