markbett Posted September 15, 2006 Share Posted September 15, 2006 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?> Quote Link to comment https://forums.phpfreaks.com/topic/20937-determine-variable-names-for-dynamic-variables-resolved/ Share on other sites More sharing options...
Wintergreen Posted September 16, 2006 Share Posted September 16, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/20937-determine-variable-names-for-dynamic-variables-resolved/#findComment-92782 Share on other sites More sharing options...
markbett Posted September 16, 2006 Author Share Posted September 16, 2006 http://207.5.19.133/sbqa/event_viewer.php?req=view_event&event_id=41click 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.... Quote Link to comment https://forums.phpfreaks.com/topic/20937-determine-variable-names-for-dynamic-variables-resolved/#findComment-92787 Share on other sites More sharing options...
markbett Posted September 16, 2006 Author Share Posted September 16, 2006 any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/20937-determine-variable-names-for-dynamic-variables-resolved/#findComment-92846 Share on other sites More sharing options...
michaellunsford Posted September 16, 2006 Share Posted September 16, 2006 [code=php:0]while(is_set($_GET['guestnum'.$i)) { do something with $_GET['guestnum'.$i]; $i++;}[/code]should workyou 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 Quote Link to comment https://forums.phpfreaks.com/topic/20937-determine-variable-names-for-dynamic-variables-resolved/#findComment-92850 Share on other sites More sharing options...
markbett Posted September 16, 2006 Author Share Posted September 16, 2006 arent your quotes in the wrong places??shoulndt it read$_POST['$guestnum.$i'] witht the quotes outside both vars? Quote Link to comment https://forums.phpfreaks.com/topic/20937-determine-variable-names-for-dynamic-variables-resolved/#findComment-92854 Share on other sites More sharing options...
michaellunsford Posted September 16, 2006 Share Posted September 16, 2006 in standard strings, it should work either way. as an array pointer, not so sure. To be safe, I always attach variables outside -- but that's just me. Quote Link to comment https://forums.phpfreaks.com/topic/20937-determine-variable-names-for-dynamic-variables-resolved/#findComment-92857 Share on other sites More sharing options...
markbett Posted September 16, 2006 Author Share Posted September 16, 2006 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..... Quote Link to comment https://forums.phpfreaks.com/topic/20937-determine-variable-names-for-dynamic-variables-resolved/#findComment-92858 Share on other sites More sharing options...
markbett Posted September 16, 2006 Author Share Posted September 16, 2006 [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.3without the " is outputs 123 Quote Link to comment https://forums.phpfreaks.com/topic/20937-determine-variable-names-for-dynamic-variables-resolved/#findComment-92862 Share on other sites More sharing options...
markbett Posted September 16, 2006 Author Share Posted September 16, 2006 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...... Quote Link to comment https://forums.phpfreaks.com/topic/20937-determine-variable-names-for-dynamic-variables-resolved/#findComment-92878 Share on other sites More sharing options...
markbett Posted September 16, 2006 Author Share Posted September 16, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/20937-determine-variable-names-for-dynamic-variables-resolved/#findComment-92886 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.