简体   繁体   English

从SQL数据库中选择HTML选择列表

[英]Selecting html select list from sql database

I kind if have an interesting question. 我有一个有趣的问题。 I'm trying to prefill an edit form in codeignitert. 我正在尝试在codeignitert中预填一个编辑表单。 I have most of the values coming through the database, but my select box is giving me a few issues. 我的大部分值都来自数据库,但是我的选择框却给了我一些问题。 I want it to only highlight what they had previously selected not erase the list and fill it in with what they selected...if that makes any sence. 我希望它仅突出显示他们先前选择的内容,而不是擦除列表,并用他们选择的内容进行填充...如果可以的话。

Here is my html 这是我的html

<?foreach($info as $row) :?>
 <select multiple="multiple" class="multi" name="wGenre[]">
   <option value="Action/Adventure">Action/Adventure</option>
       <option value="Angst">Angst</option>
       <option value="Crime">Crime</option>
       <option value="Drama">Drama</option>
       <option value="Family">Family</option>
       <option value="Fantasy">Fantasy</option>
       <option value="Friendship">Friendship</option> 
       <option value="General">General</option>
  </select>
<?endforeach;?>

I've already tried a couple of things. 我已经尝试了几件事。 I have tried putting the list in a inner for loop and trying to select what comes back through the echo, but that only threw me errors. 我尝试将列表放入内部for循环中,并尝试选择通过回显返回的内容,但这只会引发错误。 I could do an individual if statement for each item, but I shortened the list above and have another just like it and that seems like an unnecessary amount of code. 我可以为每个项目做一个单独的if语句,但是我缩短了上面的列表,并添加了另一个类似的代码,这似乎是不必要的代码量。

If it helps, when I echo back the entire list it comes back as a string, like this. 如果有帮助,当我回显整个列表时,它将以字符串形式返回,如下所示。 Fantasy Friendship General and I do have full text searching enabled. 我和《幻想友谊一般》确实启用了全文搜索。

Thank you in advance for any help! 预先感谢您的任何帮助! :) :)

 <select multiple="multiple" class="multi" name="wGenre[]">
    <option value="Action/Adventure" <?php if(in_array('Action/Adventure',$info)) echo 'selected="selected"';?>>Action/Adventure</option>

Do the same for all 对所有人都做同样的事情

Try this: 尝试这个:

<select onchange="getwGenre(this.value);" name="wGenre" id="wGenre" >
 <?php  foreach( $info  as $row ) { 
    $sel = '';
   if( $row["wGenre_wGenre_name"] == $selected ) 
    $sel = 'selected="selected"';           

    echo '<option value="'.$row["wGenre_wGenre_name"].'" '.$sel.'>'.$row["wGenre_wGenre_name"].'</option>'."\n";
    } ?>
    </select>
    <?php echo form_error('wGenre'); ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM