marie
marie

Reputation: 1

codeigniter array

How can I grab this data and increment it in Codeigniter?

$_SESSION['cart'][$_GET[id]]++;

Upvotes: 0

Views: 311

Answers (5)

Pratik Soni
Pratik Soni

Reputation: 2588

You can do in this way.

By passing variable in your controller function, Your controller function will look like this

function my_function($id='')
{
    //Your code goes here
    $my_cart = $this->session->userdata('cart');
    $my_data = $my_cart[$id];

}

Upvotes: 0

Phil Sturgeon
Phil Sturgeon

Reputation: 30766

$this->input->get() is no longer messed with, so GET away.

Upvotes: 0

Gerardo Jaramillo
Gerardo Jaramillo

Reputation: 485

maybe like this...

$cart = $this->session->userdata('cart'); $cart[$this->uri->segment(3)];

Upvotes: 0

benembery
benembery

Reputation: 676

It's frowned upon, but if you really want to use the $_GET var you can always do the following:

parse_str($_SERVER['QUERY_STRING'],$_GET); 

I would stick with using URI segments as shown by Ross or have the 'id' supplied as a parameter in the controller function.

Upvotes: 0

Ross
Ross

Reputation: 17967

because CI destroys the $_GET array, you can do this

$_SESSION['cart'][$this->uri->segment(3)]++;

where 3 is the URL segment of the ID. But I would look in to the shopping cart class as recommended by Malachi.

from the docs ~

$data = array(
               'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
               'qty'   => 3
            );

$this->cart->update($data); 

Upvotes: 3

Related Questions