Connecting Tech Pros Worldwide Forums | Help | Site Map

Removing Items from a shopping cart

Newbie
 
Join Date: Jan 2008
Posts: 13
#1: Jan 19 '08
Hi All,

I am trying to remove all items from my shopping cart but am having problems.

I can remove individual items by using clicking a link which runs this piece of code:
Expand|Select|Wrap|Line Numbers
  1. <a href="ShoppingCart.php?action=remove_item&id=<?php echo $row["car_id"]; ?>">Remove</a>
  2.  
My problem is that i want to include a link that when clicked will clear all items and not just specific ones that are chosen by the car_id

remove item code is below:
Expand|Select|Wrap|Line Numbers
  1. case "remove_item":
  2. {
  3.     RemoveItem($_GET["id"]);
  4.     ShowCart();
  5.     break;
  6. }
  7.  
Can anyone help

Thanks in advance

Matthew

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,748
#2: Jan 19 '08

re: Removing Items from a shopping cart


Hi Matthew.

The easiest way would probably be to get a list of all items in the chart and run them all through your RemoveItem function.
Depending on what that function does exactly, this may not be the most efficient way tho.

If you tell us how the items in the chart are stored we may be able to give a more specific answer.
Newbie
 
Join Date: Jan 2008
Posts: 13
#3: Jan 20 '08

re: Removing Items from a shopping cart


Thank you for your response

I think that the code that you are looking for is this:

[PHP]@mysql_query("insert into cart(cookieId, car_id, qty) values('" . GetCartId() . "', $car_id, $qty)");[/PHP]

hope this helps you to help me lol

Thanks again
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,940
#4: Jan 20 '08

re: Removing Items from a shopping cart


Could you also show what the 'removeFunction' is doing, aswell, please?
Newbie
 
Join Date: Jan 2008
Posts: 13
#5: Jan 20 '08

re: Removing Items from a shopping cart


I hope this is correct and what you wanted.

[PHP] // Remove the item from the users cart
RemoveItem($car_id);
}
else
{
mysql_query("update cart set qty = $qty where cookieId = '" . GetCartId() . "' and car_id = $car_id");
}
}

function RemoveItem($car_id)
{
// Uses an SQL delete statement to remove an item from
// the users cart

global $dbServer, $dbUser, $dbPass, $dbName;

// Get a connection to the database
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);

mysql_query("delete from cart where cookieId = '" . GetCartId() . "' and car_id = $car_id");
[/PHP]

Thank you

Matthew
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,748
#6: Jan 20 '08

re: Removing Items from a shopping cart


Based on that code, you could probably make a function to delete all cart items at once.

It would only require minor changes to the code you posted.
Something like:
Expand|Select|Wrap|Line Numbers
  1. function RemoveAllItems()
  2. {
  3.     // Uses an SQL delete statement to remove ALL items from
  4.     // the users cart
  5.  
  6.     global $dbServer, $dbUser, $dbPass, $dbName;
  7.  
  8.     // Get a connection to the database
  9.     $cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
  10.  
  11.     mysql_query("DELETE  FROM cart WHERE cookieId = '" . GetCartId());
  12. }
  13.  
Before you test this!
Keep in mind that I do not really know the program you are using so this may not be exactly what you need.
Make sure to test this on data you won't miss if something goes wrong and it all gets messed up! :)
Newbie
 
Join Date: Jan 2008
Posts: 13
#7: Jan 20 '08

re: Removing Items from a shopping cart


Thanks for the post.

I have tried creating a new function as stated but have no joy.

This maybe pointless but i am going to include all of the code that i am using. Maybe that would help.

[PHP]

<?php

include("db.php");

