Welcome to WebmasterWorld Guest from 50.19.34.234

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Variables, Arrays, and Multiple Loops - Oh my!

     

NoLimits

1:28 am on Oct 26, 2007 (gmt 0)

10+ Year Member



Okay...

I have a form where the user enters a number in an input field and hits submit.

Based on the number the user enters - a number of new textareas will become available on the following page. The new textareas are named in number order (tdata1,tdata2,tdata3,tdata4) - Now when this page is submitted is where I am encountering a problem.

I need to print the values of these textareas from the previous page, but since I don't have a way of knowing how many there are going to be, I assume it needs to be in an array of sorts. I'm currently using POST to send the variables.

... I had no clue how to approach this, so I tried to do the following:
variable nof is the nuber that was entered on the first page, and also t
he number of input fields that were created on the second page.


$nof = $_POST['fnum'];
$i=0;
while ($i < $nof){
$var1 = $_POST['tdata1'];
$i++
}

I need var1 to incriment also... $var2 for tdata2, $var3 for tdata3, and so forth...

Any help would be greatly appreciated... I would be more than happy to share the URLs and full code. Thanks for you time folks.

cameraman

6:58 am on Oct 26, 2007 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



It's really easier to just keep carrying it around as an array, but to answer your question, this is a job for variable variables [php.net].

$nof = $_POST['fnum'];
$i=1;
while ($i <= $nof){
$varname = "var$i";
$$varname = $_POST["tdata$i"];
$i++
}

You don't like for loops?
for($i = 1; $i <= $nof; $i++) {
$varname = "var$i";
$$varname = $_POST["tdata$i"];
}

A more concise yet maybe little harder to understand syntax is:
for($i = 1; $i <= $nof; $i++) {
${"var$i"} = $_POST["tdata$i"];
}

 

Featured Threads

Hot Threads This Week

Hot Threads This Month