dongll0502
dongll0502
2013-05-21 11:04
浏览 63
已采纳

PHP从数据库中获取值到selectbox

I have a problem to show my database values into a select box.

Here is my code

<select name="bugsolver">
<?php 
  if(count($yourBugs) > 0) 
  {
    foreach( $emails as $key=> $singleEmail)
    { ?>
  <option value="<?=$singleEmail['email']?>" selected='selected'> <?php echo $singleEmail['email']?></option>";

<?php }
} ?>

</select>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • drasv0904
    drasv0904 2013-05-21 11:24
    已采纳

    Your code should look like this:

    <select name="bugsolver">
      <?php 
      if(count($yourBugs) > 0):
        foreach( $emails as $key=> $singleEmail ):
      ?>
          <option value="<?php echo $singleEmail['email']; ?>"> 
            <?php echo $singleEmail['email']; ?>
          </option>
    
      <?php
        endforeach;
      endif;
      ?>
    </select>
    

    When your code is easy to read (at least for you) it's easier to find bugs. If above code still doesn't work, do var_dump($yourBugs) and var_dump($emails) to check if these values are properly set. You can have disabled short tags (<?=) or variables are just empty.

    点赞 评论
  • drdu53813
    drdu53813 2013-05-21 11:24

    Your code seems be OK. Try to test for $emails instead $yourBugs to get more info:

    <?php if (count($emails)) : // test if $emails have values ?>
    <select name="bugsolver">
        foreach( $emails as $key=> $singleEmail) { /* ... */ }
    </select>
    <?php else : ?>
        There are no emails to select ($emails is empty)
    <?php endif; ?>
    
    点赞 评论

相关推荐