Disappearing Array


#1

Hi all! Thanks for being there.

I’m trying to write a procedure that will collect a person’s name & contact info, then store a bunch of related items in an array. When they’re done adding items, I want to save the data to a couple of tables in a MySQL db.

My problem is the Array with the ‘related items’ vanishes.

I’m saving the ‘owner’ info in variables like $_SESSION[‘fieldname’] where fieldname = the input from the form. That’s all cool, it stays where it should and life is good.

When I establish the array for the related items, however, I can’t go very far before it’s missing and I get the “Undefined index” message.

Here’s a quick & dirty rundown of what I’m doing:

At the top of the script I have a
[php]<?
session_start();

//Then this tells us what we’re doing each time we come back to the script…

if (!isset($_SESSION[‘action’]))
{
$_SESSION[‘action’] = “getpersoninfo”; // The first thing to do is get personal info
}
else
{
$_SESSION[‘action’] = $_REQUEST[‘action’]; // Do whatever is passed with the next ‘call’ to this same script
}

// Works fine up to here

if (!isset($_ITEMS)) // Establish the Array for Items. IS THIS RIGHT?
{
$_ITEMS[] = array();
$_ITEMS[0] = “x”;
echo $_ITEMS[0];
}

// It will display x here (on the first pass) no problem.

// If after filling in the Personal Info, and entering an Item Number on forms within the script (not shown here), the site user says “yes, I want this”, then this should commit the item to the array, right?

if ($_REQUEST[‘action’] = “additem”)
{
$_ITEMS[] = $thisitem; // I would think this would add the $thisitem to the $_ITEMS array - right?
}

// Unfortunately, the array $_ITEMS is gone by the second time we’re coming down the script - so we can’t append to it!

switch $_SESSION[‘action’]
{
case “getpersonalinfo”:
// code for the form to get info, with a form action to come back to this script ?action=getitems
break;;

case “getitems”:
// code for the form to get an item code with a form action to come back to this script ?action=confirm
break;;

case “confirm”;
/* code to check that the item exists in the database (yes we’re hooked up to the db & all error checking is handled). Then there are links to allow them to
1)confirm it’s what they want & add more, or
2)confirm it’s what they want & done, or
3)bail out & try again.
*/
break;;

/* If they choose either of the first two options - the script calls itself ?action=additem - meeting the If() earlier in the script … but sadly, the array is gone, so no item is ADDED.
*/

}
?>[/php]

Any help or insights you can provide would be GREATLY appreciated!!!

**MOD EDIT - Added [ php ] tags for readablity.


#2

I would recommend renaming the array. I am not exactly sure on if this would cause problems or not, but usually Super Globals use the $_var format.

try something like $item = array();
then to add an item to the array I would use - array_push($item, “newitem”);

http://www.php.net is a great place to look if you need help with these functions.

I think that should help.


#3

I presume by saying “Second time through” you mean it’s called as if in a form or something. So to get data from the Previous iteration, you must pass it to the next one somehow. Are you passing it? (if so how?)

Sessions are are stored on the server and the browsers (generally) create a unique id for the session for recall later. So that would be a good place to store it as well. You could create the items array as a session variable. (I believe it would now be a multidimensional array).

Instead of $_ITEMS (which in itself is not really good because that style is typically reserved for GLOBAL variables) you could use

$_SESSION[‘items’][‘itemnumber’] to store it. (I could be wrong with this but I think it should work.)

That way it gets passed in the sessions.


#4

Thanks for the quick reply!

OK - that’s the ticket … I’m not ‘passing’ the array. Thank you!

No, I’m not storing it in the $_SESSION array right now, and have never been brave enough to try a multidimensional array, but it looks like that’s where I’m headed.

When I said ‘second time’ it’s because this script does something, then calls itself with a different ‘action’, does something, calls itself with a different ‘action’, etc.

I don’t suppose I could eliminate the need for putting an array within an array by just declaring the Array ‘Global’, could I?


#5

No not if you are calling the page again. It doesn’t work that way.

Global only changes the context within a script, that it’s available to such as functions or methods.

Another alternative you could consider would be to have a “Temporary” table in a database to keep everything. When it’s completed, then remove those items. You could use a “Permanent” table as a “Temporary” storage (probably easier if you are not good with SQL) or you could actually create a literally “Temporary” table on the fly.

Finally, if you are not passing the array, then you need to (Perhaps as a hidden value or in the URL). Obviously, depending on the size of the array, that could make things very large which would slow down loading of pages.


#6

Global only changes the context within a script, that it’s available to such as functions or methods.

OK - understood. I don’t like it - but too bad for me.

Another alternative you could consider would be to have a “Temporary” table in a database to keep everything.

Yes, a good possibility - but I think I’ll take Option #3 … rewrite the “add items” routine to be completely contained in a WHILE construct, so I can keep the Items Array cookin’ on its’ own until the site visitor says “done.” “Done” will bounce us out of the WHILE loop and I’ll just continue down the script to commit the contact info and the items to their respective db tables before the Array goes away.

Thanks again for your help … this was pretty frustrating until I could find someone who A) understood and B) was smart enough to be able to help!

Have an outstanding weekend!