ruraldev Posted December 10, 2014 Share Posted December 10, 2014 I have the following code which works (thanks to PHP Freaks members) foreach ($swap["sims"] as $swap) { echo $swap['voice'] . "</br>"; } But I can't figure out why the following doesn't work <select name="sim_for_swap[]"> <?php foreach ($swap["sims"] as $swap) { ?> <option value="<?php echo $swap['voice'];?>"><?php echo $swap['voice'];?></option> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/293011-foreach-to-create-dropdown-list-problem/ Share on other sites More sharing options...
CroNiX Posted December 10, 2014 Share Posted December 10, 2014 It looks like it should work, if your first example works. What's not working about it? Do you close the </select> tag after the options are created in the loop? Quote Link to comment https://forums.phpfreaks.com/topic/293011-foreach-to-create-dropdown-list-problem/#findComment-1499213 Share on other sites More sharing options...
WinstonLA Posted December 10, 2014 Share Posted December 10, 2014 (edited) Change name for second variable in the foreach foreach ($swap["sims"] as $swap) foreach ($swap["sims"] as $swap) This formatting looks better <select name="sim_for_swap[]"> <?php foreach ($swap["sims"] as $item): ?> <option value="<?php echo $item['voice']; ?>"> <?php echo $item['voice']; ?> </option> <?php endforeach; ?> </select> Edited December 10, 2014 by WinstonLA Quote Link to comment https://forums.phpfreaks.com/topic/293011-foreach-to-create-dropdown-list-problem/#findComment-1499214 Share on other sites More sharing options...
ruraldev Posted December 10, 2014 Author Share Posted December 10, 2014 The nest line was closing the </select> I tried the new format but still nothing appears in the dropdown list? Weird? Quote Link to comment https://forums.phpfreaks.com/topic/293011-foreach-to-create-dropdown-list-problem/#findComment-1499216 Share on other sites More sharing options...
WinstonLA Posted December 10, 2014 Share Posted December 10, 2014 (edited) Hm... Try this code foreach ($swap["sims"] as $item) { var_dump($item); } Something is outputting on the screen? If so, go back to your code and open (Ctrl+U) code of you page and find there your select tag. Will there be any options? Edited December 10, 2014 by WinstonLA Quote Link to comment https://forums.phpfreaks.com/topic/293011-foreach-to-create-dropdown-list-problem/#findComment-1499218 Share on other sites More sharing options...
ruraldev Posted December 10, 2014 Author Share Posted December 10, 2014 I got it, turned out I was changing the value of a variable earlier in the code as I was outputting the variable for testing Simple but rather silly mistake! Quote Link to comment https://forums.phpfreaks.com/topic/293011-foreach-to-create-dropdown-list-problem/#findComment-1499222 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.