switch($_GET["action"])
{
case "add_item":
{
AddItem($_GET["id"], $_GET["qty"]);
ShowCart();
break;
}
case "update_item":
{
UpdateItem($_GET["id"], $_GET["qty"]);
ShowCart();
break;
}
case "remove_item":
{
RemoveItem($_GET["id"]);
ShowCart();
break;
}

case "remove_all_items":
{
RemoveAllItems();
ShowCart();
break;
}

default:
{
ShowCart();
}

}


function RemoveAllItems()

{
// Uses an SQL delete statement to remove ALL items from

// the users cart
global $dbServer, $dbUser, $dbPass, $dbName;
// Get a connection to the database

$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);

mysql_query("DELETE FROM cart WHERE cookieId = '" . GetCartId());

}

function AddItem($car_id, $qty)
{
// Will check whether or not this item
// already exists in the cart table.
// If it does, the UpdateItem function
// will be called instead

global $dbServer, $dbUser, $dbPass, $dbName;

// Get a connection to the database
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);

// Check if this item already exists in the users cart table
$result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and car_id = $car_id");
$row = mysql_fetch_row($result);
$numRows = $row[0];

if($numRows == 0)
{
// This item doesn't exist in the users cart,
// we will add it with an insert query

@mysql_query("insert into cart(cookieId, car_id, qty) values('" . GetCartId() . "', $car_id, $qty)");
}
else
{
// This item already exists in the users cart,
// we will update it instead

UpdateItem($car_id, $qty);
}
}

function UpdateItem($car_id, $qty)
{
// Updates the quantity of an item in the users cart.
// If the qutnaity is zero, then RemoveItem will be
// called instead

global $dbServer, $dbUser, $dbPass, $dbName;

// Get a connection to the database
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);

if($qty == 0)
{
// Remove the item from the users cart
RemoveItem($car_id);
}
else
{
mysql_query("update cart set qty = $qty where cookieId = '" . GetCartId() . "' and car_id = $car_id");
}
}

function RemoveItem($car_id)
{
// Uses an SQL delete statement to remove an item from
// the users cart

global $dbServer, $dbUser, $dbPass, $dbName;

// Get a connection to the database
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);

mysql_query("delete from cart where cookieId = '" . GetCartId() . "' and car_id = $car_id");
}

function ShowCart()
{
// Gets each item from the cart table and display them in
// a tabulated format, as well as a final total for the cart

global $dbServer, $dbUser, $dbPass, $dbName;

// Get a connection to the database
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);

$totalCost = 0;
$result = mysql_query("select * from cart inner join car_details on cart.car_id = car_details.car_id where cart.cookieId = '" . GetCartId() . "' order by car_details.Model asc");
$sql_num_rows = mysql_num_rows($result);

?>
<td height="68"><p align="center" class="style1"><font size="+3"><img src="Banner.jpg"></font></p> </td>
</tr>
<tr BGCOLOR="#0099FF">
<td><div align="center" class="style1">
<a href="Home.php">Home</a> ----
<a href="Main.php">Car Search</a>----
<a href="ShoppingCart.php">Shopping Cart</a>
<span class="style4"> ---- </span>Contact</span></div></td>
</tr>
<tr bordercolor="#0099FF">
<td><p align="center"><script language="JavaScript">

function UpdateQty(item)
{
itemId = item.name;
newQty = item.options[item.selectedIndex].text;

document.location.href = 'ShoppingCart.php?action=update_item&id='+itemId+' &qty='+newQty;
}

</script>
<body bgcolor="#ffffff"><form method="get" name="frmCart" class="style2">
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="16%" height="25" bgcolor="#0099FF"><font size="2" color="white"> &nbsp;&nbsp;<b>Qty</b> </font> </td>
<td width="21%" height="25" bgcolor="#0099FF"><font size="2" color="white"> <b>Manufacturer</b> </font> </td>
<td width="16%" height="25" bgcolor="#0099FF"><font size="2" color="white"> <b>Model</b> </font> </td>
<td width="16%" height="25" bgcolor="#0099FF"><font size="2" color="white"> <b>Colour</b> </font> </td>
<td width="18%" height="25" bgcolor="#0099FF"><font size="2" color="white"> <b>Price</b> </font> </td>
<td width="13%" height="25" bgcolor="#0099FF"><font size="2" color="white"> <b>Remove?</b> </font> </td>
</tr>
<?php

