Page 1 of 2
Dyamically add another Array
Posted: Fri Oct 15, 2004 12:08 pm
by dubs
Weirdan | Help us, help you. Please use 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
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Posted: Fri Oct 15, 2004 1:14 pm
by kettle_drum
what i want to do is keep adding rows to my array
array_push();
Posted: Sat Oct 16, 2004 6:07 am
by dethron
or
Code: Select all
<?php
$arr[count($arr)] = "new_item";
?>
Posted: Sat Oct 16, 2004 7:08 am
by timvw
i guess his problem is that he nowhere calls [php_man]session_start[/php_man]
Posted: Sat Oct 16, 2004 9:25 am
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?
but note that any of the 2 solutions is enough to add one more row to your array
good luck.
Posted: Sat Oct 16, 2004 11:35 am
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);
Posted: Sat Oct 16, 2004 3:33 pm
by dethron
but then where is the "dubs".

he/she forgets to get the answer

I'm back - went away for the weekend
Posted: Mon Oct 18, 2004 3:27 am
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.
Posted: Mon Oct 18, 2004 3:39 am
by feyd
how are $Product, $Qty, $Price set/passed?
Posted: Mon Oct 18, 2004 3:43 am
by harsha
Hi
Dub
I just did a rough guess. will this work?
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));
}
}
}
?>
Posted: Mon Oct 18, 2004 3:44 am
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'];
?>
Posted: Mon Oct 18, 2004 3:55 am
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.
Posted: Mon Oct 18, 2004 4:10 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_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];
?>
Posted: Mon Oct 18, 2004 4:25 am
by feyd
you're still using $_SESSION_VARS which doesn't exist.
Posted: Mon Oct 18, 2004 4:27 am
by dubs
Sorry, I'll change it and get back to you