How to put a default option selected php

0

Well, if the year was 2012, if the value was selected, is it possible to do it?

$ano[] = "<option value='$row[ano]'/>" 
    
asked by anonymous 07.04.2016 / 12:07

2 answers

0

To capture the current year, use

$anoAtual = date('Y');

To do what you want, you can use the PHP conditions

<?php

$anoAtual = date('Y');

if ($anoAtual == $row['ano']) {
    $ano[] = "<option value='{$row['ano']}' selected>";
} else {
    $ano[] = "<option value='{$row['ano']}'>";
}

Or so

<?php

if ($row['ano'] == '2012') {
    $ano[] = "<option value='{$row['ano']}' selected>";
} else {
    $ano[] = "<option value='{$row['ano']}'>";
}

There are many other ways, but this has to be evaluated according to your needs. Your question was not very clear.

I hope I have contributed.

    
07.04.2016 / 12:23
0

Here is the solution. If the year is 2012 the option is selected. Now you have to ensure that the year is 2012 only once.

$ano[] = "<option ".($row[ano]=='2012'?'selected':'')." value='$row[ano]'/>"
    
07.04.2016 / 12:23