Jump to content

Looping in an Array


Shattered

Recommended Posts

Is it possible to make a loop inside of an array?

IE:

[code]
array(for($x=1;$x<31;$x++) {echo "Shelf $x";});
[/code]

I have tried this already in my code, and I get:
Parse error: parse error, unexpected T_FOR, expecting ')' in C:\Server\wamp\www\update.php on line 68

So I think either its A) Not possible, or B) coding it wrong.
Link to comment
https://forums.phpfreaks.com/topic/35713-looping-in-an-array/
Share on other sites

Cool that got rid of the error.  Now Im just trying to get them all into a combo box.  Code runs fine, the combo box pops up, but the array data isnt in it.

[code]
$loc=$row['PC_LOC'];
<tr><td>Location</td><td><select name='status'>
<?php
    $location = $array = array();
for($x=1; $x<31; $x++)
    {$array[]="Shelf ".$x;}

foreach($location as $locat)
{
  if($locat == $loc)
{ echo "<option selected>".$locat."</option>"; }
else
{ echo "<option>".$locat."</option>"; }
}
?> </select></td></tr>
[/code]

Link to comment
https://forums.phpfreaks.com/topic/35713-looping-in-an-array/#findComment-169224
Share on other sites

I believe you want the shelf number in the combo box correct?

you would do this as followed:

[CODE]
$loc=$row['PC_LOC'];
<tr><td>Location</td><td><select name='status'>
<?php
    $location = array();
for($x=1; $x<31; $x++)
    {$location[]="Shelf ".$x;}

foreach($location as $locat)
{
  if($locat == $loc)
{ echo "<option selected>".$locat."</option>"; }
else
{ echo "<option>".$locat."</option>"; }
}
?>
[/CODE]
Link to comment
https://forums.phpfreaks.com/topic/35713-looping-in-an-array/#findComment-169233
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.