Jump to content

cart


ecabrera

Recommended Posts

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

$_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.

Link to comment
Share on other sites

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 />';
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>";

 

?>

Link to comment
Share on other sites

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>";

?>

Link to comment
Share on other sites

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;
}
}

Link to comment
Share on other sites

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;
}

}
?>

Link to comment
Share on other sites

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.

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.