Current location: Hot Scripts Forums » Programming Languages » PHP » shopping cart problem


shopping cart problem

Reply
  #1 (permalink)  
Old 11-01-03, 06:44 PM
darton520 darton520 is offline
New Member
 
Join Date: Aug 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
shopping cart problem

hey, i've been having some trouble with the devolopment of a shopping cart to use on my site. it says there is a parse error on the last line, but i've went through the entire script and couldn't find the problem. thanks for the help in advance.

<?php
session_start();
$connect=mysql_connect("localhost", "$username", "$pass");
mysql_select_db("$db", $connect);


function make_user_id($length, $strength=0) {
if(isset($_SESSION["user_id"]))
{
$user_id = $_SESSION["user_id"];
}
else
{
$vowels = 'aeiouy';
$consonants = 'bdghjlmnpqrstvwxz';
if ($strength & 1)
{
$consonants .= 'BDGHJLMNPQRSTVWXZ';
}
if ($strength & 2)
{
$vowels .= "AEIOUY";
}
if ($strength & 4)
{
$consonants .= '0123456789';
}
if ($strength & 8)
{
$consonants .= '@#$%^';
}
$user_id = '';
$alt = time() % 2;
srand(time());
for ($i = 0; $i < $length; $i++) {
if ($alt == 1)
{
$user_id .= $consonants[(rand() % strlen($consonants))];
$alt = 0;
}
else
{
$user_id .= $vowels[(rand() % strlen($vowels))];
$alt = 1;
}
}
return $user_id;
session_register('user_id');
$_SESSION['user_id'] = $user_id;
}
}


switch($action){
case "add":
{
add($_GET["id"], $_GET["qty"]);
showcart();
break;
}
case "update":
{
update($itemid, $itemqty);
showcart();
break;
}
case "remove":
{
remove($itemid);
showcart();
break;
}
default:
{
showcart();
}
}


