Re: Changing variable names in a while loop

From: Date: Fri, 28 May 2004 11:29:56 +0000
Subject: Re: Changing variable names in a while loop
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-187108@lists.php.net to get a copy of this message
"I.A. Gray" <tech@selectperformers.com> wrote in message news:GKENIOKKDEHBGNGOCJHNEEGBCGAA.tech@selectperformers.com... > Hi all, > > Easy question (I hope). My brain seems not be working today, so could > someone help me with this one? > > I have a while loop outputting part of an html form which inputs track > information for CDs: > > $countything = 1; > while ($countything !=31) { > echo " > <tr> > <td>$countything.</td> > <td><input name='fcomposer$countything' type='text' > id='fcomposer$countything' size='20' > maxlength='60'/></td> > <td><input name='fsubheading$countything' type='text' > id='fsubheading$countything' size='20' maxlength='80' > /></td> > <td><input name='fmovementno$countything' type='text' > id='fmovementno$countything' size='3' maxlength='3' > /></td> > <td><input name='ftracktitle$countything' type='text' > id='ftracktitle$countything' size='20' maxlength='60' > /></td> > <td><input name='ftracksubtitle$countything' type='text' > id='ftracksubtitle$countything' size='20' maxlength='80' > /></td> > <td><input name='ftracklength$countything' type='text' > id='ftracklength$countything' size='5' maxlength='8' > /></td> > <td><input name='fextrainfo$countything' type='text' > id='fextrainfo$countything' size='20' maxlength='80' > /></td> > </tr>\n"; > $countything++; > } > > > How can I get the value to change in each form? The name and id for each > input field will change from (for example) fcomposer1 to fcomposer30 but how > do I change the value for each input to show the variables taken from the > database which will be $composer1 to $composer30 ? Obviously if I put in > $composer$countything that will just output the value of $composer followed > by the value for $countything, but I want it to output the value for > $composer1 if $countything=1 or $composer10 if $countything=10 . Is there > something I can do with variable variables? I couldn't see any examples in > the PHP manual. What you want is this: $temp = 'composer' . $countything; Then use ${$temp} -> it will efectively be $composer1. Take a look here: http://de2.php.net/manual/en/language.variables.variable.php Regards, Torsten Roehr

« previous php.general (#187108) next »