To get the chosen value in a select box after submit. I use:
<option value="<?=$row["status"]?>" <?=($row["status"]==$status) ? 'selected' : ''?> >
How do I get multiple items when using an array in PHP?
<div class="row">
<div class="col-md-3" style="padding-top:5px">Status</div>
<div class="col-md-6" style="padding-top:5px">
<select name="status[]" class="form-control selectpicker" multiple>
<?php
$sel = "
SELECT DISTINCT
t.status,
s.*
FROM tickets t
LEFT JOIN statussen s
ON t.status = s.stid
GROUP BY s.stid
";
$selres = mysqli_query( $link, $sel );
while ( $row = mysqli_fetch_array( $selres ) ):
?>
<option value="<?=$row["status"]?>" <?=($row["status"]==$status) ? 'selected' : ''?> >
<?=$row["status_omschr"]?>
</option>
<?php endwhile; ?>
</select>
</div>
Try the following:
Add before your loop:
$selectedItems = [];
if(isset($_POST['status'])) {
$selectedItems = $_POST['status'];
}
Change within the loop:
<option value="<?=$row["status"]?>" <?=(in_array($row["status"], $_POST['status'])) ? 'selected' : ''?> >
<?=$row["status_omschr"]?>
</option>
You can do it with using in_array() function in condition like below.
<option value="<?=$row["status"]?>" <?php if(isset($_POST['status']) and !empty($_POST['status'])){ ?> <?=(in_array($row["status"], $_POST['status'])) ? 'selected' : ''?> <?php }?> >
<?=$row["status_omschr"]?>
</option>