1

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>';
}
2
  • Whats the error ? Commented Aug 14, 2017 at 20:44
  • As I said it only selects the last option instead of all selected values Commented Aug 14, 2017 at 20:45

1 Answer 1

3

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>';
}
Sign up to request clarification or add additional context in comments.

4 Comments

I already set a select box name with [] thats not a problem it prints the multiple values I echo $selectedopt but it only select the one option instead of multiple
works perfectly thanks can you please tell me what I was doing wrong?
You didn't break the loop. So $print_selected value was always defined based on the last element of $array.
Ohhh my mistake thanks for clarification and helping me out from this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.