Page 2 of 2

Posted: Mon Oct 18, 2004 6:15 am
by dubs

Code: Select all

<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('html_errors', false);
//set new product flag to zero
$new = 0;

//Conver URL variable to normal variables
$Product = $_GET['Product'];
$Qty = $_GET['Qty'];
$Price = $_GET['Price'];

//Is there a cart and if not create one
if(!isset($_SESSION['cart']))
{					
	$_SESSION ['cart'][] = array();    
	$_SESSION ['items'] = 0;
	$_SESSION ['Total Price']= 0.00;
}

//Has the Array been initialised if it has...
if(isset($_SESSION  ['cart'])){
	foreach($_SESSION ['cart'] as $key => $val){
		if($val[0] == $Product){
		// If the product exists then we need to update the quantity
			$_SESSION ['cart'][$key][1] = +$Qty;
			//echo $key . ' | ' . $val[0];		
		}
		else{
		// if the product isn't in the basket
			$new = 1;
		}
	}
}
if($new == 1){
	//if the add new variable is 1 then append another array to cart
	$_SESSION ['cart'][] = array($Product,$Qty,$Price); 
}
/*		
if(isset($_SESSION_VARS['cart'])) {
// Does the cart element exist if not add a new one
	foreach($_SESSION_VARS['cart'] as $key => $val){
		echo $val[0];
		if ($val[0] == $Product){
			echo $val[0] . ' ' . 'Product Already Exist';
			$_SESSION_VARS['cart'][$key][1] = 2;		
		}
		else {		
			$new = 1;
		}
			//echo $val[0] . '<br>';
			//echo $key;		
	}
}
if(isset($new) && $new == 1){
	$_SESSION_VARS['cart'][] = array($Product,$Qty,$Price); 
}
*/
		
//Debug
echo '<pre>';
print_r($_SESSION ['cart']);
echo '</pre>';
//echo count($_SESSION_VARS['cart']);
//echo $_SESSION_VARS['cart'][0][0];
//echo $_SESSION_VARS['cart'][0][1];
//echo $_SESSION_VARS['cart'][0][2];

?>
I'm finally getting the array to increment, but I'm now getting an error

"Notice: Undefined offset: 0 in c:\program files\apache group\apache\htdocs\roadnet\product.php on line 25"



From this error I'm deducing that var[0] doesn't exist, how do i get around this.

Posted: Mon Oct 18, 2004 11:00 am
by feyd
[php_man]empty()[/php_man] may be of use.

Posted: Mon Oct 18, 2004 11:06 am
by timvw
dubs wrote:

Code: Select all

foreach($_SESSION ['cart'] as $key => $val){
		if($val[0] == $Product){
make that

Code: Select all

foreach($_SESSION['cart'] as $cart)
{
     if ($cart[0] == $Product) {