[nycphp-talk] accessing $_SESSION elements
Mark Armendariz
nyphp at enobrev.com
Fri Oct 24 19:00:13 EDT 2003
Well, you could do it like:
Form input names:
fair_name[]
fair_location[]
fair_date[]
Then on the processor, the array size will be the size of passed vars. So
if they only fill out 3 sets, the array will actaully be 3 large. Hence, no
checking for empties. The empties aren't passed (not sure if that's http or
php, but empties don't get passed).
Then you can loop through and continue:
foreach($_POST['fair_name'] as $index => $value) {
echo '<b>Fair ' . ($index + 1) . '</b><br />';
echo 'name: ' . $_POST['fair_name'][$index] . '<br />';
echo 'location: ' . $_POST['fair_location'][$index] . '<br />';
echo 'date: ' . $_POST['fair_date'][$index] . '<br />';
}
If you need the indices to be specific numbers, (Ie starting at 1 instead of
zero) you can set the form inut names as fair_name[1], fair_name[2], etc.
Mark
-----Original Message-----
From: talk-bounces at lists.nyphp.org [mailto:talk-bounces at lists.nyphp.org] On
Behalf Of Aaron Fischer
Sent: Friday, October 24, 2003 6:40 PM
To: NYPHP Talk
Subject: Re: [nycphp-talk] accessing $_SESSION elements
Hi Robert,
I do have an array of data, let me backtrack to explain:
In the first form a user enters name, etc. and how many fairs they want to
enter.
On post, a loop iterates set number of times which is equal to the number of
fairs they have chosen.
On each iteration, the loop spits out the html form fields that enables the
user to enter each fair, i.e. 1_fair_name, 1_fair_location, 1_fair_date,
2_fair_name, 2_fair_location, 2_fair_date, etc. The 1, 2, 3, etc.
distinguish each fair. If I was creating a static html form I would be
naming the fields with the same convention. Here I am just using php to
create the fields.
I did this to make the interface more appealing, so the user doesn't have to
wade through a long form with all of the fields for 10 different fairs if
they only want to enter 1.
Then on submit I am looping the set number of times again and testing to see
if the array elements are empty. If any element is empty I will reload the
form with appropriate error messages.
On typing this I realize that I don't need to test to see if array elements
are there. I know what is there, based on the number of fairs they have
chosen, which is stored as an array element as well. I just want to test to
see if said fair elements are empty.
I have been reading through the array functions but hadn't found a good way
to say "Does array key "foo" exist? and if it does, is it empty?".
I could very well be blind at this point.
-Aaron
On Friday, October 24, 2003, at 05:57 pm, Robert Redcay wrote:
> Aaron,
> It seems like what you really want is an array of data. If you have
> your form submit the individual fairs as elements of an array
> ($fairs), you'll find php's array functions very helpful when
> determining what indeces are set. It seems that your prepending an
> integer to the string 'fair_name' is just a poor man's array which
> makes your looping tests akward.
>
> Best,
> Robert Redcay
_______________________________________________
talk mailing list
talk at lists.nyphp.org
http://lists.nyphp.org/mailman/listinfo/talk
More information about the talk
mailing list