drzablspw01655860
2017-08-14 20:41
浏览 91
已采纳

PHP Bootstrap多选选项

In my code I am using PHP which populates the bootstrap multi selectpicker from MySQL database but the problem is it only selects the last option instead of multiple options, I don't know what I am missing in it? Here is my my code

function MultiBindCombo($tablenames, $columnnames1, $columnnames2, $comboname, $selectedopt) {
    global $conn; 
    $sql="SELECT ". $columnnames1. ", " . $columnnames2 . " FROM ". $tablenames;
    $result = mysqli_query($conn, $sql);
    if( ! $result ) {
        echo mysql_error();
        exit;
    }
    echo '<select class="selectpicker form-control" data-live-search="true" name="' . $comboname . '" multiple="multiple">';
    $array = explode(',', $selectedopt);
    while ($row=mysqli_fetch_array($result)) {
            foreach ($array as $select_option){
            if($row[$columnnames1] == $select_option) {
                $print_selected = 'selected';
            } else {
                $print_selected = '';
            }
            echo $select_option;
            }
        echo '<option data-tokens="' . $row[$columnnames1] . '" value="' . $row[$columnnames1] . '"' .$print_selected. '>' . $row[$columnnames2] . '</option>';
    }
    echo '</select>';
}

图片转代码服务由CSDN问答提供 功能建议

在我的代码中我使用的是 PHP ,它填充了b​​ootstrap multi selectpicker < / code>来自 MySQL 数据库,但问题是它只选择最后一个选项而不是多个选项,我不知道我缺少什么? 这是我的代码

 函数MultiBindCombo($ tablenames,$ columnnames1,$ columnnames2,$ comboname,$ selectedopt){
 global $ conn;  
 $ sql =“SELECT”。  $ columnnames1。  “,”。  $ columnnames2。  “来自”。  $ tablenames; 
 $ result = mysqli_query($ conn,$ sql); 
 if(!$ result){
 echo mysql_error(); 
 exit; 
} 
 echo'&lt; select class =“  selectpicker form-control“data-live-search =”true“name =”'。$ comboname。'“multiple =”multiple“&gt;'; 
 $ array = explode(',',$ selectedopt); 
  while($ row = mysqli_fetch_array($ result)){
 foreach($ array as $ select_option){
 if($ row [$ columnnames1] == $ select_option){
 $ print_selected ='selected'; 
  } else {
 $ print_selected =''; 
} 
 echo $ select_option; 
} 
 echo'&lt; option data-tokens =“'。$ row [$ columnnames1]。'”value =“'  。$ row [$ columnnames1]。'“'。$ print_selected。  '&GT;'  。  $ row [$ columnnames2]。  '&lt; / option&gt;'; 
} 
 echo'&lt; / select&gt;'; 
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doq91130 2017-08-14 20:45
    已采纳

    To get all selected values in a multiselect you need to use name attribute with []:

    echo '<select class="selectpicker form-control" data-live-search="true" name="' . $comboname . '[]" multiple="multiple">';
    

    After that you can iterate over $_POST[$comboname] with a foreach.

    Update:

    Let's look closer to your code here:

    while ($row=mysqli_fetch_array($result)) {
        foreach ($array as $select_option){
    
            // You iterate over every value of array, so in the end
            // `$print_selected` is defined depending on value of the 
            // last `$select_option`
    
            if($row[$columnnames1] == $select_option) {
                $print_selected = 'selected';
            } else {
                $print_selected = '';
            }
    
            // remove this. Why you echo `$select_option`?
            echo $select_option;
        }
        echo '<option data-tokens="' . $row[$columnnames1] . '" value="' . $row[$columnnames1] . '"' .$print_selected. '>' . $row[$columnnames2] . '</option>';
    }
    

    Rewrite it as:

    while ($row=mysqli_fetch_array($result)) {
        $print_selected = ''; 
        foreach ($array as $select_option){
            if($row[$columnnames1] == $select_option) {
                $print_selected = 'selected';
    
                // break `foreach` as you found the right item
                break;
            }
        }
        // if right item is not found - `$print_selected` is empty
    
        echo '<option data-tokens="' . $row[$columnnames1] . '" value="' . $row[$columnnames1] . '"' .$print_selected. '>' . $row[$columnnames2] . '</option>';
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题