Jump to content

Update quantity format for simple shopping cart.


masalastican

Recommended Posts

I have a shopping cart that I'm using in my website and it uses a drop down menu to select the quantity.  For what I'm doing this will not work and I need this cart to update quantities by means of a text box.  My code looks like this:

 

<?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;

}

default:

{

ShowCart();

}

}

 

function AddItem($itemId, $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 itemId = $itemId");

$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, 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)

{

// Updates the quantity of an item in the users cart.

// If the quantity 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($itemId);

}

else

{

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

}

}

 

function RemoveItem($itemId)

{

// 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 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 inventory on cart.itemId = inventory.itemId where cart.cookieId = '" . GetCartId() . "' order by inventory.itemDesc asc");

?>

<html>

<head>

<title> Your Shopping Cart </title>

<font> <font face="Engravers MT" color="#000066" size="+2"</font>

<center>

<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>

<form name="frmCart" method="get">

<table width="95%" cellspacing="0" cellpadding="0" border="0">

<tr>

<td width=9%" height="25" bgcolor="white">

<font face="engravers MT" size="1" color="black">

  <b>Qty</b>

</font>

</td>

<td width="15%" height="25" bgcolor="white">

<font face="engravers MT" size="1"

color="black">

<b>SKU</b>

</font>

</td>

<td width="46%" height="25" bgcolor="white">

<font face="engravers MT" size="1"

color="black">

  <b>Description</b>

</font>

</td>

<td width="20%" height="25" bgcolor="white">

<font face="engravers MT" size="1"

color="black">

<b>Price Each</b>

</font>

</td>

<td width="10%" height="25" bgcolor="white">

<font face="engravers MT" size="1" color="black">

</font>

</td>

</tr>

<?php

 

while($row = mysql_fetch_array($result))

{

// Increment the total cost of all items

$totalCost += ($row["qty"] * $row["itemPrice"]);

?>

<tr>

<td width="10%" height="25">

<font face="verdana" size="1" color="black">

<select name="<?php echo $row["itemId"]; ?>" onChange="UpdateQty(this)">

<?php

 

for($i = 0; $i <= 500; $i++)

{

echo "<option ";

if($row["qty"] == $i)

{

echo " SELECTED ";

}

echo ">" . $i . "</option>";

}

?>

</select>

</font>

</td>

<td width="20%" height="25">

<font face="verdana" size="1" color="black">

<?php echo $row["itemSKU"]; ?>

</font>

</td>

<td width="35%" height="25">

<font face="verdana" size="1" color="black">

<?php echo $row["itemDesc"]; ?>

</font>

</td>

<td width="20%" height="25">

<font face="verdana" size="1" color="black">

$<?php echo number_format($row["itemPrice"], 2, ".", ","); ?>

</font>

</td>

<td width="15%" height="25">

<font face="verdana" size="1" color="black">

<b><center><a href="cart.php?action==update_item&id='+itemId+'&qty='+newQty;?>">Update Cart</a><center><b>

</font>

</td>

</tr>

<?php

}

 

// Display the total

?>

<tr>

<td width="100%" colspan="5">

<hr size="1" color="990000" NOSHADE>

</td>

</tr>

<tr>

<td width="30%" colspan="3">

<font face="verdana" size="1" color="black">

<a href="http://localhost"><< Keep Shopping</a>

</font>

</td>

<td width="70%" colspan="3">

<font face="Engravers MT" size="2" color="black">

<b>Total: $<?php echo number_format($totalCost, 2, ".", ","); ?></b>

</font>

</td>

</tr>

</table>

</form>

<br>

<br>

<font face="verdana" size="1"

color="black">

<center>If your cart does not contain all of your items please click the refresh button at the top of the browser window.</center>

</font>

</body>

</html>

<?php

}

 

?>

[code=php:0]

 

 

 

If anyone can help I'd greatly appreciate it!

it not right to display all your code just choose the line which you think has a problem

 

one more i always see people doing this

 

@@@@@@@ I dont know whats on their mind doing something like that why dont they cure real problem instead of doing that crazy stuff

I posted the whole code because I'm not sure what in this code needs to be referenced etc. by a text box in order for the text box to be able to submit information... I'm pretty clueless on this whole thing.  I'm sorry for all the code but I wanted to be thorough...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.