Dyamically add another Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

dubs
Forum Commoner
Posts: 28
Joined: Tue Oct 12, 2004 4:55 pm

Dyamically add another Array

Post by dubs »

Weirdan | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Code: Select all

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

//Has the Array been initialised if it has...
if(isset($_SESSION_VARS['cart'])){
	foreach($_SESSION_VARS['cart'] as $key => $val){
		if($val[0] == $Product){
		// If the product exists then we need to update the quantity
			$_SESSION_VARS['cart'][$key][1] = $Qty;
			echo $key . '|' . $val[0];		
		}
		else{
		// if the product isn't in the basket
			$new = 1;
		}
	}
}
if($new == 1){
//Add Another Row to my array
	$_SESSION_VARS['cart'][] = array($Product,$Qty,$Price); 
}

I'm stuck here, my code will only add one row to the array...what i want to do is keep adding rows to my array.

Weirdan | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

what i want to do is keep adding rows to my array
array_push();
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

or

Code: Select all

<?php
$arr[count($arr)] = "new_item";
?>
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

i guess his problem is that he nowhere calls [php_man]session_start[/php_man]
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

Actually there are other things,

Consider the following part

Code: Select all

<?php
if($new == 1){
    //Add Another Row to my array    
    $_SESSION_VARS['cart'][] = array($Product,$Qty,$Price); 
}
?>
and read this part again
my code will only add one row to the array
and look at the code again, this is what we suppose from code to do.

there are no loops here, so how can the code keep going to add new rows?
:lol:

but note that any of the 2 solutions is enough to add one more row to your array ;)

good luck.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

his code is working ;) (only, because session_start() is missing, he will always initialise $cart...)

Code: Select all

$test[] = array('name' => 'one');
$test[] = array('name' => 'two' );
print_r($test);
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

but then where is the "dubs". :) he/she forgets to get the answer :P
dubs
Forum Commoner
Posts: 28
Joined: Tue Oct 12, 2004 4:55 pm

I'm back - went away for the weekend

Post by dubs »

I'm using session_start at the top of every page

Code: Select all

<?php 
if($new == 1){//Add Another Row to my array    $_SESSION_VARS['cart'][] = array($Product,$Qty,$Price); }
?>
I thought that by leaving [] blank would have the same result as array_push.

If my user adds an item to their cart then the first thing i do is check the array has been intialized, then i check whether the item exists...if it does i increase the quantity.

if it doesn't then i simply add the item to the array using the above code.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

how are $Product, $Qty, $Price set/passed?
User avatar
harsha
Forum Contributor
Posts: 103
Joined: Thu Jul 11, 2002 1:35 am
Location: Bengaluru (Bangalore) > Karnataka > India

Post by harsha »

Hi
Dub
I just did a rough guess. will this work? :?: :idea: :!: :roll:

Code: Select all

<?php

if(!isset($_SESSION_VARS['cart']))
{                    
    $_SESSION_VARS['cart'][] = array(); 
    array_push($_SESSION_VARS['cart'][],array('Empty',0,0));
    $_SESSION_VARS['items'] = 0;
    $_SESSION_VARS['Total Price']= 0.00;
}

if(isset($_SESSION_VARS['cart'])){
    foreach($_SESSION_VARS['cart'] as $key => $val){
        if($val[0] == $Product){
        // If the product exists then we need to update the quantity
            $_SESSION_VARS['cart'][$key][1] = $Qty;
            echo $key . '|' . $val[0];        
        }
        else{
array_push($_SESSION_VARS['cart'][], array($Product,$Qty,$Price));
        }
    }
}
?>
Last edited by harsha on Mon Oct 18, 2004 3:45 am, edited 1 time in total.
dubs
Forum Commoner
Posts: 28
Joined: Tue Oct 12, 2004 4:55 pm

Post by dubs »

In the url but I'm converting them to normal variables:

Code: Select all

<?php
session_start();
$Product = $_GET['Product'];
$Qty = $_GET['Qty'];
$Price = $_GET['Price'];
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Can you confirm that you have error_reporting set to E_ALL, and display_errors is set to on/true/1 ?

I'm pretty sure you don't have E_ALL set, as you should get an unitialized variable warning, at the least.

Also note that $_SESSION_VARS does not exist. $_SESSION or $HTTP_SESSION_VARS exists, depending on your php version.
dubs
Forum Commoner
Posts: 28
Joined: Tue Oct 12, 2004 4:55 pm

Post 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_VARS['cart']))
{					
	$_SESSION_VARS['cart'][] = array();    
	$_SESSION_VARS['items'] = 0;
	$_SESSION_VARS['Total Price']= 0.00;
}

//Has the Array been initialised if it has...
if(isset($_SESSION_VARS['cart'])){
	foreach($_SESSION_VARS['cart'] as $key => $val){
		if($val[0] == 'Empty'){
		// If the product exists then we need to update the quantity
			$_SESSION_VARS['cart'][$key][1] = 55;
			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_VARS['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_VARS['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];

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you're still using $_SESSION_VARS which doesn't exist.
dubs
Forum Commoner
Posts: 28
Joined: Tue Oct 12, 2004 4:55 pm

Post by dubs »

Sorry, I'll change it and get back to you
Post Reply