Jump to content

Session-Based Shopping Cart


ballouta

Recommended Posts

Hi,

I have a good shopping cart session-based code, but it is missing a remove option for each item in the cart!

 

<?php
//this is the ADD to cart code, no problems
if ($_POST['add'])
{
    foreach ($_POST['qty'] as $k => $v)
    {
        // if the value is 0 or negative
        // don't bother changing the cart
        if ($v > 0)
        {
		//echo "V = $v and K = $k"; V = QTY and K = article
            $_SESSION['cart'][$k] = $_SESSION['cart'][$k] + $v;
        }
    }    
}
?>

 

and this is the update code:

<?php
if ($_POST['update'])
{
    foreach ($_POST['qty'] as $k => $v)
    {
        // if the value is empty, 0 or negative
        // don't bother changing the cart
        if ($v != "" && $v >= 0)
        {
            $_SESSION['cart'][$k] = $v;
        }
    }    
}

?>

 

I tried to 'imitate' the update abd clear code and did the following:

in the cart itself:

 

<?php
	<input type ='hidden' size=2 type=text name=\"qtyy[" . $k . "]\">
	<input type='submit' name='remove' value='Remove'>


//and I added those lines those

if ($_POST['remove'])
{
    foreach ($_POST['qtyy'] as $k => $v)
    {
echo "V==$v";
            $_SESSION['cart'][$k] = 0;
    }    
}
?>

 

The Shopping cart is of course didn;t work with my remove modification, could you please help me?

Please note that If i update any item qty in the cart to zero, it disappears.

 

Thank you so much

Link to comment
Share on other sites

hmm cant find anything wrong, are u getting any error messages?

if not maybe turn on error handling in your script.

 

 

can u catch the $_POST[remove] and $_POST[qtty] data at the start of the script?

might be worth while to check,try to do a

print_f($_POST);

and see if the form is working.

other than that i cant seem to find anything wrong, except for the key which is named qtyy instead of just qty.(think  that's on purpose)

Link to comment
Share on other sites

Hi The Little Guy

 

in this situation, what would be the code in teh cart form instead of those two lines?

 

<input type ='hidden' size=2 type=text name=\"qtyy[" . $k . "]\">

<input type='submit' name='remove' value='Remove'>

 

thank you

Link to comment
Share on other sites

I would use a check box.

 

So...

 

foreach($_SESSION['cart'] as $id){
     echo 'Remove: <input type="checkbox" name="itemid[]" value="'.$id.'" /><br />';
}
echo '<input type="submit" name="remove" value="Remove" />';

 

Then I actually would check like this:

 

if (isset($_POST['remove'])){
    foreach ($_POST['itemid'] as $id){
        if (in_array($id, $_SESSION['cart'])){
            unset($_SESSION['cart'][$id]);
        }
    }    
}

 

Give it a try and let me/us know how it works (you might have to modify/tweak it some)!

Link to comment
Share on other sites

I made this change in the code:

 

<input type='checkbox' name='itemid[]' value='$k' />

 

since my code that displays the cart begins with this:

<?php
if (is_array($_SESSION['cart']))
{
    foreach ($_SESSION['cart'] as $k => $v)
    {
        // only display items that have been selected
        // that is, quantities > 0
        if ($v > 0)
        {
//etc...

<input type='checkbox' name='itemid[]' value='$k' />

?>

 

i ticked the check box and pressed remove but still in the cart!

 

kindly help

Link to comment
Share on other sites

You may begin tracing the problem from here

 

This is my final code in the shopping cart.php form:

 

<?php
//it begins like this and there is no problem in viewing the cart content

if (is_array($_SESSION['cart']))
{
    foreach ($_SESSION['cart'] as $k => $v)
    {
  //$k is the item SKu which is the primary key/ item code

	<input type='submit' name='remove' value='Remove'>
	<input type='hidden' name='re' value='$k' />
//etc...
?>

 

if the above two lines are correct then I still need the loop the removes this item from the cart PLZ

 

Note that the add to cart code is working and it's like this:

<?php
if ($_POST['update'])
{
    foreach ($_POST['qty'] as $k => $v)
    {
        // if the value is empty, 0 or negative
        // don't bother changing the cart
        if ($v != "" && $v >= 0)
        {
            $_SESSION['cart'][$k] = $v;
        }
    }    
}
?>

 

could you please help me

Link to comment
Share on other sites

Hi

 

I do NOT have any problem in my form, what makes sure of that is that the update QTY and Empty Cart buttons are working properly.

 

          <form action='<?=$_SERVER['PHP_SELF']?>' method='post'>

 

Thank you

Link to comment
Share on other sites

I could be completely wrong with what I assumed you was doing/trying to do, however this code works for me.

 

<?php
session_start();

if ($_POST['update'])
{
	foreach ($_POST['qty'] as $k => $v)
	{
		// if the value is empty, 0 or negative
		// don't bother changing the cart
		if ($v != "" && $v >= 0)
		{
			$_SESSION['cart'][$k] = $v;
		}
	}    
}

//this is the ADD to cart code, no problems
if ($_POST['add'])
{
	foreach ($_POST['qty'] as $k => $v)
	{
		// if the value is 0 or negative
		// don't bother changing the cart
		if ($v > 0)
		{
			//echo "V = $v and K = $k"; V = QTY and K = article
			$_SESSION['cart'][$k] = $_SESSION['cart'][$k] + $v;
		}
	}    
}

if ($_POST['remove'])
{
	for( $i = 0; $i < count( $_POST['qty'] ); $i++ )
	{
		unset( $_SESSION['cart'][$_POST['qty'][$i]] );	
	}
}
?>
<form method="post">
    <input type="text" name="qty[1]" />
    <input type="submit" name="add" value="Add" /> 
</form>

<form method="post">
    <?php
if( isset( $_SESSION['cart'] ) )
	foreach( $_SESSION['cart'] as $k => $v )
		echo "<input type='text' name='qty[" . $k . "]' value='" . $v . "' />";	
?>
    <input type="submit" name="update" value="Update" /> 
</form>

<form method="post">
    <?php
if( isset( $_SESSION['cart'] ) )
	foreach( $_SESSION['cart'] as $k => $v )
		echo "<input type='checkbox' name='qty' value='" . $k . "' />";	
?>
    <input type="submit" name="remove" value="Remove" /> 
</form>

 

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.