I'm trying to do a shopping cart feature, and it works really well with a sample script, but when I try to implement it on a longer scripts (with functions)
errors occur..
something like "header already sent on line blah blah" (the line is one the Setcookie() line)
here're parts of my scripts:
main store script:
$tablestore = "shopping";
include( "shoppingcart.php3");
$cart = new Cart;
//if(!$cookies)
//$cookies = $session;
require($carcloak[$page].".inc");
if($add)
{
if($cookies)
$session = $cookies;
$result = mysql_query( "SELECT * FROM car_record WHERE id='$add'");
$row = mysql_fetch_array($result);
$cart->add_item($tablestore,$session,$row[id])
}
function somefunction() {
global $REQUEST_URI,$session,$cookies;
while($row = mysql_fetch_array($result))
{
echo "<tr><FORM ACTION='ecs_database.php3' METHOD=post><td bgcolor=$color><center><font face=verdana size=1>$row[counter]</font></center></td>\n";
echo "<td bgcolor=$color><CENTER><A HREF=\"$REQUEST_URI&add=".urlencode($row[id]);
if (!$cookies)
echo "&session=$session\">";
else
echo "\">";
}
shoppingcart.php3
if(!$session && !$cookies) //make sure this hasn't already been established
{
$session = md5(uniqid(rand())); //creates a random session value
Setcookie( "cookies", "$session",time()+7200);
}
class Cart
{
function add_item($tablestore, $session,$id)
{
// Checks to see if they already have that id in their list
$in_list = "SELECT * FROM $tablestore WHERE session='$session' ";
$in_list .= "AND id='$id'";
$result = mysql_query( "$in_list");
$num_rows = mysql_num_rows($result);
// they don't have that id in their cart? Put it in.
if($num_rows == 0)
{
$sql = "INSERT INTO $tablestore (session,id) VALUES ";
$sql .= "('$session','$id')";
mysql_query( "$sql");
}
Someboy enlighten me.. 🙁
im going nuts.
Thanks!!!!!