function add($itemid, $itemqty)
{
$sql = mysql_query("SELECT * FROM cart WHERE
userid='" . make_user_id() . "' AND id='$itemqty'");
$row = mysql_fetch_array($sql);
$numrows = $row[0];

if($numRows == 0)
{
mysql_query("INSERT INTO cart(cookieId, itemId, qty, itemPrice)
VALUES('" . make_user_id() . "', $id, $itemqty, $price)");
}
else
{
update($itemid, $itemqty);
}

function update($itemid, $itemqty)
{
if($itemqty == 0)
{
remove($itemId);
}
elseif($itemqty > $qty)
{
$result = 'We\'re sorry, but we only have $qty of the item
you requested<br>';
}
else
{
mysql_query("UPDATE cart SET qty=$itemqty
WHERE cookieId='" . make_user_id() . "' AND itemId=$itemid");
}
}

function remove($itemid)
{
mysql_query("DELETE FROM cart WHERE cookieId='" . make_user_id() . "'
AND itemId=itemId");
}

function showcart()
{
$totalCost = 0;
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title><?php echo "$title"; ?></title>
</head>

<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2"><?php include 'top.php'; ?>&nbsp;</td>
</tr>
<tr>
<td width="161"><?php include 'side.php'; ?>&nbsp;</td>
<td><div align="center"><?php echo '$result'; ?><?php echo '$result2'; ?>
<?php echo '$result3'; ?>
<table width="100%" border="1" cellpadding="0" cellspacing="0" bordercolor="#000066">
<tr>
<td width="15%" height="25" bgcolor="#000099"> <font face="verdana" size="1" color="white">
&nbsp;&nbsp;<b>Qty</b> </font> </td>
<td width="55%" height="25" bgcolor="#000099"> <font face="verdana" size="1" color="white">
<b>Product</b> </font> </td>
<td width="20%" height="25" bgcolor="#000099"> <font face="verdana" size="1" color="white">
<b>Price </b></font> </td>
<td width="10%" height="25" bgcolor="#000099"> <font face="verdana" size="1" color="white">
<b>Remove?</b> </font> </td>
</tr>
<?php
$query = "select * from cart where cookieId='" . make_user_id() . "' order by
itemId desc";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
$totalCost += ($row["qty"] * $row["itemPrice"]);
?>
<tr>
<td width="15%" height="24"> <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
}
?>
<tr>
<td width="100%" colspan="4">
<hr size="1" color="red" 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>
</p>
</body>
</html>
Reply With Quote
  #2 (permalink)  
Old 11-02-03, 07:28 PM
evo4ever evo4ever is offline
Software Developer Guru
 
Join Date: Aug 2003
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
Getting a parse error on the last line usually means you are missing a closing code brace ( } ). Revise your if/else, for, while, switch etc for a missing closing brace.

PS: Your code will be much easier to read if it was indented.
Reply With Quote
  #3 (permalink)  
Old 11-03-03, 11:20 AM
jewellgr jewellgr is offline
Wannabe Coder
 
Join Date: Aug 2003
Location: Michigan USA
Posts: 111
Thanks: 0
Thanked 0 Times in 0 Posts
I looked at you code and changed two errors I got. You were getting an error because you forgot to put a closing bracket after your add() function.
Here is the code I changed:

Code:
<?php 
session_start();
$connect=mysql_connect("localhost", "$username", "$pass");
mysql_select_db("$db", $connect);


function make_user_id($length, $strength=0) {
if(isset($_SESSION["user_id"]))
{
$user_id = $_SESSION["user_id"];
}
else
{
$vowels = 'aeiouy';
$consonants = 'bdghjlmnpqrstvwxz';
if ($strength & 1) 
{
$consonants .= 'BDGHJLMNPQRSTVWXZ';
}
if ($strength & 2) 
{
$vowels .= "AEIOUY";
}
if ($strength & 4)
{
$consonants .= '0123456789';
}
if ($strength & 8) 
{
$consonants .= '@#$%^';
}
$user_id = '';
$alt = time() % 2;
srand(time());
for ($i = 0; $i < $length; $i++) {
if ($alt == 1) 
{
$user_id .= $consonants[(rand() % strlen($consonants))];
$alt = 0;
} 
else 
{
$user_id .= $vowels[(rand() % strlen($vowels))];
$alt = 1;
}
}
return $user_id;
session_register('user_id'); 
$_SESSION['user_id'] = $user_id;
}
}


switch($action){
case "add":
{
add($_GET["id"], $_GET["qty"]);
showcart();
break;
}
case "update":
{
update($itemid, $itemqty);
showcart();
break;
}
case "remove":
{
remove($itemid);
showcart();
break;
}
default:
{
showcart();
}
}


function add($itemid, $itemqty)
{
$sql = mysql_query("SELECT * FROM cart WHERE 
userid='" . make_user_id() . "' AND id='$itemqty'");
$row = mysql_fetch_array($sql);
$numrows = $row[0];

if($numRows == 0)
{
mysql_query("INSERT INTO cart(cookieId, itemId, qty, itemPrice) 
VALUES('" . make_user_id() . "', $id, $itemqty, $price)");
} 
else 
{
update($itemid, $itemqty);
}
} // Added this bracket

function update($itemid, $itemqty)
{
if($itemqty == 0)
{
remove($itemId);
}
elseif($itemqty > $qty)
{
$result = 'We\'re sorry, but we only have $qty of the item 
you requested<br>';
}
else
{
mysql_query("UPDATE cart SET qty=$itemqty 
WHERE cookieId='" . make_user_id() . "' AND itemId=$itemid");
}
}

function remove($itemid)
{
mysql_query("DELETE FROM cart WHERE cookieId='" . make_user_id() . "' 
AND itemId=itemId");
}

function showcart()
{
$totalCost = 0;
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title><?php echo "$title"; ?></title>
</head>

<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr> 
<td colspan="2"><?php include 'top.php'; ?>&nbsp;</td>
</tr>
<tr> 
<td width="161"><?php include 'side.php'; ?>&nbsp;</td>
<td><div align="center"><?php echo '$result'; ?><?php echo '$result2'; ?>
<?php echo '$result3'; ?>
<table width="100%" border="1" cellpadding="0" cellspacing="0" bordercolor="#000066">
<tr> 
<td width="15%" height="25" bgcolor="#000099"> <font face="verdana" size="1" color="white"> 
&nbsp;&nbsp;<b>Qty</b> </font> </td>
<td width="55%" height="25" bgcolor="#000099"> <font face="verdana" size="1" color="white"> 
<b>Product</b> </font> </td>
<td width="20%" height="25" bgcolor="#000099"> <font face="verdana" size="1" color="white"> 
<b>Price </b></font> </td>
<td width="10%" height="25" bgcolor="#000099"> <font face="verdana" size="1" color="white"> 
<b>Remove?</b> </font> </td>
</tr>
<?php
$query = "select * from cart where cookieId='" . make_user_id() . "' order by 
itemId desc";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
$totalCost += ($row["qty"] * $row["itemPrice"]);
?>
<tr> 
<td width="15%" height="24"> <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
}
?>
<tr>
<td width="100%" colspan="4">
<hr size="1" color="red" 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>
</p>
</body>
</html>
__________________
ArecaWeb Team
Gregg Kenneth Jewell

Last edited by jewellgr; 11-03-03 at 11:22 AM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to refresh a shopping cart window darksoap JavaScript 3 03-05-05 03:28 PM
shopping cart michelle Job Offers & Assistance 2 06-25-04 10:59 AM
I need Help with Dropdown list knightjt02 PHP 3 10-30-03 03:10 AM
whats the best php free shopping cart script? dgames PHP 4 09-19-03 01:40 AM


All times are GMT -5. The time now is 07:22 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.