jon1009 Posted February 2, 2017 Share Posted February 2, 2017 Hello All, Iam creating a shopping cart and basing the session content iam adding a add / remove to the product. First question is that a good away of doing it? Second would be Iam throwing a error if the $_session doesn't exist for both the session and the 'array_key_exists' Would best way to solve this be by wrapping it iin a if if($_SESSION["cart_products"]) {the code below} else {out put add button} $out = " <div class='row'> <form method='post' action='http://localhost/cart/'> <div class='medium-4 columns'> {$room->title} {$room->id} </div> <div class='medium-4 columns'> £{$room_cost_pp} p/p x {$room->room_spaces} guests = £{$room_cost} </div> <div class='medium-2 columns'> <input type='hidden' name='cabin_id' value='{$room->id}' /> <input type='hidden' name='cabin_name' value='{$room->title}'/> <input type='hidden' name='cabin_cost' value='{$room_cost}'/> <input type='hidden' name='return_url' value='{$current_url}' />"; $search_array = $_SESSION["cart_products"]; if (array_key_exists($room->id, $search_array)) { $out.="<input type='hidden' name='remove_code[]'' value='{$room->id}' /> <button type='submit' class='alert button'>Remove Cabin</button>"; }else { $out.="<input type='hidden' name='type' value='add' /> <button type='submit' class='success button'>Add Cabin</button>"; } Cheers Jon Quote Link to comment https://forums.phpfreaks.com/topic/303085-session-cart-advice/ Share on other sites More sharing options...
mac_gyver Posted February 2, 2017 Share Posted February 2, 2017 use isset() to (directly) test if the item is in the cart or not - if(isset($_SESSION["cart_products"][$room->id])) { Quote Link to comment https://forums.phpfreaks.com/topic/303085-session-cart-advice/#findComment-1542235 Share on other sites More sharing options...
jon1009 Posted February 3, 2017 Author Share Posted February 3, 2017 Iam using the follow but cant seem to get it to work. If i remove the isset() it works but throws a error once the session has be destroyed. <?php if(isset($_SESSION["cruise_id"]) == $page->id) { }else{ $_SESSION = ""; session_destroy() ; }?> Quote Link to comment https://forums.phpfreaks.com/topic/303085-session-cart-advice/#findComment-1542271 Share on other sites More sharing options...
jon1009 Posted February 3, 2017 Author Share Posted February 3, 2017 Scrap that Ive just looked into isset() and realised that its would only return true/false. So I went with the following that ok to use? <?php if(isset($_SESSION["cruise_id"])) { if($_SESSION["cruise_id"] != $page->id) { $_SESSION = ""; session_destroy() ; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/303085-session-cart-advice/#findComment-1542272 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.