0

This is sort of an extension of the problem solved here: Set default value for HTML select control in PHP however I would like to fill in Multiple values that match, with the values to fill in stored in an additional array:

This is my code so far:

<select name="genres[]" id="genres_edit" multiple>
<?php
$genrelist = array(  
'Action',  
'Adventure',
'Comedy',
'Cooking',
'War',
'Western');
for($i = 0;$i < count($genrelist);$i++) {
    echo "<option value=\"$genrelist[$i]\"";
    for ($g = 0; $g < count($genre);$g++) {
        if ($genrelist[$i] == $genre[$g]) {
            echo "selected=\"selected\"";
        }
    echo ">$genrelist[$i]</option>";
    }
}
?>
</select>

$genrelist is the array of all possible genres that will be used to fill up the select control, and the array of actual genres is stored in $genre.

Basically I want it to highlight the values in the selectbox that match any of the values in the $genre array.

i.e. if the genres stored in $genres are: Adventure, Cooking, Western, then those 3 values will be highlighted in the select box, out of the 6 available genres in the box.

3
  • ...well you can have only 1 selected value. For starters. Commented Feb 6, 2015 at 13:56
  • @AndreiP.No, you can have multiple selected values in a select statement, as ive indicated with mine. Commented Feb 6, 2015 at 13:58
  • Instead of going for second loop why don't you use "in_array" function of php. Commented Feb 6, 2015 at 14:01

3 Answers 3

0

Here's how I'd do it ...

$genres = array(
    'Action',
    'Western'
    );

$genrelist = array(
    'Action',  
    'Adventure',
    'Comedy',
    'Cooking',
    'War',
    'Western');

foreach ($genrelist as $k=>$v) {
    $sel = (array_search($v,$genres) !== false) ? ' selected' : '';
    echo '<option value="'. $k .'"'. $sel .'>'. $v .'</option>';
}

Here's the sandbox ... http://sandbox.onlinephpfunctions.com/code/e4f2ca28e0fd43513b694f5669329cc1db328598

Sign up to request clarification or add additional context in comments.

3 Comments

That works great @neokio! Can I ask why you're using !== false and not == true? I've noticed that if I do != instead, it doesn't select the first genre (in this case, Action) in the selection box, why is that?
well, 0 does not == true, it == false. but it doesn't === false :) coding is very specific.
so basically ... $genres[0] is 'action', and array_search will return 0 for 'action'. and 0==false, but does not === false.
0

Assuming your form in being set with method="get" then the $_GET superglobal should have an array in it called genres (as defined by the fact that your multiple select box is called genres[]).

So when looping through your output you should be able to check if the current genre (from the $genrelist array) exists in the $_GET['genres'] array ... like so:

<?php
$genrelist = array(  
    'Action',
    'Adventure',
    'Comedy',
    'Cooking',
    'War',
    'Western');
?>

<select name="genres[]" id="genres_edit" multiple="multiple">
<?php foreach($genrelist as $genre): ?>
<?php $sThisSelected = in_array($genre, $_GET['genres']) ? " selected=\"selected\"" : ""; ?>
<option value=\"<?= $genre; ?>\"<?= $sThisSelected; ?>><?= $genre; ?></option>
<?php endforeach; ?>
</select>

There's no sanity checking or sanitisation in this but that's the theory anyway.

Comments

0

If you want multiple selection you must specify your select tag with multiple option

<select multiple="1">
  <option selected>option1</option>
  <option selected>option1</option>
  <option selected>option1</option>
</select>

The drawback is that the select menu is no more a dropdown, if that matter for you, let me know and I will try to tune in the solution.

Comments

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.