Jump to content

determine variable names for dynamic variables *RESOLVED*


Recommended Posts

heres the situation... a form is posted by a user saying i am bringing X number of guests with me to an event.... they are brought to a new page that have x number of boxes so tehy can enter in the name and email address of the people they are bringing......

how do i know how to get these variables back because i dont know what they are on the form because i dont know what X is....

the form i can just have it echo field_id=guest<?echo guest_num?> but once that is posted how do i know how many of those variables i am going to be looking for?  can i make the variables in my php code dynamic as in if($guest<?echo guest_num?>
How are they specifying the amount?  If they're doing it by a small text-area, just give it a name and then it's easy to do.  You can just access the number of people coming with them with $_POST['guests'] or whatever you choose to name the element.

Then just do
[code]

if(!empty($_POST['guests'])) {
$guests = $_POST['guests'];
$_SESSION['guests'] = $guests;
for($i = 0; $i < $guests; $i++) {
  echo "Guest " . $i . "<br />";
  echo "<input type=text size=25 name=" . $i . "><br />";
}
}
[/code]
The session var is just so you know how many names you'll be checking on the next page. 
http://207.5.19.133/sbqa/event_viewer.php?req=view_event&event_id=41

click the I want to go and then on the next page make the dropdown selector anything other then 0 and you will see whats going on....  right now those form values all have the same name... thats what i need to change but i need to do it in a manner so when i post that second form i know what variables im reading and what they are called....
[code=php:0]while(is_set($_GET['guestnum'.$i)) {
  do something with $_GET['guestnum'.$i];
  $i++;
}
[/code]

should work

you can also "eval" code to do what you were talking about:
[code=php:0]eval("if($guest".$guest_num");");[/code]
but eval can get pretty confusing
this isnt right because its not making me a new variable... its putting data in a variable...

i need to be able to know that $guest1_name exists and guest1_email exists and i need to be able to process all these variables until all the guests are through.... but if there were 100 guests then that means i would have to write the code out for 100 propeple and then counter to stop on time but thats a lot of bloat....  so im not trying to load data into a variable... im trying to make the variables on the fly.....
[code] case "proc_guests":
$i='1';
$guests=3;
$guestnum1="test";
$guestnum2="tesingt";
$guestnum3="tessafasfd sdft";
//$guests=mysql_real_escape_string($_POST['guests']);
while($i<=$guests){
echo "$guestnum.$i";
$i++;
}
break;[/code]

which is a variation of your example outputted=
.1.2.3

without the " is outputs 123
does anyoen have any alternative ideas for how i could work this code??  im just getting stuck on how i know what the name of the form fields im fetchign data from will be....  if there is a better way then let me know......
ok i got it... woohoo

[code] $i='1';
$guests=3;
$guestnum1="test";
$guestnum2="tesingt";
$guestnum3="tessafasfd sdft";
//$guests=mysql_real_escape_string($_POST['guests']);
while($i<=$guests){
echo ${guestnum.$i};
$i++;
}[/code]

returns what i need
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.