essjay_d12 Posted April 21, 2008 Share Posted April 21, 2008 Hi, I want to be able to add my $i to variables within a for loop (to creat multiple variables - the number of variables is dependant on the number of rows from a database - i.e. $final) $final will be generated from a number of rows from a database - so will be unknown. The below code didnt work - how do I go about adding a number to variables giving me the correct amount of variables $i=1; $final = 10; for($i; $i>$final; $i++) { $name.$i = $_POST['name.$i']; $number.$i = "$_POST['number.$i']; } echo $name2; echo $name3; Any ideas? Cheers S Quote Link to comment https://forums.phpfreaks.com/topic/102118-for-loop-help-needed/ Share on other sites More sharing options...
kenrbnsn Posted April 21, 2008 Share Posted April 21, 2008 If you want to do that, you should look into using arrays: <?php $final = 11; $name = array(); for($i=1; $i<$final; $i++) { $name[$i] = $_POST['name'.$i]; $number[$i] = $_POST['number'.$i]; } echo $name[2] . '<br>'; echo $name[3]; ?> Also, variables inside single quotes are not evaluated. Ken Quote Link to comment https://forums.phpfreaks.com/topic/102118-for-loop-help-needed/#findComment-522748 Share on other sites More sharing options...
jonsjava Posted April 21, 2008 Share Posted April 21, 2008 Kinda right, but you still have the issue that you can't do this: <?php $_POST['name'.$i]; ?> I changed it to a while loop, but the principle is the same: <?php $name = array(); $number = array(); $i=1; $final = 10; while ($i < 10) { $name[$i] = $_POST['name'].$i; /*print "part ".$i."done\n"; Edited to comment out error checking */ $number[$i] = $_POST['number'].$i; ++$i; } print_r($name); print_r($number); ?> Quote Link to comment https://forums.phpfreaks.com/topic/102118-for-loop-help-needed/#findComment-522759 Share on other sites More sharing options...
sasa Posted April 21, 2008 Share Posted April 21, 2008 $i=1; $final = 10; for($i; $i>$final; $i++) { ${'name'.$i} = $_POST['name.$i']; ${'number'.$i} = "$_POST['number.$i']; } echo $name2; echo $name3; Quote Link to comment https://forums.phpfreaks.com/topic/102118-for-loop-help-needed/#findComment-522773 Share on other sites More sharing options...
kenrbnsn Posted April 21, 2008 Share Posted April 21, 2008 Yes, you can do: <?php $_POST['name'.$i]; ?> If you have a form with something like the following; <input name="name1"><br> <input name="name2"><br> <input name="name3"> Please give an example of the form that would support your code. Ken Quote Link to comment https://forums.phpfreaks.com/topic/102118-for-loop-help-needed/#findComment-522776 Share on other sites More sharing options...
jonsjava Posted April 21, 2008 Share Posted April 21, 2008 hadn't thought of it that way. good point. I just don't know many people who do it that way Quote Link to comment https://forums.phpfreaks.com/topic/102118-for-loop-help-needed/#findComment-522779 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.