interpim Posted September 1, 2008 Share Posted September 1, 2008 So, here is the problem I need to find a solution for... I have a form, which entry into is repetitive, although not all parts of the form will be required to be filled in. basically i have the following values. alt1, class1 alt2, class2 alt3, class3 alt4, class4 etc... so I want to cycle through these, and assign these to variables when the page is processed, if they contain data. How can I build a variable name using for each of these without physically typing a block of code for each numbered name since the type of data will be the same. So far I have this $j = 1; while($j < 10){ $var = "alt" . $j; #This is where I cannot figure out how to get the value in $var to actually be the variable... $j++; } Am I on the right track? and if so, what am I missing here? Link to comment https://forums.phpfreaks.com/topic/122226-dynamic-variables/ Share on other sites More sharing options...
PFMaBiSmAd Posted September 1, 2008 Share Posted September 1, 2008 Use an array. Variable variables are 3 times slower than an array and the code to use variable variables is more complicated than using an array. If you post your actual form, someone can help with how to use it with arrays. Link to comment https://forums.phpfreaks.com/topic/122226-dynamic-variables/#findComment-631069 Share on other sites More sharing options...
interpim Posted September 1, 2008 Author Share Posted September 1, 2008 <form method='post' action='build_userpage.php'> SERVER: <input type='text' name='server'> Main Character: <input type='text' name='main'>Class: <input type='text' name='main_class'> Alt: <input type='text' name='alt1'>Class: <input type='text' name='alt1_class'> Alt: <input type='text' name='alt2'>Class: <input type='text' name='alt2_class'> Alt: <input type='text' name='alt3'>Class: <input type='text' name='alt3_class'> Alt: <input type='text' name='alt4'>Class: <input type='text' name='alt4_class'> etc..... <input type='submit' value='create'></form> Link to comment https://forums.phpfreaks.com/topic/122226-dynamic-variables/#findComment-631070 Share on other sites More sharing options...
kenrbnsn Posted September 1, 2008 Share Posted September 1, 2008 Change to <form method='post' action='build_userpage.php'> SERVER: <input type='text' name='server'> Main Character: <input type='text' name='main'>Class: <input type='text' name='main_class'> Alt: <input type='text' name='alt[1]'>Class: <input type='text' name='alt1_class'> Alt: <input type='text' name='alt[2]'>Class: <input type='text' name='alt2_class'> Alt: <input type='text' name='alt[3]'>Class: <input type='text' name='alt3_class'> Alt: <input type='text' name='alt[4]'>Class: <input type='text' name='alt4_class'> etc..... <input type='submit' value='create'></form> Then you can use <?php for ($i=1;$i<5;$i++) echo $_POST['alt'][$i] . '... '; ?> Ken Link to comment https://forums.phpfreaks.com/topic/122226-dynamic-variables/#findComment-631089 Share on other sites More sharing options...
interpim Posted September 1, 2008 Author Share Posted September 1, 2008 thank you Ken, let me run through this for a bit and see how it works out. Link to comment https://forums.phpfreaks.com/topic/122226-dynamic-variables/#findComment-631095 Share on other sites More sharing options...
PFMaBiSmAd Posted September 1, 2008 Share Posted September 1, 2008 Ever easier (does not care how many there are) - <?php foreach($_POST['alt'] as $key => $value) echo $key . $value; ?> Link to comment https://forums.phpfreaks.com/topic/122226-dynamic-variables/#findComment-631117 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.