Removing Items from a shopping cart | Newbie | | Join Date: Jan 2008
Posts: 13
| |
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: -
<a href="ShoppingCart.php?action=remove_item&id=<?php echo $row["car_id"]; ?>">Remove</a>
-
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: -
case "remove_item":
-
{
-
RemoveItem($_GET["id"]);
-
ShowCart();
-
break;
-
}
-
Can anyone help
Thanks in advance
Matthew
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,748
| | | 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
| | | 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
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,940
| | | 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
| | | 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
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,748
| | | 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: -
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());
-
}
-
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
| | | 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"> <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"><< Continue Searching</a></font></td>
<td width="16%" colspan="1"><div align="center"></div></td>
<td> </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= >1">Empty Cart</a></font></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td><font size="2" color="black"><b> Total Quantity: <?php echo "$qty" ?></b></font></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td><font size="2" color="black"><b>Total: £<?php echo number_format($totalCost, 2, ".", ","); ?></b></font></td>
</tr>
</table>
</form>
</body>
</html>
<?php
}
?>
[/PHP]
Thanks again
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,748
| | | 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: -
mysql_query("DELETE FROM cart WHERE cookieId = '" . GetCartId());
-
It's missing the closing ' quote after the cart id.
Should be: -
mysql_query("DELETE FROM cart WHERE cookieId = '" . GetCartId() ."'");
-
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,748
| | | 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: -
$var = "Hello";
-
-
function PrintVar() {
-
global $var;
-
echo $var;
-
}
-
This prints "Hello" when the function is called.
This may be easier if you plan on using the variable in many functions: -
define("VAR", "Hello");
-
-
function PrintVar() {
-
echo VAR;
-
}
-
You will do exactly the same thing as the first code, but without having to import the variables into the function scope.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|