Printing an array inside a SESSION array!

Hi, please help. It should be so simple but it isn’t…

public function cart() 
{
    // Get the id of the item from the URL
    $data_id = $this->uri->segment(3);
    // With the ID from the URL, reference the URL ID to PRODUCT ID in the database and retur result
    $query = $this->Welcome_model->Read_single($data_id);
    // Check to see if SESSION has been created already, if so add the item the the session, if not create the session and then add the item
    if (isset($_SESSION['cart'])) {
        // Session has been created
        echo "session already created";
        array_push($_SESSION['cart'], $query->result());
        echo '<pre>';
        print_r($_SESSION);
    } else {
        // Session hasn't been created
        echo "session not already created";
        $_SESSION['cart'] = array();
        array_push($_SESSION['cart'], $query->result());
        echo '<pre>';
        print_r($_SESSION);
    }        
}

Did you start the session somewhere with session_start?

Yeah, I can use this:

array_push($_SESSION[‘cart’], $query->result());

        print_r(array_values($_SESSION['cart']));

And I can see the values. I can also

        foreach ($_SESSION['cart'] as $key=>$value) {
            echo "{$key} => {$value} ";

        }

Where key prints 1,2,3,4,5 … but value just returns Array.

I guess my question is…what’s your question? I’m confused as to what you’re trying to do, you’ve set $_SESSION['cart'] to be an array, so $value is going to return an array…are you trying to get it to print the values?

Edit: OH…ok I think I get what you’re trying to do. You mean that it’s literally printing the word “Array”…not that it is an array right? Try:

        foreach ($_SESSION['cart'] as $key=>$value) {
            echo "{$key} => " . print_r($value);
        }

Cheers! I think I got this from here, thanks very much. But can you explain why it is like this, why the print_r?

Awesome! echo is designed to only display strings (and other types that can be converted to strings, like numbers). print_r (and also var_dump) are designed to display values in human readable form.

I’m not sure why they chose it to be that way though

Hey I am still have trouble, I want to print each individual value in a web page. But I am now having trouble getting to a specific value. Value is just printing the whole array.

I think I am doing it all wrong, there must be a simpler way.

Your cart is kind of a god method; it knows about URLs, it knows about loading data from the database, and it knows a lot about how the session works.

So let’s start with creating a proper Cart object which can be used to interact with the session, but that doesn’t need to know anything about the URL or the database.

class Cart
{
    public function add($productId)
    {
        if (!isset($_SESSION['cart'])) {
            $_SESSION['cart'] = array();
        }

        array_push($_SESSION['cart'], $productId);
    }

    pubic function getAllProducts()
    {
        return isset($_SESSION['cart']) ? $_SESSION['cart'] : [];
        // Or if you are on PHP 7+:
        // return $_SESSION['cart'] ?? [];
    }
}

Ideally the Cart doesn’t even know about $_SESSION, but for now this is a good first step :slight_smile:

This would simplify your cart function to

public function cart() 
{
    // Get the id of the item from the URL
    $data_id = $this->uri->segment(3);
    // With the ID from the URL, reference the URL ID to PRODUCT ID in the database and retur result
    $query = $this->Welcome_model->Read_single($data_id);
    // Add product to cart
    $this->cart->add($query->result()['id']); /// assuming the productId is in the id field

    echo '<pre>';
    print_r($cart->getAllProducts());
}

Does that help? :slight_smile:

2 Likes

Yes this helps because this is how I should be programming by created object. Thank you very much.

I did actually manage to do what I wanted by

foreach ($query as $row) {

echo $row[0]->id;

}

Or something like this, but then once I did it I then realized it isn’t what I need. My mistake was that in my flow charts I never designed the flow of the cart so I went in blind folded. Silly really.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.