homepage Welcome to WebmasterWorld Guest from 54.234.0.85
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Array being reset rather than added to
algalogica




msg:4550006
 1:42 am on Mar 1, 2013 (gmt 0)

Hello,

I have some values that I am getting from a form users fill out. I am grabbing these values and storing them into sessions like so

$_SESSION['id'] = $_GET['id'];

Next, I'm storing those sessions into an array, like this

$array = array(
'model' => $_SESSION['model'],
'qty' => $_SESSION['qty'],
'id' => $_SESSION['id'],
);


I'm then trying to store that array, in another array. Like this

$other_array = Array(
"ID" => $array,
);



This works for the values that I submit from the form the first time around. When I submit different values from the same form I want it to create another array using the GET values and then store that in my $other_array.

Currently, it just replaces the first array that is added.

How can I get it to add another array rather than replacing it?

 

Matthew1980




msg:4550394
 12:06 pm on Mar 2, 2013 (gmt 0)

Hi there algalogica, and a very warm welcom to WebmasterWorld!

Firstly, are you aware that $_SESSION is in it's very nature an array anyway, anything that has: ['somevalue'] is a reference to an element within an array.

If you did Print_r($_SESSION); you would see what I mean.

The way you're trying to do this would require multidimensional arrays, so that you could create a sort of look-up table for your sessions and the values.

Have a quick google for that and I'm sure that you'll find lots of tips on that.

Cheers,
(from a very tired)
MRb

Readie




msg:4550507
 6:24 pm on Mar 2, 2013 (gmt 0)

Hey Algalogica,

I'm reading this as thinking every time the user submits this form, it adds to an array of previously submitted data?

In that case, you don't need to specify a specific key for your $other_array. You can use a variable key, or just let it decide on keys by itself:

<?php

// Let it decide it's own key.
// First submission has key 0
// Next has key 1 etc..
// print_r($other_array[1]);
$other_array[] = $array;

// Or you can use something posted in the form
$other_array[$_GET['id']] = $array;

?>


Also, hi Matt! :)

algalogica




msg:4550750
 11:44 pm on Mar 3, 2013 (gmt 0)

Thank you very much Matthew and Readie, these answers helped me solve my problem!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved