/    Sign up×
Community /Pin to ProfileBookmark

this is the code i was using

[code=php]<select name=”<?php echo $row[“itemId”]; ?>” onChange=”UpdateQty(this)”>
<?php
for($i = 1; $i <= 20; $i++)
{
echo “<option “;
if($row[“qty”] == $i)
{
echo ” SELECTED “;
}
echo “>” . $i . “</option>”;
}
?>
</select>[/code]

it updates the item the works out the total cost but i am don’t want this now as it looks poor on the site. can anyone help me with a update that would work by people just typing the amount in.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@SamFeb 06.2004 — could we get either a link, or a full source?
Copy linkTweet thisAlerts:
@chris9902authorFeb 06.2004 — why?

that is the only function i need help with
Copy linkTweet thisAlerts:
@SamFeb 07.2004 — because your code isn't enough to see it in context
Copy linkTweet thisAlerts:
@chris9902authorFeb 07.2004 — [code=php]
<?php
error_reporting(0);
include("db.php");

switch($_GET["action"])
{
case "add_item":
{
AddItem($_GET["itemId"], $_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)
{
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);
$numRows = $row[0];
if($numRows == 0)
{
@mysql_query("insert into cart(cookieId, itemId, qty) values('" . GetCartId() . "', $itemId, $qty)");
}
else
{
UpdateItem($itemId, $qty);
}
}
function UpdateItem($itemId, $qty)
{
global $dbServer, $dbUser, $dbPass, $dbName;
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);

if($qty == 0)
{
RemoveItem($itemId);
}
else
{
mysql_query("update cart set qty = $qty where cookieId = '" . GetCartId() . "' and itemId = $itemId");
}
}
function RemoveItem($itemId)
{
global $dbServer, $dbUser, $dbPass, $dbName;
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);

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

function ShowCart()
{
global $dbServer, $dbUser, $dbPass, $dbName;
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);

$totalCost = 0;
$result = mysql_query("select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" . GetCartId() . "' order by items.itemName asc");
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Shopping Cart</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script language="JavaScript" type="text/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>

</body>
</html>

<form name="frmCart" method="get" action="">
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="15%" height="25" bgcolor="red">
<font face="verdana" size="1" color="white">
&nbsp;&nbsp;<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["itemPrice"]);
?>
<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 <= 20; $i++)
{
echo "<option ";
if($row["qty"] == $i)
{
echo " SELECTED ";
}
echo ">" . $i . "</option>";
}
?>
</select>
</font>
</td>
<td width="55%" height="25">
<font face="verdana" size="1" color="black">
<?php echo $row["itemName"]; ?>
</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="10%" height="25">
<font face="verdana" size="1" color="black">
<a href="cart.php?action=remove_item&id=<?php echo $row["itemId"]; ?>">Remove</a>
</font>
</td>
</tr>
<?php
}

// Display the total
?>
<tr>
<td width="100%" colspan="4">
<hr size="1" NOSHADE>
</td>
</tr>
<tr>
<td width="70%" colspan="2">
<font face="verdana" size="1" color="black">
<a href="products.php">&lt;&lt; Keep Shopping</a>
</font>
</td>
<td width="30%" colspan="2">
<font face="verdana" size="2" color="black">
<b>Total: $<?php echo number_format($totalCost, 2, ".", ","); ?></b>
</font>
</td>
</tr>
</table>
</form>
</body>
</html>
<?php
}

?>[/code]
×

Success!

Help @chris9902 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...