2 querys, one drop-down-menu, multiselection

Hello friends,
I have one table with two columns in my mysql and a drop-down-menu in my php file. I would like to fill in a drop down menu with the elements of the first column and after a click on a submit button (if(isset($_POST[‘submit’])) condition) I want to select those elements on the drop-down-menu if they are the same with the elements on the second column. For example:

Column1

List item1
List item2
List item3
List item4

Column2

List item2
List item3
For this table the result should be a drop-down-menu with

List item1
List item2 selected
List item3 selected
List item4
Any idea how to do the loops? I need two loops, one embedded to the other. I tried to do it with two while-loops but it isn’t right.

<?php
<html><body><form action='test1.php' method='POST'>
echo"<select name='sub[]' size='7' width='300' style='width: 300px' multiple>";
    $items1=mysqli_query($db, $sql1);
    while($result_items1=mysqli_fetch_array($items1))
         {                  
            echo "<option value='".$result_items1['column1']."'";
            if(isset($_POST['submit']))
              {
                 $items2=mysqli_query($db, $sql2);
               while($result_items2=mysqli_fetch_array($items2))
                    {
                          if($result_items1['column1']==$result_items2['column2'])
                             {
                              echo"selected";
                             }


                    }
               }
    echo"'>".$result_items1['column1']."</option>";

        }
echo "</select></form></body></html>";
?>

First of all, never do SQL queries in a loop if you care about performance.

Execute both queries and collect their results before building list:

$column1 = [];
$column2 = [];

$result1 = mysqli_query($db, $sql1);
while ($item = mysqli_fetch_assoc($result1)){ $column1[] = $item['column1']; }

$result2 = mysqli_query($db, $sql2);
while ($item = mysqli_fetch_assoc($result2)){ $column2[] = $item['column2']; }

Then build a list using in_array() to check if the columns intersect:

<?php
echo '<select name="sub[]" size="7" width="300" style="width: 300px" multiple>';
    foreach($column1 as $c1){ 
        echo '<option value="'.$c1.'"';            
        if (in_array($c1, $column2) && isset($_POST['submit'])){
            echo ' selected="selected"';
        }
        echo '>' . $c1 . '</option>';
    }
echo '</select>';
1 Like
<?php
while($result_items1=mysqli_fetch_array($items1)) { $list1[] = $result_items1['column1']; }
while($result_items2=mysqli_fetch_array($items2)) { $list2[] = $result_items2['column2']; }
foreach($list1 AS $item) {
 echo "<option value='$item' ".((in_array($item,$list2) && isset($_POST['submit']) ? 'selected' : '').">$item</option>";
}

(This could be further simplified with a better SQL query, though).

EDIT: hehe. megazoid and I came up with the same answer at the same time, independently.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.