I'm having problem on select value on the dropdown list
.
What I have tried:
As currently I have no problem set value inside pure html
and some php
for logic
in order to set the value. As show in this example:
<?php $temp_val = 3; ?>
<td>
<select>
<option value="1" <?php if($temp_val == "1") echo("selected='selected'")?>>1</option>
<option value="2" <?php if($temp_val == "2") echo("selected='selected'")?>>2</option>
<option value="3" <?php if($temp_val == "3") echo("selected='selected'")?>>3</option>
<option value="4" <?php if($temp_val == "4") echo("selected='selected'")?>>4</option>
<option value="5" <?php if($temp_val == "5") echo("selected='selected'")?>>5</option>
</select>
</td>
In this case, it should select number 3, and the result does works like this:
As seen here, everything works as expected with pure html
and some php
.
The Problem
Currently I would like use the same logic inside php echo()
, which means I need to put php code inside a php code with echo
. As I have researched it is not possible as stated here.
As I have tried to implement inside php echo code
and this is the result:
<?php
$temp_val = 3;
echo "<td>
<select>
<option value='1' <?php if($temp_val == '1') echo('selected='selected'')?>1</option>
<option value='2' <?php if($temp_val == '2') echo('selected='selected'')?>2</option>
<option value='3' <?php if($temp_val == '3') echo('selected='selected'')?>3</option>
<option value='4' <?php if($temp_val == '4') echo('selected='selected'')?>4</option>
<option value='5' <?php if($temp_val == '5') echo('selected='selected'')?>5</option>
</select>
</td>";
?>
When combining I made some subtle changes from "
to '
as it might interfere with echo. At this point, there must be something wrong. and this is the result:
As of why it shows 3 of them, because I need looped with echo and php, that's the reason I'm unable to use pure html to accomplish the task. Does anyone have the similar problem?
Thanks for the time, and have a nice day.