Andy2024 Posted April 22 Share Posted April 22 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 Quote Link to comment https://forums.phpfreaks.com/topic/320088-problems-reading-session-array/ Share on other sites More sharing options...
Barand Posted April 22 Share Posted April 22 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? Quote Link to comment https://forums.phpfreaks.com/topic/320088-problems-reading-session-array/#findComment-1622135 Share on other sites More sharing options...
Andy2024 Posted April 22 Author Share Posted April 22 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? Quote Link to comment https://forums.phpfreaks.com/topic/320088-problems-reading-session-array/#findComment-1622142 Share on other sites More sharing options...
Andy2024 Posted April 22 Author Share Posted April 22 Sorry am getting PH FLEXI FRAME EXTENDER FRAME 370373 1 210.92 210.92 So why is it putting everything on a new row! Quote Link to comment https://forums.phpfreaks.com/topic/320088-problems-reading-session-array/#findComment-1622144 Share on other sites More sharing options...
Solution Andy2024 Posted April 22 Author Solution Share Posted April 22 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); Quote Link to comment https://forums.phpfreaks.com/topic/320088-problems-reading-session-array/#findComment-1622151 Share on other sites More sharing options...
Barand Posted April 22 Share Posted April 22 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; Quote Link to comment https://forums.phpfreaks.com/topic/320088-problems-reading-session-array/#findComment-1622152 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.