Solution for <select> with many options

Asked

Viewed 1,008 times

2

What is the best solution to do the <select> leave such result selected in a <select> with many options ?

I use IF in this situation with few options, but it becomes unviable for a very large...

follows example code:

<select class="alterarperfilNomeInput2" name="sexo">
<?php 
if ($row[sexo] == "Masculino")
{
echo "<option value=\"Masculino\" selected>Masculino</option>
<option value=\"Feminino\">Feminino</option>";
}
else
{
echo "<option value=\"Masculino\">Masculino</option>
<option value=\"Feminino\" selected>Feminino</option>";
}
?>
</select>

also follows select code that need to leave an option selected only according to BD...

<select name="dia" class="alterarperfilNomeInput2A">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>

3 answers

3


In these situations you can use the ternary operator ?:, directly on echo, thus:

<select class="alterarperfilNomeInput2" name="sexo">
<option value="Masculino"<?php echo ($row[sexo] == "Masculino" ? ' selected':'') ?>>Masculino</option>
<option value="Feminino"<?php echo ($row[sexo] == "Feminino" ? ' selected':'') ?>>Feminino</option>
</select>

For the day use a for, thus:

<select name="dia" class="alterarperfilNomeInput2A">
<?php
for($i = 1;$i<=31;$i++) {
    echo '<option value="'.$i.'"'.($row[dia] == $i ? ' selected':'').'>'.$i.'</option>'."\n";
}
?>
</select>

2

You can always make an ajax call and get the value you have stored in the database (can you do that?). From there, just put a function on document ready page:

$( document ).ready(function() {
    var opcao = "5"; //exemplo de um valor do dia
    $('[name=dia]').val(opcao);
});

Link: jsfiddle

With this example it will work for any kind of select you might have. If one appears that is not with the days of the month (value is an integer) and is with car brands or so (value is string) you will have problems...

2

With this function you are free to use numbers or letters and you can also put the different value of the text in your options to select.

$dias = range(1,31);//array 1 a 31
$cont = 5;
select($dias,$cont);

$gender = array("masculino"=>"homem", "feminino"=>"mulher");
$gcont = "masculino";
select($gender,$gcont);

function select(array $options, $controler){
    $select_options = NULL;
    foreach ($options as $key=>$value) {
        $selec = ($key == $controler) ? "selected='selected'" : "";
        $select_options .= "<option value='$key' $selec>$value</option>";
    }  
    return $select_options;
} 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.