Jump to content

foreach to create dropdown list problem


ruraldev

Recommended Posts

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
}
?>
Link to comment
https://forums.phpfreaks.com/topic/293011-foreach-to-create-dropdown-list-problem/
Share on other sites

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>

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?

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.