creating shooping cart

A

Anonymous

Guest
Hi guys,

I have some problem in creating a shopping cart.. actually i tried learn how to create a shopping cart by following one of the article published in DevArticle... but encoutered a lot of prob and i hope some of u guys might able to help

My questions:
by the time i press the showcart link in the product.php page, it shows a blank page... a page with some output should be displayed right ?? did i insert the javascript at the wrong place or what ?

when press the add item link, it says that no database selected.. why is that so ??? the include file do works in the product page then why it doesn't work in cart page ???

i did try to put all the functions into a function.php and include it in the file after include(db.php) so i dun have to put all the functions after the switch case and i can used it for some other page... but it shows a lot of errors when i use include("function.php") function .. thats y i didn't make an include file for all function.. my friend said my showcart function contains error.. is that so ??

I did use global for

Please advise.. TQ


These are my coding..
my db.php file:


PHP:--------------------------------------------------------------------------------

<?

Hi guys,

I have some problem in creating a shopping cart.. actually i tried learn how to create a shopping cart by following one of the article published in DevArticle... but encoutered a lot of prob

My questions:
by the time i press the showcart link in the product.php page, it shows a blank page... a page with some output should be displayed right ?? did i insert the javascript at the wrong place or what ?

when press the add item link, it says that no database selected.. why is that so ??? the include file do works in the product page then why it doesn't work in cart page ??? I did try to use global in my db.php file for dbServer, dbPass, dbName and name_db but the result still the same.. no db selected.. is it the scope prob ???

i did try to put all the functions into a function.php and include it in the file after include(db.php) so i dun have to put all the functions after the switch case and i can used it for some other page... but it shows a lot of errors when i use include("function.php") function .. thats y i didn't make an include file for all function.. my friend said my showcart function contains error.. is that so ??

Please advise.. TQ


These are my coding..
my db.php file:


PHP:--------------------------------------------------------------------------------

<?

$dbServer = "localhost";
$dbUser = "user";
$dbPass = "pass";
$dbName = "name_db";

function ConnectToDb($server, $user, $pass, $database)
{
$s = mysql_connect($server, $user, $pass) or die("Query failed: $query<br><br>" . mysql_error());
$d = mysql_select_db($database, $s) or die("Query failed: $query<br><br>" . mysql_error());
/*
if (!$s || !$d)
return false ;
else
return true ;
*/

}



function GetCartId()
{
//generate an encrypted string and set it as cookie
if (isset($_cookie["cartId"]))
{
return $_cookie["cartId"];
}
else
{
session_start();
setcookie("cartId", session_Id(), time() + ((3600 * 24) * 30));
return session_id();
}
}

?>


my products.php file:
<?

include ("db.php");

$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
//$result = mysql_query("select * from items order by itemName asc");
//show the error with mysql_error() function
$result = mysql_query("select * from items order by itemName asc") or die("Query failed: $query<br><br>". mysql_error());

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<BODY>

<table border="0">
<?php
while($row = mysql_fetch_array($result))
{
?>
<tr>
<td width="10%" height="25">

<font face="Arial" size="2" color="black"><?php echo $row["itemName"]; ?>
</font>
</td>

<td width="10%" height="25">
<font face="arial" size="2" color="black">
RM<? echo $row["itemPrice"]; ?>
</font>
</td>

<td width="20%" height="25">
<font face="arial" size="2" color="black"><?php echo $row["itemDesc"]; ?>
</font>
</td>

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

<font face="verdana" size="2" color="black"><a href ="cart.php?action=add_item&id=<?php echo $row["itemId"];?>&qty=1">Add Item
</a></font>
</td>
<td></td>
</tr>


<tr>
<td width="100%" colspan="4">
<hr size="1" color="red" NOSHADE>
</td>
</tr>
<? } ?>
<tr>
<td width="100%" colspan="4">
<font face="arial" size="1" color="black"><a href="cart.php">Your shopping cart >>
</a></font>
</td>
</tr>

</table>
</BODY>



my cart.php file:
<?

