Skip to content
Advertisement

Printing a nested array on View | Codeigniter

I’m building a cart using sessions, I’m adding each pizza to an array and passing to the session. The array is as follows,

  [cart_items] => Array
        (
            [0] => Array
                (
                    [item_id] => 3
                    [item_name] => New York-Style Pizza
                    [price] => 800.00
                    [toppings] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 1
                                    [name] => Mozarella Cheese 
                                    [price] => 150.00
                                )

                            [1] => Array
                                (
                                    [id] => 7
                                    [name] => Chicken Bacon 
                                    [price] => 250.00
                                )

                        )

                    [toppings_price] => 0
                    [quantity] => 1
                    [price_per_item] => 800
                )

        )

I was able to display the item id, pizza name, price, and quantity But cannot figure out to display the topping names user has selected in the Toppings column.How should I do this>

View

The following code shows the way that I’m getting values from array,

<?php $list = $this->session->userdata('cart_items');
        if($list==Null) {
            print_r("There are no items in the your cart");
        }else{
            foreach ($list as $item=>$value){

                echo "<li class='list-group-item d-flex justify-content-between lh-condensed'>";
                print_r("<div>".$value['item_id']."</div>");
                print_r("<div>".$value['item_name']."</div>");
                print_r("<div>".$value['price']."</div>");
                $toppings_names=array($value['toppings']);
                foreach ($toppings_names as $name=>$val){
                print_r("<div>");
                print_r($val['name']);
                print_r("</div>");
                }
                print_r("<div>".$value['quantity']."</div>");
                echo "</li>";
            }
        }
        ?>

Output

The output that I’m expecting is as follows

Item ID |Item Name|Price|Toppings(selected topping names)|Quantity

The list is not displaying I’m getting the error “Undefined index: name”.

Advertisement

Answer

You must make a loop over topping since that is also array, so you can check for each item in it.

 <?php

$list = $this->session->userdata('cart_items');    //Array
if ($list == null) {
    print_r("There are no items in the your cart");
} else {
    foreach ($list as $item => $value) {
        echo "<li class='list-group-item d-flex justify-content-between lh-condensed'>";
        print_r("<div>".$value['item_id']."</div>");
        print_r("<div>".$value['item_name']."</div>");
        print_r("<div>".$value['price']."</div>");

        // toppings array is empty (nothing selected)
        if ($value['toppings'] == null) {
            print_r("<div>There is no toppings selected</div>");
        } else {
            foreach ($value['toppings'] as $val) {
                // here you can access indexes id, name, price in toppings array
                print_r("<div>");
                print_r($val['name']);
                print_r("</div>");
            }
        }
        
        print_r("<div>".$value['quantity']."</div>");
        echo "</li>";
    }
}

?>

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement