Jump to content

Help plzzz!! add to cart problem..


joinx

Recommended Posts

I am trying to retrive the product id and add to cart and displat cart..here is my code but not working:

showcart.php:

<?php require_once('../Connections/JaceyConn.php'); ?>
<?php require_once('functions.inc.php');

if (!isset($_SESSION)) {
  session_start();
}

$MM_authorizedUsers = "";
$MM_donotCheckaccess = "true";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
  // For security, start by assuming the visitor is NOT authorized. 
  $isValid = False; 

  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. 
  // Therefore, we know that a user is NOT logged in if that Session variable is blank. 
  if (!empty($UserName)) { 
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. 
    // Parse the strings into arrays. 
    $arrUsers = Explode(",", $strUsers); 
    $arrGroups = Explode(",", $strGroups); 
    if (in_array($UserName, $arrUsers)) { 
      $isValid = true; 
    } 
    // Or, you may restrict access to only certain users based on their username. 
    if (in_array($UserGroup, $arrGroups)) { 
      $isValid = true; 
    } 
    if (($strUsers == "") && true) { 
      $isValid = true; 
    } 
  } 
  return $isValid; 
}

$MM_restrictGoTo = "login.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0) 
  $MM_referrer .= "?" . $QUERY_STRING;
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); 
  exit;
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$colname_result = "-1";
if (isset($_GET['product_id'])) {
  $colname_result = $_GET['product_id'];
}
mysql_select_db($database_JaceyConn, $JaceyConn);
$query_result = sprintf("SELECT * FROM product WHERE product_id = %s", GetSQLValueString($colname_result, "int"));
$result = mysql_query($query_result, $JaceyConn) or die(mysql_error());
$row_result = mysql_fetch_assoc($result);
$totalRows_result = mysql_num_rows($result);
?>
<?
// Process actions
$cart = $_SESSION['cart']; 
if ($cart) { 
$cart .= ','.$_GET['product_id']; 
} else { 
$cart = $_GET['product_id']; 
} 
$_SESSION['cart'] = $cart;

$action = $_GET['action'];
switch ($action) {
case 'add':
	if ($cart) {
		$cart .= ','.$_GET['product_id'];
	} else {
		$cart = $_GET['product_id'];
	}
	break;
case 'delete':
	if ($cart) {
		$items = explode(',',$cart);
		$newcart = '';
		foreach ($items as $item) {
			if ($_GET['product_id'] != $item) {
				if ($newcart != '') {
					$newcart .= ','.$item;
				} else {
					$newcart = $item;
				}
			}
		}
		$cart = $newcart;
	}
	break;
case 'update':
if ($cart) {
	$newcart = '';
	foreach ($_POST as $key=>$value) {
		if (stristr($key,'qty')) {
			$id = str_replace('qty','',$key);
			$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
			$newcart = '';
			foreach ($items as $item) {
				if ($id != $item) {
					if ($newcart != '') {
						$newcart .= ','.$item;
					} else {
						$newcart = $item;
					}
				}
			}
			for ($i=1;$i<=$value;$i++) {
				if ($newcart != '') {
					$newcart .= ','.$id;
				} else {
					$newcart = $id;
				}
			}
		}
	}
}
$cart = $newcart;
break;
}


?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<meta name="description" content="description"/>
<meta name="keywords" content="keywords"/> 
<meta name="author" content="author"/> 
<link rel="stylesheet" type="text/css" href="bntemplate590/images590/default.css"/>
<title>Shopping Cart</title>
<style type="text/css">
<!--
.style4 {font-size: 14pt}
body,td,th {
font-size: 9pt;
}
-->
</style></head>

<body>

<div class="main">

<div class="main_left">

<div class="header">
		<h1 align="center">Jacey Computers</h1>
  </div>

	<div class="link_menu">
		<a href="main.php" accesskey="1">Home</a>
		<a href="category.php" accesskey="2">Product</a>
		<a href="support.php" accesskey="3">Support & Help</a>
		<a href="contact.php" accesskey="4">Contact Us</a>		</div>

	<div class="content">
        <?php
	$qty=1; //by default

      	$total = 0;
	$id=$_GET['product_id'];
	$cart = $_SESSION['cart'];
if ($cart) {
	$items = explode(',',$cart);
	$contents = array();
	foreach ($items as $item) {
		$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
	}
	$output[] = '<form action="showcart.php?action=update" method="post" id="cart">';
	$output[] = '<table>';

		foreach ($contents as $id=>$qty){
		mysql_free_result($result);
		extract($row);	
		$output[] = '<tr>';
		$output[] = '<td><a href="showcart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
		$output[] = '<td>'.$row['product_name'].'</td>';
		$output[] = '<td>Rs;'.$row['product_price'].'</td>';
		$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
		$output[] = '<td>Rs;'.($price * $qty).'</td>';
		$total += $price * $qty;
		$output[] = '</tr>';
	}
	$output[] = '</table>';
	$output[] = '<p>Grand total: <strong>Rs;'.$total.'</strong></p>';
	$output[] = '<div><button type="submit">Update cart</button></div>';
	$output[] = '</form>';
} else {
	$output[] = '<p>You shopping cart is empty.</p>';
}
$d=join('',$output);
echo $d;

?>     



        <form id="form1" method="post" action="order.php">
          <input name="order" type="submit" value="Confirm Order" />
             or <a href="category.php"> Continue Shopping.</a>   
        </form>
       
      	  </div>
</div>

</div>
<div class="footer"></div>
</div>
<br style="clear: both;" />

</body>

</html>

 

Functions.inc.php:

<?php
function writeShoppingCart() {
$cart = $_SESSION['cart'];
if (!$cart) {
	return '<p>You have no items in your shopping cart</p>';
} else {
	// Parse the cart session variable
	$items = explode(',',$cart);
	$s = (count($items) > 1) ? 's':'';
	return '<p>You have <a href="showcart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>';
}
}?>

 

and here the error i am getting..

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Apache2.2\htdocs\Jacey\functions.inc.php:52) in C:\Apache2.2\htdocs\Jacey\showcart.php on line 5

 

Warning: extract() [function.extract]: First argument should be an array in C:\Apache2.2\htdocs\Jacey\showcart.php on line 214

 

 

Link to comment
https://forums.phpfreaks.com/topic/103118-help-plzzz-add-to-cart-problem/
Share on other sites

plz help...need to make that work..still hasn't find a solution...

 

getting errors like

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Apache2.2\htdocs\Jacey\functions.inc.php:52) in C:\Apache2.2\htdocs\Jacey\showcart.php on line 5

 

Warning: extract() [function.extract]: First argument should be an array in C:\Apache2.2\htdocs\Jacey\showcart.php on line 214

 

 

Only thing I can think of is you have output before the session_start() function. Put your includes below that.

 

you don't need to check for the $_SESSION either. just do:

<?php

session_start();

// rest of script
?>

 

If it says headers are already sent, it's saying that the page has already started to load prior to setting up some other header information. IE perhaps you have an error being reported prior to the session_start()

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.