while($row = mysql_fetch_array($result))
{
// Increment the total cost of all items
$totalCost += ($row["qty"] * $row["Price"]);
?>
<tr>
<td width="16%" height="25"><font size="2" color="black">
<select name="<?php echo $row["car_id"]; ?>" onChange="UpdateQty(this)">
<?php

for($i = 1; $i <= 5; $i++)
{
echo "<option ";
if($row["qty"] == $i)
{
echo " SELECTED ";
}
echo ">" . $i . "</option>";
}


$qty += $row["qty"]

?>
</select>
</font> </td>
<td width="21%" height="25"><font size="2" color="black"> <?php echo $row["Manufacturer"]; ?> </font> </td>
<td width="16%" height="25"><font size="2" color="black"> <?php echo $row["Model"]; ?></font> </td>
<td width="16%" height="25"><font size="2" color="black"> <?php echo $row["Colour"]; ?></font> </td>
<td width="18%" height="25"><font size="2" color="black"> £<?php echo number_format($row["Price"], 2, ".", ","); ?> </font> </td>
<td width="13%" height="25"><font size="2" color="black"> <a href="ShoppingCart.php?action=remove_item&id=<?php echo $row["car_id"]; ?>">Remove</a></font> </td>

<?php
}

// Display the total
?>
<tr>
<td colspan="6"><hr size="1" color="#0099FF" noshade> </td>
</tr>
<tr>
<td colspan="2"><font size="2" color="black"> <a href="main.php">&lt;&lt; Continue Searching</a></font></td>
<td width="16%" colspan="1"><div align="center"></div></td>
<td>&nbsp;</td>
<td><font size="2" color="black"><b> Total Cars: <?php echo "$sql_num_rows" ?></b></font></td>
<td><font size="2" color="black">

<a href="ShoppingCart.php?action=remove_all_items&id= &gt;1">Empty Cart</a></font></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><font size="2" color="black"><b> Total Quantity: <?php echo "$qty" ?></b></font></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><font size="2" color="black"><b>Total: &pound;<?php echo number_format($totalCost, 2, ".", ","); ?></b></font></td>
</tr>
</table>
</form>
</body>
</html>
<?php
}

?>
[/PHP]

Thanks again
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,748
#8: Jan 21 '08

re: Removing Items from a shopping cart


Are you getting any errors when you try the RemoveAllItems function?

I just noticed a small typo in the Query:
Expand|Select|Wrap|Line Numbers
  1. mysql_query("DELETE  FROM cart WHERE cookieId = '" . GetCartId());
  2.  
It's missing the closing ' quote after the cart id.

Should be:
Expand|Select|Wrap|Line Numbers
  1. mysql_query("DELETE  FROM cart WHERE cookieId = '" . GetCartId() ."'");
  2.  
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,748
#9: Jan 21 '08

re: Removing Items from a shopping cart


Also, want to add a small suggestion...

You use the global keyword to import the database variables into your functions scope.

You could declare them as constants instead so you wouldn't have to import them into every function.

For example, this code uses global as you do:
Expand|Select|Wrap|Line Numbers
  1. $var = "Hello";
  2.  
  3. function PrintVar() {
  4.   global $var;
  5.   echo $var;
  6. }
  7.  
This prints "Hello" when the function is called.

This may be easier if you plan on using the variable in many functions:
Expand|Select|Wrap|Line Numbers
  1. define("VAR", "Hello");
  2.  
  3. function PrintVar() {
  4.   echo VAR;
  5. }
  6.  
You will do exactly the same thing as the first code, but without having to import the variables into the function scope.
Reply