Creating Variables from unpreset number of data.
Greetings to all.
I have a problem in coding as was hoping for some help.
In form1/page1, we ask the person to enter a random number, say number of friends; could be from 1 to infinity.
For the example let’s say the user enters the number 5.
In the next page, there’s a form where we ask the user to enter data for each friend separately. The name, the gender, the age, the e-mail etc.
So I create a random number of variables by using the $friends_number data (in this case: 5) and giving the appropriate names to the form elements such as input by using a loop that created the form elements names with a number added to the end of them, starting from 1 to 5 (or to any given number given by the user). So, in this example we have form elements names as:
[php]For ($i = 1 ; $i <= $friends_total_number ; $i++)
{
echo (“<input name=friend_name” . $i . “[…]”);
…
…
}[/php]
Which in the next page will create variables such as:
$friend_name1, $friend_name2 …
$friend_gender1, $friend_gender2 …
$frined_email1, $friend_email2…
etc…
Now comes the tricky part where help is needed.
On the 3rd and final page, we need to process the given data. And to do that, we need to load the data from variables. But since the variables were created by unknown amount of number in earlier stage, we do not know how many times to run the loop. Easily solved by a “for each” loop and/or by passing the a $friends_total_number variable which in this case has stored the number 5.
But!..
How can I tell in a loop:
[php]for ($i=1 ; $i <=$friends_total_number ; $i++)
{
echo ($friend_name#); /where #= $i/
}[/php]
Where # is the number. Remember, we could have variables named $friend_name1 to $friend_name(n~).
In simple words. How can I CONSTRUCT the variable name by taken the existing variable name and adding to (or renaming it) by adding data to it. So that the variable $friend_name1 can change to $friend_name2, etc?!
Same goes for the gender, email and other data assigned to each $friend_
And here I’m blocked… I don’t know how to proceed from here. Any help would be much appreciated. ???
Note: The data is passed on with POST method and some stored in hidden inputs, no database or file is used to store the data (at least, not yet).