Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating var within a loop!??

Status
Not open for further replies.

fireburner69

Programmer
Aug 22, 2002
108
GR
OK my question is is there a way to generate the variable names with in a loop! for example
I have send the vars field1 and field2 and I want in the display to show what are tha vars
so i want to use value=$field1 but i want within the loop to generete the numbers according to the $cellz for example if I have $cellz= "10" to make automatic tha values from $field1 to $field10
I used something like $\"field$num\" but always comes up $"field1"
Can someone help me out :)

$num = "1";
while ($num <= $cellz) {
echo &quot;<p>&nbsp;&nbsp;$stoixeia<b>&nbsp;</b><b>$num</b>&nbsp;&nbsp;
<input type=\&quot;text\&quot; name=\&quot;field$num\&quot;>&quot;;
$num = ($num + &quot;1&quot;);
echo &quot;&nbsp;&nbsp;$stoixeia<b>&nbsp;</b><b>$num</b>&nbsp;&nbsp;
<input type=\&quot;text\&quot; name=\&quot;field$num\&quot;>
</p>&quot;;
$num = ($num + &quot;1&quot;);
};
 
hozac is right, using arrays will be much easier. Read the information at the link he showed you and then try this:

$fields = array(&quot;field1value&quot;,&quot;field2value&quot;,...etc);

$num = 1;

foreach ($fields as $field) {

echo &quot;<p> $stoixeia<b> </b><b>$num</b>
<input type=\&quot;text\&quot; name=\&quot;$field\&quot;>&quot;;

$num++;

}

The foreach statement is a loop that is the equivelent to:

while ($num <= $x)

where $x is the total number of elements in the array. Except each time through the loop, the foreach statment sets the next element in the array to a specified variable, $field, in this case. Celia
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top