Putting Data into a PHP ARRAY from a Form

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
maboroshi
Forum Commoner
Posts: 25
Joined: Thu Nov 13, 2003 10:10 am

Putting Data into a PHP ARRAY from a Form

Post by maboroshi »

Hi I figured this would be more of a theory question and not a coding question

I don't want to use a file or a database this is more an experiment and I wanted some feedback

What I would like to do is use a form what this form does is when you submit the data instead of putting it into a database the data goes into an array

eg: if I submit a value name and the name is Andrew then the array looks like this name = array("Andrew") but when I submit the field again with a different name I would like the code to then look like this

name = array("Andrew", "Another Name") and so on

I understand that this might not be a practical idea. Has anyone had an idea like this before?

What would I need to do in order to accomplish this. I am not looking for an answer I am looking for some guidance

Cheers

Maboroshi
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Without a way to save it, there's no way to keep the data from the previous person.

My suggestion would be save it to a text file. maybe seperate the entries with a character that wouldnt be typed in (say ~), then use explode() to re-create the array when you need to use it after reading it from the file (the string).

So file.txt might look like:
Andrew~steve~Fred~Justin
and the array would be:
$array[0] = "Andrew";
$array[1] = "Steve";
//etc
EricS
Forum Contributor
Posts: 183
Joined: Thu Jul 11, 2002 12:02 am
Location: Atlanta, Ga

Life of the data

Post by EricS »

PHP is what's known as a stateless programming language. Basically that means every single page that runs, even the exact same page run over and over, has NO idea what any other page has ever done.

The reason is that (and I may not be exactly correct on this but the idea is still close enough so don't start flaming) basically PHP is designed to run every single page as a completely seperate application. Since each page, even the same page run again, is a seperate application, it can't automatically remember what you did from page to another. Okay, so that's why it's considered stateless.

Now, just because it can't automatically remember things from one page to another automatically, doesn't mean it can't pass data, it just needs a little help. Enter: Query Strings, Sessions, Databases, Files, and a few others.

So you want to pass data from one page to another in an array, and you don't wanna do it in a file or a database, you can either pass it as a query string or a session variable.

By putting the contents of the array into the query string portion of a URL or inside a form, you can pass the data from page to page. The downside, is that every single page must pass the data to the next. As soon as you don't inject the data into the query string its gone.

The other solution is to just put the array into a session variable. Session variables are special variables that do live on after a page has finished running. Normally that data is stored in a temporary file on the server and when the session variable is called from another page, it's retrieved from that file.

Even sessions have draw backs though. More resources are taken up on the server and even session variables are temporary. If a session times out or a user closes their browser, that data is gone. And again we are back to PHP's stateless nature.

Now keep in mind, both query strings and sessions are user dependent. So if one person submits a form and you save the data in a query string or a session and then a different person submits the form, there is no way to retrieve the data from the other persons submission. Sessions and query strings are tied to single people. So data can't be colated across many users.

If you want permanent storage of the data, you HAVE to use a database or file or some web service, but PHP is stateless and will not permanently store it's data on it's own.

I hope I went in the direction you were looking, if not post again with a little more information about the problem your trying to solve.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Actually.. I just thought of something..

What if you were to set the timeout on inactive sessions to never (or close to it :P), then find a way to assign the same session to EVERYONE that visits the site, that way they can all contribute to the same session array?
maboroshi
Forum Commoner
Posts: 25
Joined: Thu Nov 13, 2003 10:10 am

Hey

Post by maboroshi »

Hey that helps a lot and makes sense to me

I just wanted to explore new ideas on programming

and had no real problem in mind

but I like LilpunkSkateR' idea about setting a session to (close to never) timeout and then assign the same session to everyone that visits the site

That would be interesting could you explain this more
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Practically, the code could look like this:

Code: Select all

$array=array("first_name"=>"Eric","last_name"=>"Hobsbawm");
$array1=array("first_name"=>"Shula","last_name"=>"Marks");
$array2=array("first_name"=>"Norbert","last_name"=>"Elias");
$array3=array("first_name"=>"Nigel","last_name"=>"Worden");

$new_array  =   array_merge_recursive($array,$array1,$array2,$array3);

echo "<pre>";print_r($new_array);echo"</pre>";

As said above, you will need a session for that if you refrain from using a database or file.

Code: Select all

if(!empty($_POST)){
$_SESSION["form"]=array_merge_recursive($_SESSION["form"],$_POST);
}
Now, the only thing left would be, as LiLpunkSkateR said, to set the session-cache to as close to "never expire" as you can with [php_man]session_cache_expire[/php_man] (afaik you can't set it to unlimited, though) and then use [php_man]session_regenerate_id[/php_man] to force a "regeneration" of the session. You would need a cron-job to call the script that does the session-regeneration to keep it, and the data, alive, I would think.

That's pretty much all off the top of my head just before bedtime ;)
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

Aren't sessions normal kept in files? Unless of course your talking about using shared memory to save the sessions? That would avoid the use of files.
Post Reply