homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Variables, Arrays, and Multiple Loops - Oh my!

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


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'];
while ($i < $nof){
$var1 = $_POST['tdata1'];

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.



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

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'];
while ($i <= $nof){
$varname = "var$i";
$$varname = $_POST["tdata$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"];

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved