Jump to content

Problems reading session array


Andy2024
Go to solution Solved by Andy2024,

Recommended Posts

I'm trying to create a shopping cart but am having trouble retrieving the results and my head is about to explode.

The array has been created because when i print_r or var_dump it i get

Array ( [ItemDesc] => JOULE 300L AERO COIL [ItemCode] => 118710 [ItemQty] => 1 [price] => 4764.54 [ItemTotalAmount] => 4764.54 )

but when i call the array

<table class="table table-striped">
								<?php
                if(!empty(Session::get("cart_item"))) {
                  foreach(Session::get("cart_item") as $row) {
                ?>
                  <tr>
                    <td width="70"><center><?php echo $row['ItemCode']; ?></center></td>
                    <td><?php echo $row['ItemDesc']; ?></td>
                    <td width="70"><center><?php echo $row['ItemQty']; ?></center></td>
                    <td width="70"><center><?php echo $row['ItemAmount']; ?></center></td>
                    <td width="70"><center><?php echo $row['ItemTotalAmount']; ?></center></td>
                  </tr>
                <?php
                  }
                } else {
                ?>
                  <tr>
                    <td colspan="5"><center><span class="rounded-pill p-1 px-5 bg-secondary">No Purchase Order Items Data Available!</span></center></td>
                  </tr>
                <?php } ?>
                </table>

I am getting  the following error

Error on Apr 22, 2024 22:25PM - Attempt to read property "ItemCode"

 

Does anyone have any suggestions please

I have also tried


                  <tr>
                    <td width="70"><center><?php echo $row->ItemCode; ?></center></td>
                    <td><?php echo $row->ItemDesc; ?></td>
                    <td width="70"><center><?php echo $row->ItemQty; ?></center></td>
                    <td width="70"><center><?php echo $row->ItemAmount; ?></center></td>
                    <td width="70"><center><?php echo $row->ItemTotalAmount; ?></center></td>
                  </tr>

 

But its throwing the exact same error

 

Link to comment
Share on other sites

Are you saying that if you

foreach(Session::get("cart_item") as $row) {
    print_r($row);
}
                

then you get...

 

22 minutes ago, Andy2024 said:
Array ( [ItemDesc] => JOULE 300L AERO COIL [ItemCode] => 118710 [ItemQty] => 1 [price] => 4764.54 [ItemTotalAmount] => 4764.54

for each item?

Link to comment
Share on other sites

I know its a different item because i cleared the session in case it was that but when i 

if(!empty(Session::get("cart_item"))) {
	print_r(Session::get("cart_item"));
	
	foreach(Session::get("cart_item") as $row) {
		print_r($row);
	}
}

for the session i'm getting

Array ( [ItemDesc] => PH FLEXI FRAME EXTENDER FRAME [ItemCode] => 370373 [ItemQty] => 1 [price] => 210.92 [ItemTotalAmount] => 210.92 )

but for the row i'm getting

PH FLEXI FRAME EXTENDER FRAME

So where are the other keys and values going?

Link to comment
Share on other sites

  • Solution

Thanks you so much, i couldn't see the wood for the trees

 

i'd put $cartItems = $itemArray; instead of $cartItems[] = $itemArray; before setting the session Session::set("cart_item", $cartItems);

Link to comment
Share on other sites

Your foreach() loop expects several items and that each will be put into $row as you loop through.

But get(cart_item) only returns a single item so when you use foreach() you are looping through its properties

Try

$row = Session::get("cart_item");
echo <<<ITEM
                  <tr>
                    <td width="70"><center>{$row['ItemCode']}</center></td>
                    <td>{$row['ItemDesc']}</td>
                    <td width="70"><center>{$row['ItemQty']}</center></td>
                    <td width="70"><center>{$row['ItemAmount']}</center></td>
                    <td width="70"><center>{$row['ItemTotalAmount']}</center></td>
                  </tr>
ITEM;

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.