How to display data array in view in codeigniter?

Viewed 77

I'm developing a shopping cart with sessions. I have gotten the form data into the controller and created an array to pass into the session.

if($this->input->get()) {
        //get form data
        $pizza = $this->input->get('pizza');
        $size = $this->input->get('size');
        $toppings = $this->input->get('topping');
        $sides = $this->input->get('side');
        $amount = $this->input->get('amount');
         
       //get list of toppings
        if ($toppings) {
            foreach ($toppings as $topping) {
                $slug = $topping;
                $toppinglist = $this->toppingmodel->get_topping($slug);
            }
        }
        else{
            $toppinglist = NULL;
        }
        
      
       //get list of slides
        if ($sides) {
            foreach ($sides as $side) {
                $slug = $side;
                $sidelist = $this->sidemodel->get_side($slug);
            }
        }
        else{
            $sidelist = NULL;
        }
        

        //create order array
        $orderList = array(
            'pizza' => $pizza,
            'size' => $size,
            'toppings' => $toppinglist, 
            'sides' => $sidelist,
            'amount' => $amount
        );
       //add array to sessions
        if ($this->session->has_userdata('orderId')) {
            $cart = $this->session->userdata('orderList');
            array_push($cart, $orderList);
            $this->session->set_userdata('orderList', $cart);
        } else {
            $uniqueId = uniqid();
            $cart = array($orderList);
            $this->session->set_userdata('orderId', $uniqueId);
            $this->session->set_userdata('orderList', $orderList);

        }
    }

Then i'm passing that array into the data array to pass into the view.

$data['cartitems'] = $this->session->userdata('orderList');

This is a sample output array i'm getting when i use print_r. There are two orders in this array.

   Array
(
[cartitems] => Array
    (
        [0] => Array
            (
                [pizza] => Meat Delight
                [size] => 450.00
                [toppings] => Array
                    (
                        [id] => 2
                        [slug] => cheese
                        [toppingName] => Extra Cheese
                        [price] => 75.00
                    )

                [sides] => Array
                    (
                        [id] => 1
                        [sideName] => Chicken Wings
                        [price] => 400.00
                        [slug] => chicken-wing
                    )

                [amount] => 1
            )

        [1] => Array
            (
                [pizza] => Meat Delight
                [size] => 800.00
                [toppings] => Array
                    (
                        [id] => 3
                        [slug] => peppers
                        [toppingName] => Green Peppers
                        [price] => 60.00
                    )

                [sides] => Array
                    (
                        [id] => 1
                        [sideName] => Chicken Wings
                        [price] => 400.00
                        [slug] => chicken-wing
                    )

                [amount] => 1
            )

    )

)

How can i loop through this array in the view to display the cart? The data array is sent to the view by $this->load->view('pages/cart', $data); Is the way i'm creating the order array wrong? Thank you.

Edit: I am looping the array in view as,

        <?php foreach ($cartitems as $cartitem) : ?>
            <?php foreach ($cartitem['toppings'] as $topping) : ?>
            <h5>Item Name</h5>

            <p> <?php echo $cartitem['pizza']?> - Price = <?php echo $cartitem['size']?></p>
            <p> Toppings - <?php echo $topping['price'] ?> </p>

            <?php endforeach; ?>

        <?php endforeach; ?>

When i'm trying to access the toppings i am thrown an error saying Message: Illegal string offset 'price' Can anyone tell me why is that?

0 Answers
Related