Shattered Posted January 25, 2007 Share Posted January 25, 2007 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 68So I think either its A) Not possible, or B) coding it wrong. Quote Link to comment https://forums.phpfreaks.com/topic/35713-looping-in-an-array/ Share on other sites More sharing options...
owenjh Posted January 25, 2007 Share Posted January 25, 2007 To my knowledge its not possible,you would complete the task like this:[CODE]for ($x=0; $i < 30; $x++) { $array[$x] = "Shelf $x";}[/CODE] Quote Link to comment https://forums.phpfreaks.com/topic/35713-looping-in-an-array/#findComment-169211 Share on other sites More sharing options...
Orio Posted January 25, 2007 Share Posted January 25, 2007 And the answer is... B) The coding is wrong.This will do the job:[code]<?php$array = array();for($x=1; $x<31; $x++) $array[]="Shelf ".$x;?>[/code]Orio. Quote Link to comment https://forums.phpfreaks.com/topic/35713-looping-in-an-array/#findComment-169213 Share on other sites More sharing options...
Shattered Posted January 25, 2007 Author Share Posted January 25, 2007 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] Quote Link to comment https://forums.phpfreaks.com/topic/35713-looping-in-an-array/#findComment-169224 Share on other sites More sharing options...
owenjh Posted January 25, 2007 Share Posted January 25, 2007 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] Quote Link to comment https://forums.phpfreaks.com/topic/35713-looping-in-an-array/#findComment-169233 Share on other sites More sharing options...
Shattered Posted January 25, 2007 Author Share Posted January 25, 2007 Fantastic, thank you much, exactly what I wanted. Quote Link to comment https://forums.phpfreaks.com/topic/35713-looping-in-an-array/#findComment-169234 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.