Jump to content

Session cart advice.


jon1009

Recommended Posts

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

Link to comment
Share on other sites

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() ;
}?>
Link to comment
Share on other sites

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() ;
}
} ?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.