Page 1 of 1

Dynamicly create variables??

Posted: Sun Jan 30, 2005 5:02 pm
by nirus
Ok I want to dynamicly create variables for posted data. We do not know how many variables there are going to be... could be 10 or 62.

You can see what I am trying to do which obviously will not work ... $host$i embeded into a while statement while stepping $i by 1

Code: Select all

//here is the value we do not know... could be 2 or 63 we are just unsure
$hostlines= $_POSTї'hostlines']; 

$i = 1;
while ($i < $hostlines)&#123;
        $host$i = $_POST&#1111;'host$i']; 
         $i++;
                 &#125;
//this is what I want as a result
echo "$host1, $host2, $host3, $host4,";



Basically I am trying to automate this:
$host1 = $_POST['host1'];
$host2 = $_POST['host2'];
$host3 = $_POST['host3'];
$host4 = $_POST['host4'];


hostlines is comming from <input's> on a form. all the inputs have dynamicly created name="host1" or name="host8"

You deserve a gold star if you can make this happen! :D
- Nirus

Posted: Sun Jan 30, 2005 5:31 pm
by magicrobotmonkey

Code: Select all

//here is the value we do not know... could be 2 or 63 we are just unsure
$hostlines= $_POST['hostlines'];

$i = 1;
while ($i < $hostlines){
         $name = "host".$i;
         $$name = $_POST[$name];
         $i++;
                 }

Posted: Sun Jan 30, 2005 9:58 pm
by protokol
This is what the extract() function is for.

Posted: Sun Jan 30, 2005 11:16 pm
by magicrobotmonkey
gew I hate extract().

Posted: Sun Jan 30, 2005 11:18 pm
by feyd
the smart-er extract I wrote and posted for several someones may work.. however.. if the fields were named "host[]" they'd come in as an array, so they would be simple to access, without any additional parsing..