0

我需要从数据库中检索多个选定列表中的数据

在我的数据库值是

马拉地语,阿鲁纳恰利语,阿萨姆语,阿瓦迪语,马拉地语,阿鲁纳恰利语,阿萨姆语,阿瓦迪语

我用下面的代码来检索

     <select id="spokenlanguages" name="known_languages[]" multiple="multiple" size="5">
                 <option value="" >Select</option>
                  <?php 
                    $langs =  $editdatapersonaltbl[0]['known_languages'] ;                  

                    $known_languages = explode(",",$langs); 

                    //print_r($known_languages); exit();

                    for($i=0;$i < count($known_languages);$i++)
                    {  
                   ?>               
                  <option value="<?php echo $known_languages[$i] ;?>"  selected="selected"><?php echo $known_languages[$i] ;?></option>                     
                  <?php } ?>  

                  <option value="Marathi" >Marathi</option>
                  <option value="Arunachali" >Arunachali</option>
                  <option value="Assamese" >Assamese</option>
                  <option value="Awadhi" >Awadhi</option>
                  <option value="Bengali" >Bengali</option>
                  <option value="Bhojpuri" >Bhojpuri</option>
                  <option value="Brij" >Brij</option>
                  <option value="Bihari" >Bihari</option>
</select>

但我的问题是,如果之前选择了马拉地语,而不是它显示在选中和未选中

<option value="Marathi" >Marathi</option>如果我将删除此选项<option value="Marathi" >Marathi</option>而不是马拉地语不在数据库中,而不是用户想要选择马拉地语,则未选择该 选项

请给我一些解决方案....

4

3 回答 3

2

尝试这个:

<select id="spokenlanguages" name="known_languages[]" multiple="multiple" size="5">
         <option value="" >Select</option>
          <?php 
            $langs =  $editdatapersonaltbl[0]['known_languages'] ;                  

            $known_languages = explode(",",$langs);
            $known_languages    = array_filter( $known_languages ); #remove the blank values if any
            //print_r($known_languages); exit();
          ?>

          <option value="Marathi" <?php if(in_array('Marathi', $known_languages)){echo 'selected="selected"';}?> >Marathi</option>
          <option value="Arunachali" <?php if(in_array('Arunachali', $known_languages)){echo 'selected="selected"';}?>>Arunachali</option>
          <option value="Assamese" <?php if(in_array('Assamese', $known_languages)){echo 'selected="selected"';}?>>Assamese</option>
          <option value="Awadhi" <?php if(in_array('Awadhi', $known_languages)){echo 'selected="selected"';}?>>Awadhi</option>
          <option value="Bengali" <?php if(in_array('Bengali', $known_languages)){echo 'selected="selected"';}?>>Bengali</option>
          <option value="Bhojpuri" <?php if(in_array('Bhojpuri', $known_languages)){echo 'selected="selected"';}?>>Bhojpuri</option>
          <option value="Brij" <?php if(in_array('Brij', $known_languages)){echo 'selected="selected"';}?>>Brij</option>
          <option value="Bihari" <?php if(in_array('Bihari', $known_languages)){echo 'selected="selected"';}?>>Bihari</option>
</select>
于 2013-10-10T11:51:04.663 回答
1
<?php

//$lang_array is ur language array
$lang_array = array('Marathi','Hindi','Gujrati','Bengali');

//this array ehich u fetching from DB
$known_languages = array('Marathi', 'Gujrati');
?>
 <select id="spokenlanguages" name="known_languages[]" multiple="multiple" size="5">
 <option value="" >Select</option>

<?php

for($i=0;$i < count($lang_array); $i++)
{
    if(in_array($lang_array[$i], $known_languages))
    {
        $str = 'selected="selected"';
    }
    else
    {
        $str ='';
    }
echo 
'<option value="'.$lang_array[$i].'" '.$str.'> '.$lang_array[$i].'</option>';
}
?>
于 2013-10-10T12:06:47.920 回答
1

首先,您必须将 html 选项作为 html_aarray 放入数组中,然后 cho 数据库数组。如果 html_aarray 的节点在数据库数组中不存在,则在另一个 while eho html 选项之后。

我写了 4 u,使用这个:http ://codepad.org/gjWW9UGg

于 2013-10-10T12:04:05.290 回答