473,811 Members | 2,538 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Removing Items from a shopping cart

13 New Member
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
Jan 19 '08 #1
8 2609
Atli
5,058 Recognized Expert Expert
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.
Jan 19 '08 #2
mtgriffiths86
13 New Member
Thank you for your response

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

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

hope this helps you to help me lol

Thanks again
Jan 20 '08 #3
Markus
6,050 Recognized Expert Expert
Could you also show what the 'removeFunction ' is doing, aswell, please?
Jan 20 '08 #4
mtgriffiths86
13 New Member
I hope this is correct and what you wanted.

[PHP] // Remove the item from the users cart
RemoveItem($car _id);
}
else
{
mysql_query("up date 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($d bServer, $dbUser, $dbPass, $dbName);

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

Thank you

Matthew
Jan 20 '08 #5
Atli
5,058 Recognized Expert Expert
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! :)
Jan 20 '08 #6
mtgriffiths86
13 New Member
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_ite m":
{
UpdateItem($_GE T["id"], $_GET["qty"]);
ShowCart();
break;
}
case "remove_ite m":
{
RemoveItem($_GE T["id"]);
ShowCart();
break;
}

case "remove_all_ite ms":
{
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($d bServer, $dbUser, $dbPass, $dbName);

mysql_query("DE LETE 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($d bServer, $dbUser, $dbPass, $dbName);

// Check if this item already exists in the users cart table
$result = mysql_query("se lect 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("i nsert 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($d bServer, $dbUser, $dbPass, $dbName);

if($qty == 0)
{
// Remove the item from the users cart
RemoveItem($car _id);
}
else
{
mysql_query("up date 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($d bServer, $dbUser, $dbPass, $dbName);

mysql_query("de lete 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($d bServer, $dbUser, $dbPass, $dbName);

$totalCost = 0;
$result = mysql_query("se lect * from cart inner join car_details on cart.car_id = car_details.car _id where cart.cookieId = '" . GetCartId() . "' order by car_details.Mod el 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="#0099F F">
<td><div align="center" class="style1">
<a href="Home.php" >Home</a> ----
<a href="Main.php" >Car Search</a>----
<a href="ShoppingC art.php">Shoppi ng Cart</a>
<span class="style4"> ---- </span>Contact</span></div></td>
</tr>
<tr bordercolor="#0 099FF">
<td><p align="center"> <script language="JavaS cript">

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

document.locati on.href = 'ShoppingCart.p hp?action=updat e_item&id='+ite mId+'&qty='+new Qty;
}

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

while($row = mysql_fetch_arr ay($result))
{
// Increment the total cost of all items
$totalCost += ($row["qty"] * $row["Price"]);
?>
<tr>
<td width="16%" height="25"><fo nt size="2" color="black">
<select name="<?php echo $row["car_id"]; ?>" onChange="Updat eQty(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"><fo nt size="2" color="black"> <?php echo $row["Manufactur er"]; ?> </font> </td>
<td width="16%" height="25"><fo nt size="2" color="black"> <?php echo $row["Model"]; ?></font> </td>
<td width="16%" height="25"><fo nt size="2" color="black"> <?php echo $row["Colour"]; ?></font> </td>
<td width="18%" height="25"><fo nt size="2" color="black"> £<?php echo number_format($ row["Price"], 2, ".", ","); ?> </font> </td>
<td width="13%" height="25"><fo nt size="2" color="black"> <a href="ShoppingC art.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"><fo nt size="2" color="black"> <a href="main.php" >&lt;&lt; Continue Searching</a></font></td>
<td width="16%" colspan="1"><di v align="center"> </div></td>
<td>&nbsp;</td>
<td><font size="2" color="black">< b> Total Cars: <?php echo "$sql_num_r ows" ?></b></font></td>
<td><font size="2" color="black">

<a href="ShoppingC art.php?action= remove_all_item s&id=&gt;1">Emp ty 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
Jan 20 '08 #7
Atli
5,058 Recognized Expert Expert
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.  
Jan 21 '08 #8
Atli
5,058 Recognized Expert Expert
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.
Jan 21 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2870
by: Don Grover | last post by:
I am retrieving costs and product id's from a sql db. and need to build a shopping cart around it. How do I store the selected items and qty req so I can move into another catalog and total up as im going. Just a couple of hints will do, Im familiar with vb script but not java based code , and im wondering how to store what they select so I can move around different product ranges. Don
2
3320
by: Paul Bruneau | last post by:
Hi, I hope someone can help me make a working shopping cart, as a learning tool. If I have a "Product Demo" html page with a "Buy Me" button, there must be a simple javascript method of storing the necessary product information. There could be several fields involved... Then there ought to be another javascript method that can create a new document based on the list a user has built selecting product from several
3
2927
by: help | last post by:
please i need help. I am doing a wed site online shopping system for my school project, which requires some asp coding using vb script. I am new to asp and have no knowledge whatsoever on how to code what i need. Please please please could someone please provide me with help, or coding on how to add item details into a basket record, once the user clicks the add to basket link. *** Sent via Developersdex http://www.developersdex.com...
1
1824
by: Jia Sun | last post by:
hello , everybody , i need a similar program , just like fancyimport.com if possible, pls contact me ,thank you very much . inchina@gmail.com
1
3233
by: Adil Akram | last post by:
I have created a site shopping cart in ASP.net. I am using ASP session object's SessionID on non SSL connection to track session. While adding products to cart DB I insert product and SessionID in table. All products and cart status pages are on non SSL connection. On checkout to get secure user information I shifted connection to SSL but when shifting to SSL, the SessionID changed (As is this is default behavior of IIS to prevent...
9
2591
by: Rob Meade | last post by:
Hi all, Ok - so I've got the array thing going on, and the session thing going on, and up until now its all been ok. I've got my view basket page which is displaying 3 rows (as an example) - I have an input box on each row enabling the user to update the quantity. If the enter a zero I need to remove the item from the array....
7
2645
by: David Lozzi | last post by:
Howdy, I have a shopping cart in my arraylist. Works great for adding items but I'm displaying the shopping cart in a gridview and the user can update the qty of the items. If they set it to 0, then I want to remove it. I'm doing the following script: For Each ele In bag Dim qty As Integer = CType(gvBag.Rows.Item(cnt).FindControl("txtQty"),
1
2108
by: Vahehoo | last post by:
Hi, I have an ASP .Net e-business site that is built using DNN 2.0. I am having troubles passing my shopping cart items to PayPal. I implemented a total paynow button, but it was not good enough for my customer. I found some PayPal ASP.Net controls to be used with .net studio. Basically, I want the user to be able to shop on my website, add items to my shopping cart and at the checkout to be taken to the PayPal website. I would like...
0
1534
by: Pratay | last post by:
Hi all, great forum - keep it up. The title of this post pretty much sums it up. I have a flash banner on my website with a button on it which says "view cart (0 items)". I would like this text to correctly reflect the number of items in my shopping cart. My website is here: www.kissdvdtemplates.com. The shopping cart system I'm using is Zen Cart. I have managed to self teach myself the basics of flash but this could be a bit tricky! ...
0
9730
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10651
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7671
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5555
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5693
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4341
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3868
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.