hi can anyone help me with this? i really had a hard time putting my if statement in this code
<select name="date_opened_year">
<?php
define('DOB_YEAR_START', 1962);
$current_year = date('Y');
for ($count = $current_year; $count >= DOB_YEAR_START; $count--)
{
echo "<option value='{$count}'>{$count}</option>";
}
?>
</select>
and i have here a code where i had put my if statement
<select name="date_opened_year">
<?php
define('DOB_YEAR_START', 1962);
$current_year = date('Y');
for ($count = $current_year; $count >= DOB_YEAR_START; $count--)
{
echo "<option value='{$count}' if($yy == $count) echo 'selected'>{$count}</option>";
}
?>
</select>
$yy = 2003 well it comes from a query in the database and when i look at the code it gives me something like this
<option 'selected'="" echo="" 2012)="" if(2003="=" value="2012">2012</option>
why is that? can anyone revise the code? thanks
try this:
$selectStr = ($yy == $count) ? 'selected': '';
echo "<option value='".$count."' ".$selectStr.">".$count."</option>";
Best to break it up.
You've got your if inside a string creation double quote.
do this:
if($yy == $count){
echo "<option value='{$count}' selected='selected'>{$count}</option>";
}
Echo will not evaluate if statement. Try @DCoder solution with printf or this:
echo "<option value='{$count}'";
if($yy == $count) echo 'selected';
echo ">{$count}</option>";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.