include("db.php");
Switch($_GET["action"])
{
case "add_item":
{
AddItem($_GET["id"], $_GET["qty"]);
//ShowCart();
include("showcart.php");
break;
}

case "update_item":
{
UpdateItem($_GET["id"], $_GET["qty"]);
//ShowCart();
include("showcart.php");
break;
}

case "remove_item":
{
RemoveItem($_GET["id"]);
//ShowCart();
include("showcart.php");
break;
}

default:
{
//ShowCart();
include("showcart.php");
}

}

function AddItem($itemId, $qty)
{
$result = mysql_query("Select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId")or die("Query failed: $query<br><br>" . mysql_error());

$row = mysql_fetch_row($result);
$numRows = $row[0];

if($numRows == 0)
{
mysql_query("insert into cart(cookieId, itemId, qty) values('".GetCartId()."', $itemId, $qty)") or die("query failed".mysql_error());
}

else

{

UpdateItem($itemId, $qty);

}
}


function UpdateItem($itemId, $qty)
{

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

}


function RemoveItem($itemId)
{

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

?>


my showcart.php file:
<?
function ShowCart()
{
?>
<script language="JavaScript">
function UpdateQty(item)
{
itemId = item.name;
newQty = item.options[item.selectedIndex].text;

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

</script>

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

while($row = mysql_fetch_array($result))
{
$totalCost += ($row["qty"] * $row["itemPrice"]);
?>

<tr>
<td width = "15%" height="25">
<font face="arial" size="2" color="black">
<select name="<? echo $row["itemId"]; ?>" onChange="UpdateQty(this)">

<?
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="arial" size="2" color="black">
<? echo $row["itemName"]; ?>
</font>
</td>

<td width="20%" height="25">
<font face="arial" size="2" color="black">
RM<? echo number_format($row["itemPrice"], 2,".", "."); ?>
</font>
</td>

<td width="10%" height="25">
<font face="arial" size="2" color="black">
<a href="cart.php?action=remove_item&id=<? echo $row["itemId"]; ?>">Remove</a>
</font>
</td>

<?
} // close for while loop

//$totalCost += ($row["qty"] * $$row["itemPrice"]);
//$totalCost = $totalCost + ($row["qty"] * $row["itemPrice"]); // same by using +=
?>


<tr>
<td width="100%" colspan="4">
<hr size="1" color="red" NOSHADE>
</td>
</tr>

<tr>
<td width="70%" colspan="2">
<font face="arial" size="2" color="black">
<a href="products.php"><< Keep Shopping</a>
</font>
</td>

<td width="30%" colspan="2">
<font face="arial" size="3" color="black">
<b>Total: RM<? echo number_format($totalCost, 2, ".","."); ?> </b>
</font>
</td>
</tr>
<? } //close function?>

function ConnectToDb($server, $user, $pass, $database)
{
$s = mysql_connect($server, $user, $pass) or die("Query failed: $query<br><br>" . mysql_error());
$d = mysql_select_db($database, $s) or die("Query failed: $query<br><br>" . mysql_error());
/*
if (!$s || !$d)
return false ;
else
return true ;
*/

}



function GetCartId()
{
//generate an encrypted string and set it as cookie
if (isset($_cookie["cartId"]))
{
return $_cookie["cartId"];
}
else
{
session_start();
setcookie("cartId", session_Id(), time() + ((3600 * 24) * 30));
return session_id();
}
}

?>


my products.php file:
<?

include ("db.php");

$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
//$result = mysql_query("select * from items order by itemName asc");
//show the error with mysql_error() function
$result = mysql_query("select * from items order by itemName asc") or die("Query failed: $query<br><br>". mysql_error());

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<BODY>

<table border="0">
<?php
while($row = mysql_fetch_array($result))
{
?>
<tr>
<td width="10%" height="25">

<font face="Arial" size="2" color="black"><?php echo $row["itemName"]; ?>
</font>
</td>

<td width="10%" height="25">
<font face="arial" size="2" color="black">
RM<? echo $row["itemPrice"]; ?>
</font>
</td>

<td width="20%" height="25">
<font face="arial" size="2" color="black"><?php echo $row["itemDesc"]; ?>
</font>
</td>

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

<font face="verdana" size="2" color="black"><a href ="cart.php?action=add_item&id=<?php echo $row["itemId"];?>&qty=1">Add Item
</a></font>
</td>
<td></td>
</tr>


<tr>
<td width="100%" colspan="4">
<hr size="1" color="red" NOSHADE>
</td>
</tr>
<? } ?>
<tr>
<td width="100%" colspan="4">
<font face="arial" size="1" color="black"><a href="cart.php">Your shopping cart >>
</a></font>
</td>
</tr>

</table>
</BODY>



my cart.php file:
<?

include("db.php");
Switch($_GET["action"])
{
case "add_item":
{
AddItem($_GET["id"], $_GET["qty"]);
//ShowCart();
include("showcart.php");
break;
}

case "update_item":
{
UpdateItem($_GET["id"], $_GET["qty"]);
//ShowCart();
include("showcart.php");
break;
}

case "remove_item":
{
RemoveItem($_GET["id"]);
//ShowCart();
include("showcart.php");
break;
}

default:
{
//ShowCart();
include("showcart.php");
}

}

function AddItem($itemId, $qty)
{
$result = mysql_query("Select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId")or die("Query failed: $query<br><br>" . mysql_error());

$row = mysql_fetch_row($result);
$numRows = $row[0];

if($numRows == 0)
{
mysql_query("insert into cart(cookieId, itemId, qty) values('".GetCartId()."', $itemId, $qty)") or die("query failed".mysql_error());
}

else

{

UpdateItem($itemId, $qty);

}
}


function UpdateItem($itemId, $qty)
{

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

}


function RemoveItem($itemId)
{

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

?>


my showcart.php file:
<?
function ShowCart()
{
?>
<script language="JavaScript">
function UpdateQty(item)
{
itemId = item.name;
newQty = item.options[item.selectedIndex].text;

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

</script>

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

while($row = mysql_fetch_array($result))
{
$totalCost += ($row["qty"] * $row["itemPrice"]);
?>

<tr>
<td width = "15%" height="25">
<font face="arial" size="2" color="black">
<select name="<? echo $row["itemId"]; ?>" onChange="UpdateQty(this)">

<?
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="arial" size="2" color="black">
<? echo $row["itemName"]; ?>
</font>
</td>

<td width="20%" height="25">
<font face="arial" size="2" color="black">
RM<? echo number_format($row["itemPrice"], 2,".", "."); ?>
</font>
</td>

<td width="10%" height="25">
<font face="arial" size="2" color="black">
<a href="cart.php?action=remove_item&id=<? echo $row["itemId"]; ?>">Remove</a>
</font>
</td>

<?
} // close for while loop

//$totalCost += ($row["qty"] * $$row["itemPrice"]);
//$totalCost = $totalCost + ($row["qty"] * $row["itemPrice"]); // same by using +=
?>


<tr>
<td width="100%" colspan="4">
<hr size="1" color="red" NOSHADE>
</td>
</tr>

<tr>
<td width="70%" colspan="2">
<font face="arial" size="2" color="black">
<a href="products.php"><< Keep Shopping</a>
</font>
</td>

<td width="30%" colspan="2">
<font face="arial" size="3" color="black">
<b>Total: RM<? echo number_format($totalCost, 2, ".","."); ?> </b>
</font>
</td>
</tr>
<? } //close function?>
 
I don't have time to go through it line by line but at a glance I saw these problems.
Code:
//generate an encrypted string and set it as cookie 
if (isset($_cookie["cartId"])) 
{ 
return $_cookie["cartId"]; 
}

Variables are case sensitive. $_cookie[] does not exist. it is $_COOKIE[]
 
Redcircle, really thanks for you help, the errors seem to be gone.. however.. there is a fatal error occured..

error:
Fatal error: Cannot redeclare getcartid() (previously declared in /home/eonenet/public_html/shopping_cart/cart.php:15) in /home/eonenet/public_html/shopping_cart/db.php on line 28


may i know what does it mean ?? i checked my cart.php and i didn't declare this getcarid function but how come the such error occured ??

please advise
 
Back
Top