General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 28th, 2004, 01:36 PM
aloo aloo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 26 aloo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
shopping cart problem with quantity

hi

im working on the shopping cart tutorial on this site. ive got a problem with the quantity.

what happens when i add an item from product.php, it increases the quantity.
but when i try to change the quantity from cart.php using the combo box, this doesnt work.

i just cannot work out where im going wrong.

* Please enclose your code within the appropriate [ code ] or [ php ] tags *

PHP Code:
?php
include("db.php");
switch(
$_GET["action"])
{
case 
"add_item":
{
AddItem($_GET["id"], $_GET["qty"]);
trolley();
break;
}
case 
"update_item":
{
UpdateItem($_GET["id"], $_GET["qty"]);
trolley();
break;
}
case 
"remove_item":
{
RemoveItem($_GET["id"]);
trolley();
break;
}
default:
{
ShowCart();
}
}
function 
AddItem($itemId$qty)
{
global 
$dbServer$dbUser$dbPass$dbName;
$cxn = @ConnectToDb($dbServer$dbUser$dbPass$dbName);

$result mysql_query("select count(*) from cart where cookieId = '" GetCartID() . "' and itemId = $itemId");
$row mysql_fetch_row($result); (line 38)
$numRows $row[0];

if(
$numRows == 0)
{

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

UpdateItem($itemId$qty);
}
}

function 
UpdateItem($itemId$qty)
{

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($itemId);
}
else
{
mysql_query("update cart set qty = qty + $qty where cookieId = '" GetCartId() . "' and itemId = $itemId");
}
}

function 
RemoveItem($itemId)
{

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 itemId = $itemId");
}

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 items on cart.itemId = item.itemId where cart.cookieId= '" GetCartId() . "' order by item.name asc");
?>
<html>
<head>
<title> Your Shopping Cart </title>
<script language="JavaScript">

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

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

</script>
</head>
<body bgcolor="#ffffff">
<h1>Your Shopping Cart</h1>
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="15%" height="25" bgcolor="red">
<font face="verdana" size="1" color="white">
 <b>Qty</b>
</font>
</td>
<td width="55%" height="25" bgcolor="red">
<font face="verdana" size="1" color="white">
<b>Product</b>
</font>
</td>
<td width="20%" height="25" bgcolor="red">
<font face="verdana" size="1" color="white">
<b>Price Each</b>
</font>
</td>
<td width="10%" height="25" bgcolor="red">
<font face="verdana" size="1" 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="15%" height="25">
<font face="verdana" size="1" color="black">
<select name="<?php echo $row["itemId"]; ?>" onChange="UpdateQty(this)">
<?php
                        
for($i 1$i <= 5$i++)
{
echo 
"<option ";
if(
$row["qty"] == $i)
{
echo 
" SELECTED ";
}
echo 
">" $i "</option>";
}
?>
</select>
</font>
</td> 

Reply With Quote
  #2  
Old January 28th, 2004, 02:11 PM
Bruski Bruski is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Toronto, Canada
Posts: 192 Bruski User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
what line is the erorr so we can better assist u

Reply With Quote
  #3  
Old January 28th, 2004, 02:14 PM
aloo aloo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 26 aloo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
thats the thing i just can't spot any errors.

Reply With Quote
  #4  
Old January 28th, 2004, 02:30 PM
Bruski Bruski is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Toronto, Canada
Posts: 192 Bruski User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
what is it doing that its not supposed to be doing

Reply With Quote
  #5  
Old January 28th, 2004, 02:43 PM
aloo aloo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 26 aloo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
what happens
when i add an item from product.php, for example if i add item1 twice it will show a quanty of 2 for that item in my shopping cart

but when i try to change the quantity the quantity from my shopping cart.php using the combo box, this doesnt work the quantity will remain at 2.

Reply With Quote
  #6  
Old January 28th, 2004, 02:46 PM
Bruski Bruski is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Toronto, Canada
Posts: 192 Bruski User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
how are u storing the items that the person chose? or the quantity of the items?

Reply With Quote
  #7  
Old January 28th, 2004, 02:56 PM
aloo aloo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 26 aloo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
ive got 2 tables a cart table and a items table

im trying to update the quantity buy using the query

mysql_query("update cart set qty = qty + $qty where cookieId = '" . GetCartId() . "' and itemId = $itemId");

Reply With Quote
  #8  
Old January 28th, 2004, 03:05 PM
Bruski Bruski is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Toronto, Canada
Posts: 192 Bruski User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
why are you doing qty + $qty, why dont u just set qty to the value in drop down menu

Reply With Quote
  #9  
Old January 28th, 2004, 03:07 PM
aloo aloo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 26 aloo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
how do i do that, im still quiet new to php.

Reply With Quote
  #10  
Old January 28th, 2004, 03:14 PM
Bruski Bruski is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Toronto, Canada
Posts: 192 Bruski User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
try changing the qty + $qty with just $qty

Reply With Quote
  #11  
Old January 28th, 2004, 03:20 PM
aloo aloo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 26 aloo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
ive changed the query like you said, now it doesn'y work at all.

even if i select the same item twice from product.php that quantity in the shopping cart remains 1.

Reply With Quote
  #12  
Old January 28th, 2004, 03:22 PM
Bruski Bruski is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Toronto, Canada
Posts: 192 Bruski User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
im looking at it, you have this online, cuz my server is down

Reply With Quote
  #13  
Old January 28th, 2004, 03:31 PM
Bruski Bruski is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Toronto, Canada
Posts: 192 Bruski User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
my guess is that it doesnt update the database because a variable isnt passed correctly

Reply With Quote
  #14  
Old January 28th, 2004, 03:40 PM
Bruski Bruski is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Toronto, Canada
Posts: 192 Bruski User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
i might be wrong, but your not assigning a value to the <option>

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



option is given no value, and thus when u pass it doesnt pass a variable for qty

you can try

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



this way if qty = to $i, it will be selected, otherwise it will be $i

Reply With Quote
  #15  
Old January 28th, 2004, 03:55 PM
aloo aloo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 26 aloo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
what happens now is that the qty does'nt change it only increases the total price

Reply With Quote
  #16  
Old January 28th, 2004, 07:27 PM
Bruski Bruski is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Toronto, Canada
Posts: 192 Bruski User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
once my server comes up ill fix it for u k,.... just send me the files

Reply With Quote
  #17  
Old January 29th, 2004, 05:37 AM
davidekins davidekins is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 9 davidekins User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 54 m 31 sec
Reputation Power: 0
Shopping Cart Tutorial - Checkout Page

Hi,

does anyone have a working checkout page for the tutorial, that they could share with us all?

Regards

David

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > shopping cart problem with quantity


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap