简体   繁体   中英

Selecting html select list from sql database

I kind if have an interesting question. I'm trying to prefill an edit form in 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

<?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. 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 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'); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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