Welcome Guest, Not a member yet? Register   Sign In
Elegant Solution for Array Session - Ecommerce Basket
#1

[eluser]Yorkshire Pudding[/eluser]
Hi all,

I'm currently rebuilding one of my sites for the first time using Codeignitor.

My current basket code uses arrays saved in a session.

My old code looked like this
Code:
if (!isset($_SESSION['cart']))
    {
        $_SESSION['cart'] = array();
    }

    if (isset($_POST['productid']))
    {
        $_SESSION['cart'][] = $_POST['productid'];
        
    header('location: ' . $_SERVER['PHP_SELF'] . '?' . SID);
    exit();
    }

My new Codeignitor code looks like this:

Code:
if($this->input->post('product_id'))
            {
                if($this->session->userdata('cart'))
                {
                    if(!is_array($this->session->userdata('cart')))
                    {
                        $product_array = array();
                        $product_array[] = $this->session->userdata('cart');
                    }
                    else
                    {
                        $product_array = $this->session->userdata('cart');    
                    }
                    $product_array[] = $this->input->post('product_id');
                    $this->session->set_userdata('cart', $product_array);
                }
                else
                {
                    $this->session->set_userdata('cart', $this->input->post('product_id'));
                }    
            }

It works, but looks a bit bloated to me. Is there a more elegant way of doing this using Codeignitor?

Thanks for all you help in advance


Messages In This Thread
Elegant Solution for Array Session - Ecommerce Basket - by El Forum - 06-24-2008, 08:47 AM
Elegant Solution for Array Session - Ecommerce Basket - by El Forum - 06-24-2008, 09:44 AM
Elegant Solution for Array Session - Ecommerce Basket - by El Forum - 06-24-2008, 11:31 AM
Elegant Solution for Array Session - Ecommerce Basket - by El Forum - 06-24-2008, 11:53 AM
Elegant Solution for Array Session - Ecommerce Basket - by El Forum - 06-24-2008, 12:11 PM
Elegant Solution for Array Session - Ecommerce Basket - by El Forum - 06-24-2008, 03:42 PM
Elegant Solution for Array Session - Ecommerce Basket - by El Forum - 07-15-2008, 06:42 PM
Elegant Solution for Array Session - Ecommerce Basket - by El Forum - 07-16-2008, 12:19 PM
Elegant Solution for Array Session - Ecommerce Basket - by El Forum - 07-16-2008, 02:14 PM
Elegant Solution for Array Session - Ecommerce Basket - by El Forum - 07-16-2008, 07:57 PM



Theme © iAndrew 2016 - Forum software by © MyBB