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
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>
Edited by WinstonLA
Link to comment
Share on other sites

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 by WinstonLA
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.