JJBlaha Posted October 26, 2006 Share Posted October 26, 2006 i want to make a variable with a variable name in it. something like$n = 1;while (whatever){$b = $Array['b']; ${$n}a = $b$n++}then later on in the script to get the value of $b at the first run of the while statement i would use$1aandecho "$5a'; should output the fifth part of $Array['b']; Is there any way i can do this? Quote Link to comment https://forums.phpfreaks.com/topic/25234-variables-in-variable-names/ Share on other sites More sharing options...
kenrbnsn Posted October 26, 2006 Share Posted October 26, 2006 You want to use variable variables. Read the [url=http://www.php.net/manual/en/language.variables.php]section[/url] on variables. Variable names can not start with a number.Ken Quote Link to comment https://forums.phpfreaks.com/topic/25234-variables-in-variable-names/#findComment-115068 Share on other sites More sharing options...
Orio Posted October 26, 2006 Share Posted October 26, 2006 I am sorry, but a variable name cannot start with a number as far as I know.Orio. Quote Link to comment https://forums.phpfreaks.com/topic/25234-variables-in-variable-names/#findComment-115069 Share on other sites More sharing options...
JJBlaha Posted October 26, 2006 Author Share Posted October 26, 2006 I forgot that, then something along the lines of$n = 1;while (whatever){$b = $Array['b']; $a{$n} = $b$n++}echo "$a5';should output the fifth part of $Array['b']; Quote Link to comment https://forums.phpfreaks.com/topic/25234-variables-in-variable-names/#findComment-115071 Share on other sites More sharing options...
kenrbnsn Posted October 26, 2006 Share Posted October 26, 2006 You can do something like that, but why would you want to? You can access the fifth part of your example array by doing:[code]<?php echo $b[5] ?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/25234-variables-in-variable-names/#findComment-115073 Share on other sites More sharing options...
JJBlaha Posted October 26, 2006 Author Share Posted October 26, 2006 sorry if i was not clear enough, ill try to explain exactly what i need to do.<?$n1 = 0; include('dbconn.php'); $sql = "select * from formfields WHERE category='$category' order by formfield"; $result = @mysql_query($sql, $dbh); while ($Array = @mysql_fetch_array($result)){ $formfield = $Array['formfield']; $n1 = ($n1 + 1); echo "<tr><td>$formfield: <br><select name=\"title$n1\">"; $sql2 = "select * from formoptions WHERE formfield='$formfield' order by formoption"; $result2 = @mysql_query($sql2, $dbh); while ($Array = @mysql_fetch_array($result2)){ $formoption = $Array['formoption']; echo "<option name=\"$formoption\">$formoption</option>"; } echo "</select></br>"; } ?> which creates a bunch of forms that looks like ExampleForm1: <select name="info1"> bunch of options </select> ExampleForm2: <select name="info2"> bunch of options </select> ExampleForm3: <select name="info3"> bunch of options </select> etc... the variable $formfield is the name of the form. in this case ExampleForm1, ExampleForm2, etc after the user has filled in the forms i want to input both the name of the form and the user input into a database. so for each form made i would do something similiar to <? $info = $_POST['info1']; $formfield = ??????; $sql3 = "insert into forms (title, info) values ('$formfield', 'info')"; ?> I hope this makes it clearer :) Quote Link to comment https://forums.phpfreaks.com/topic/25234-variables-in-variable-names/#findComment-115080 Share on other sites More sharing options...
sasa Posted October 27, 2006 Share Posted October 27, 2006 change <select name=\"title$n1\">"; to <select name=\"title[$formfield]\">";in result page try[code]foreach ($_POST['title'] as $key => $value) echo "In $key is selected $value<br />";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/25234-variables-in-variable-names/#findComment-115274 Share on other sites More sharing options...
JJBlaha Posted October 27, 2006 Author Share Posted October 27, 2006 Thanks alot, thats exactly what i needed :). Quote Link to comment https://forums.phpfreaks.com/topic/25234-variables-in-variable-names/#findComment-115529 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.