ecabrera Posted January 14, 2012 Share Posted January 14, 2012 ok im making a shooping cart how do i make it so it remembers that items people add to the cart Quote Link to comment https://forums.phpfreaks.com/topic/255023-cart/ Share on other sites More sharing options...
herghost Posted January 14, 2012 Share Posted January 14, 2012 sessions Quote Link to comment https://forums.phpfreaks.com/topic/255023-cart/#findComment-1307639 Share on other sites More sharing options...
ecabrera Posted January 14, 2012 Author Share Posted January 14, 2012 ok Quote Link to comment https://forums.phpfreaks.com/topic/255023-cart/#findComment-1307642 Share on other sites More sharing options...
ecabrera Posted January 14, 2012 Author Share Posted January 14, 2012 where should i create a session for the cart i dont know how to do it or where to put it this is my cart page  <?php require ("scripts/connect.php"); if(isset($_GET['add'])){ $fd = $_GET['add']; $query = mysql_query("SELECT * FROM events WHERE id='$fd' LIMIT 1"); $row = mysql_fetch_assoc($query);  $eventid = $row["id"];  $eventname = $row["eventname"];  $eventtype = $row["typeevent"];  $eventinfo = $row["eventinfo"];  $date = $row["date"];  $time = $row["time"];  $livetickets = $row["sale"];  $numtickets = $row["numtickets"];  $pricestudents = $row["studentsprice"];  $priceseniors = $row["seniorsprice"];  $priceadults = $row["adultsprice"];  $venues = $row["venue"];  $categorys = $row["category"];  } ?> <?php $q = $_POST['howMany']; $howmany .= "<tr>"; $howmany .= "<td>".$eventname."</td>"; $howmany .= "<td>".$eventinfo."</td>"; $howmany .= "<td>".$pricestudents."</td>"; $howmany .= "<td>".$q."</td>"; $howmany .= "<td>".$q."</td>"; $howmany .= "<td>".X."</td>"; $howmany .= "</tr>"; ?> <center><h2>Shopping Cart</h2></center> <table width="100%" border="1" cellspacing="0" cellpadding="6">    <tr>     <td width="18%" bgcolor="#600"><strong>Product</strong></td>     <td width="45%" bgcolor="#600"><strong>Product Description</strong></td>     <td width="10%" bgcolor="#600"><strong>Unit Price</strong></td>     <td width="9%" bgcolor="#600"><strong>Quantity</strong></td>     <td width="9%" bgcolor="#600"><strong>Total</strong></td>     <td width="9%" bgcolor="#600"><strong>Remove</strong></td>    </tr>   <?php echo $howmany; ?>   <!-- <tr>     <td> </td>     <td> </td>     <td> </td>     <td> </td>     <td> </td>     <td> </td>    </tr> -->   </table>    </div> Quote Link to comment https://forums.phpfreaks.com/topic/255023-cart/#findComment-1307665 Share on other sites More sharing options...
ecabrera Posted January 15, 2012 Author Share Posted January 15, 2012 ok so add $_SESSION['cart_'.$_GET['add']]; but this doesnt keep the item in the cart when i go to add something else and come back that item it no there i include session_start(); already help anyone  <?php require ("scripts/connect.php"); if(isset($_GET['add'])){ $_SESSION['cart_'.$_GET['add']]; $fd = $_GET['add']; $query = mysql_query("SELECT * FROM events WHERE id='$fd' LIMIT 1"); $row = mysql_fetch_assoc($query);  $eventid = $row["id"];  $eventname = $row["eventname"];  $eventtype = $row["typeevent"];  $eventinfo = $row["eventinfo"];  $date = $row["date"];  $time = $row["time"];  $livetickets = $row["sale"];  $numtickets = $row["numtickets"];  $pricestudents = $row["studentsprice"];  $priceseniors = $row["seniorsprice"];  $priceadults = $row["adultsprice"];  $venues = $row["venue"];  $categorys = $row["category"];  } ?> <?php $q = $_POST['howMany']; $howmany .= "<tr>"; $howmany .= "<td>".$eventname."</td>"; $howmany .= "<td>".$eventinfo."</td>"; $howmany .= "<td>".$pricestudents."</td>"; $howmany .= "<td>".$q."</td>"; $howmany .= "<td>".$q."</td>"; $howmany .= "<td>".X."</td>"; $howmany .= "</tr>"; ?> <center><h2>Shopping Cart</h2></center> <table width="100%" border="1" cellspacing="0" cellpadding="6">    <tr>     <td width="18%" bgcolor="#600"><strong>Product</strong></td>     <td width="45%" bgcolor="#600"><strong>Product Description</strong></td>     <td width="10%" bgcolor="#600"><strong>Unit Price</strong></td>     <td width="9%" bgcolor="#600"><strong>Quantity</strong></td>     <td width="9%" bgcolor="#600"><strong>Total</strong></td>     <td width="9%" bgcolor="#600"><strong>Remove</strong></td>    </tr>   <?php echo $howmany; ?>   <!-- <tr>     <td> </td>     <td> </td>     <td> </td>     <td> </td>     <td> </td>     <td> </td>    </tr> -->   </table>    </div> Quote Link to comment https://forums.phpfreaks.com/topic/255023-cart/#findComment-1307723 Share on other sites More sharing options...
ecabrera Posted January 15, 2012 Author Share Posted January 15, 2012 how come it doesnt save my item Quote Link to comment https://forums.phpfreaks.com/topic/255023-cart/#findComment-1307771 Share on other sites More sharing options...
jonsjava Posted January 15, 2012 Share Posted January 15, 2012 you must have session_start() at the top of your page. Quote Link to comment https://forums.phpfreaks.com/topic/255023-cart/#findComment-1307774 Share on other sites More sharing options...
ecabrera Posted January 15, 2012 Author Share Posted January 15, 2012 i did its in my header.php Quote Link to comment https://forums.phpfreaks.com/topic/255023-cart/#findComment-1307775 Share on other sites More sharing options...
jonsjava Posted January 15, 2012 Share Posted January 15, 2012 does your header get called by your index? if so, you need it in your index, at the top. It must be called before anything else. Quote Link to comment https://forums.phpfreaks.com/topic/255023-cart/#findComment-1307776 Share on other sites More sharing options...
ecabrera Posted January 15, 2012 Author Share Posted January 15, 2012 yes thats where i have it but i dont know what to do eveytime i click add to cart it adds it but when i go to another page and come back its gone Quote Link to comment https://forums.phpfreaks.com/topic/255023-cart/#findComment-1307777 Share on other sites More sharing options...
jonsjava Posted January 15, 2012 Share Posted January 15, 2012 are you comfortable PMing me a link to your files (sanitized of any sensitive data), so I can review? Quote Link to comment https://forums.phpfreaks.com/topic/255023-cart/#findComment-1307779 Share on other sites More sharing options...
ecabrera Posted January 15, 2012 Author Share Posted January 15, 2012 yea sure Quote Link to comment https://forums.phpfreaks.com/topic/255023-cart/#findComment-1307780 Share on other sites More sharing options...
PFMaBiSmAd Posted January 15, 2012 Share Posted January 15, 2012 $_SESSION['cart_'.$_GET['add']];  Your session statement isn't assigning anything to a session variable. An assignment statement looks like -  $var = some_number;  or  $var = 'some_string';  or  $var = $some_other_var;  $var in the above can be any kind of php variable, such as a $_SESSION variable. Quote Link to comment https://forums.phpfreaks.com/topic/255023-cart/#findComment-1307784 Share on other sites More sharing options...
ecabrera Posted January 15, 2012 Author Share Posted January 15, 2012 ok so should i put  $cartitem = $_SESSION['cart_'].$_GET['add']]; Quote Link to comment https://forums.phpfreaks.com/topic/255023-cart/#findComment-1307786 Share on other sites More sharing options...
ecabrera Posted January 15, 2012 Author Share Posted January 15, 2012 the session variable does not want to be store Quote Link to comment https://forums.phpfreaks.com/topic/255023-cart/#findComment-1307792 Share on other sites More sharing options...
ecabrera Posted January 15, 2012 Author Share Posted January 15, 2012 ok so the the sessions is saveing but it wont stay on my cart page Quote Link to comment https://forums.phpfreaks.com/topic/255023-cart/#findComment-1307795 Share on other sites More sharing options...
Porl123 Posted January 15, 2012 Share Posted January 15, 2012 You need to save the variable to the session, not the other way round. Â $_SESSION['basket_items'] .= $_GET['item'] . ','; Â This is how I would perhaps do it. I'd save them all to this 'basket_items' session variable, appending each new item to the end of the list. I'd then use something like this to list the items: Â $items = explode(',', $_SESSION['basket_items']);Â Â // creates an array of basket items by splitting the list up by its commas foreach($items as $item) { echo $item . '<br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/255023-cart/#findComment-1307799 Share on other sites More sharing options...
ecabrera Posted January 15, 2012 Author Share Posted January 15, 2012 i get it but i would have no clue where to add it Quote Link to comment https://forums.phpfreaks.com/topic/255023-cart/#findComment-1307802 Share on other sites More sharing options...
Porl123 Posted January 15, 2012 Share Posted January 15, 2012 Well assuming you're using a GET to make the request, you'd put it within something like this: Â if(isset($_GET['item'])) { $_SESSION['basket_items'] .= $_GET['item'] . ','; } Â That's incredibly simplified. Realistically you would have a few validation checks on the item. The other bit of code I posted above would just go within the HTML element you're using to structure your basket. Quote Link to comment https://forums.phpfreaks.com/topic/255023-cart/#findComment-1307807 Share on other sites More sharing options...
ecabrera Posted January 15, 2012 Author Share Posted January 15, 2012 so like this  <?php  session_start();  require ("scripts/connect.php");  //make show that there is a id //if(isset($_GET['add'])){  //$fd = $_GET['add'];  if(isset($_GET['add'])) { $_SESSION['cart'] .= $_GET['add'] . ',';   // this sets variables in the session   //get hat id and set it to a variable //$fd = $_GET['add'];  //get all the data from thar variable $query = mysql_query("SELECT * FROM events WHERE id='$fd' LIMIT 1"); $row = mysql_fetch_assoc($query);   $eventid = $row["id"];  $eventname = $row["eventname"];  $eventtype = $row["typeevent"];  $eventinfo = $row["eventinfo"];  $date = $row["date"];  $time = $row["time"];  $livetickets = $row["sale"];  $numtickets = $row["numtickets"];  $pricestudents = $row["studentsprice"];  $priceseniors = $row["seniorsprice"];  $priceadults = $row["adultsprice"];  $venues = $row["venue"];  $categorys = $row["category"];  //$_SESSION['cart'] = $eventid;  }else   $noitem = "<h2>Your cart is empty</h2>"."<h3>To add an item to your cart, start by searching or browsing through our tabs. When you find something you like, click the 'Add to Cart' button and the item will be placed here until you check out.</h3><br>" ."<button type='button' class='cs'><a href='index'>Continue Shopping</a></button>";  ?> Quote Link to comment https://forums.phpfreaks.com/topic/255023-cart/#findComment-1307809 Share on other sites More sharing options...
ecabrera Posted January 15, 2012 Author Share Posted January 15, 2012 so like this  <?php session_start(); require ("scripts/connect.php"); //make show that there is a id //if(isset($_GET['add'])){ //$fd = $_GET['add']; if(isset($_GET['add'])) { $_SESSION['cart'] .= $_GET['add'] . ','; // this sets variables in the session //get hat id and set it to a variable //$fd = $_GET['add']; $noitem = ""; //get all the data from thar variable $query = mysql_query("SELECT * FROM events WHERE id='$fd' LIMIT 1"); $row = mysql_fetch_assoc($query);  $eventid = $row["id"];  $eventname = $row["eventname"];  $eventtype = $row["typeevent"];  $eventinfo = $row["eventinfo"];  $date = $row["date"];  $time = $row["time"];  $livetickets = $row["sale"];  $numtickets = $row["numtickets"];  $pricestudents = $row["studentsprice"];  $priceseniors = $row["seniorsprice"];  $priceadults = $row["adultsprice"];  $venues = $row["venue"];  $categorys = $row["category"]; //$_SESSION['cart'] = $eventid; }else   $noitem = "<h2>Your cart is empty</h2>"."<h3>To add an item to your cart, start by searching or browsing through our tabs. When you find something you like, click the 'Add to Cart' button and the item will be placed here until you check out.</h3><br>" ."<button type='button' class='cs'><a href='index'>Continue Shopping</a></button>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/255023-cart/#findComment-1307821 Share on other sites More sharing options...
PFMaBiSmAd Posted January 15, 2012 Share Posted January 15, 2012 You need to make your session variable an array where the array index is the item id number and the array value is the quantity of that item. By storing a string into the session variable, you need to have about 3-4 times more logic to manipulate it. Inserting an item (w/o duplicates), changing the quantity, and deleting it are all harder when it is stored as a string. Â To actually add an item (insert it if it is not in the cart or change the quantity if it is already in the cart), your logic would look like - Â <?php // code to 'add' an item to the cart if(isset($some_variable_that_indicates_add_to_cart_was_picked)){ $item_id = .... ; // your code that gets and validates the item id $quantity = .... ; // your code that gets and validates the quantity if(!isset($_SESSION[$item_id])){ // the item id is not already in the cart, insert it $_SESSION[$item_id] = $quantity; } else { // the item id is already in the cart, modify the quantity instead $_SESSION[$item_id] += $quantity; } } Quote Link to comment https://forums.phpfreaks.com/topic/255023-cart/#findComment-1307856 Share on other sites More sharing options...
ecabrera Posted January 15, 2012 Author Share Posted January 15, 2012 ok so i didnt all my cart code and add this  now how do i get it to show?  <?php if(isset($_GET['add'])){ $item_id = $_GET['add']; // your code that gets and validates the item id $quantity = $_GET['howMany']; // your code that gets and validates the quantity if(!isset($_SESSION[$item_id])){ // the item id is not already in the cart, insert it $_SESSION[$item_id] = $quantity; } else { // the item id is already in the cart, modify the quantity instead $_SESSION[$item_id] += $quantity; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/255023-cart/#findComment-1307886 Share on other sites More sharing options...
PFMaBiSmAd Posted January 15, 2012 Share Posted January 15, 2012 I notice that I left out an intended array index in the example code, but then again, that is what 'like' means. All the $_SESSION[$item_id] need to be $_SESSION['cart'][$item_id] so that all the cart related data is grouped together under a unique index name to distinguish it from any other possible session data. Quote Link to comment https://forums.phpfreaks.com/topic/255023-cart/#findComment-1307947 Share on other sites More sharing options...
ecabrera Posted January 15, 2012 Author Share Posted January 15, 2012 okm so i have what you said but it still does nothing Quote Link to comment https://forums.phpfreaks.com/topic/255023-cart/#findComment-1307949 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.