Dexter
Dexter

Reputation: 9334

selected='selected' the values in the Multiple Dropdown from mysql

I am trying to select the values in the Multiple dropdown from Mysql using PHP. I cannot seems to get the selection right in the dropdown. Can anyone help me out.

Here's the SELECT statement.

$id = '';
if( isset($_GET['id']) && !empty($_GET['id'])) {
    $id = $_GET['id'];

    $sqlParent = "SELECT * FROM multipleselect";
    $resultParent = mysqli_query($con, $sqlParent);
    // Output: One Two Three

    $sqlChildren = "SELECT * FROM multipleselect WHERE multipleselecttrackerid = $id";
    $resultChildren = mysqli_query($con, $sqlChildren);
    // Output: Two Three
}

The Php Dropdown using two while loop

<select name ="fm_multiple" multiple>
    <?php
    while($row = mysqli_fetch_assoc($resultParent)){
        $selectedParent = $row['multipleselect_name'];

        while($row = mysqli_fetch_assoc($resultChildren)){
            $selectedChild = $row['multipleselect_name'];
        }

        $selected = ($selectedParent == $selectedChild) ? "selected='selected'": '';
        echo "<option value='{$selectedParent}' {$selected}>{$selectedParent}</option>";
    }
    ?>
</select>

This is the Output i get.

<select name="fm_multiple" multiple="">
    <option value="One">One</option>
    <option value="Two">Two</option>
    <option value="Three" selected="selected">Three</option>
    <option value=""></option>
</select>

Upvotes: 0

Views: 58

Answers (2)

ArtisticPhoenix
ArtisticPhoenix

Reputation: 21671

Without seeing the data all I can do is guess, but this is definitely wrong:

while($row = mysqli_fetch_assoc($resultParent)){
    $selectedParent = $row['multipleselect_name'];

    while($row = mysqli_fetch_assoc($resultChildren)){ //$row is overwritten
        $selectedChild = $row['multipleselect_name'];
    }

    $selected = ($selectedParent == $selectedChild) ? "selected='selected'": '';
    echo "<option value='{$selectedParent}' {$selected}>{$selectedParent}</option>";
}

You cant iterate over the query multiple times (technically this is not true), but after the first iteration of the outer while loop the inner while loop iterates over the full result set of $resultChildren set. Therefore on the second iteration of the outer loop, you result will return false from then on.

If you wanted to iterate over the result again (which is pointless in this case) you would have to do mysqli_data_seek($resultChildren,0). It's pointless because you can fetch the data before the loop. And using in_array is more efficient then looping over it (even though you could improve the loop a bit by calling break after you find your match).

//only column you need is multipleselect_name, the smaller the result set the easier it is on the network trafic (so the faster it responds)
$sqlChildren = "SELECT multipleselect_name FROM multipleselect WHERE multipleselecttrackerid = $id";
$resultChildren = mysqli_query($con, $sqlChildren);

$selected = array_column(mysqli_fetch_all($resultChildren,MYSQLI_ASSOC), 'multipleselect_name');
/*
     we need ['item1','item2', .... ] instead of [['multipleselect_name'=>'item1'],['multipleselect_name'=>'item2']]

     I don't think you can fetch all from one column in mysqli
     ..PDO..
      $selected = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
*/

while($row = mysqli_fetch_assoc($resultParent)){
    $selectedParent = $row['multipleselect_name'];
    $selected = in_array($selectedParent,$selected) ? "selected='selected'": '';
    echo "<option value='{$selectedParent}' {$selected}>{$selectedParent}</option>";
}

On top of what I mentioned above your overwriting $row with the second loop. Which may not really matter in this case (your not using $row from Parent again) but its bad practice, which can cause all kinds of hard to track down issues.

I simplified the logic because if:

 $selectedParent = $parent['multipleselect_name']; //changed from row for clarity
 $selectedChild = $child['multipleselect_name'];   //changed from row for clarity
 //and
 $selectedParent == $selectedChild 
 //then
 $parent['multipleselect_name'] = $child['multipleselect_name'];
 //so if we have a list of all
 $child['multipleselect_name']
 //the we can find 
 $parent['multipleselect_name']
  //in that list

Hope that makes sense it's like saying a=b and b=c so a=c

Upvotes: 1

vr_driver
vr_driver

Reputation: 2085

last I knew, it should just be SELECTED in your entry.

<select name="fm_multiple" multiple="">
    <option value="One">One</option>
    <option value="Two">Two</option>
    <option value="Three" SELECTED>Three</option>
    <option value=""></option>
</select>

https://www.w3schools.com/tags/att_selected.asp

Upvotes: 0

Related Questions