如何检索多个选定列表中的数据

时间:2013-10-10 11:35:55

标签: php codeigniter zend-framework cakephp-2.0

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

我的数据库值

马拉,Arunachali,阿萨姆语,阿瓦迪,马拉,Arunachali,阿萨姆语,阿瓦迪

我使用以下代码进行检索

     <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>而不是因为marathi不在数据库中而不是用户想要选择马拉地语

,那么因为<option value="Marathi" >Marathi</option>选项而未选择

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

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分:1)

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

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

答案 2 :(得分: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>';
}
?>