Reputation: 157
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.
Upvotes: 0
Views: 3778
Reputation: 82112
You code is not echoing out the second selected due to the single quotes, just do:
<?php if($temp_val == '1') echo('selected')?>
All the option needs is 'selected'. I really like taking the logic out of the HTML, however in this case it would result in several variables being defined. I guess it's up to your person preference.
I hope this helps!
Upvotes: 0
Reputation: 184
For understanding, I have to tell you about 2 things:
First, You don't need use echo inside echo, just use it once.
Second, You can't use single quote inside single quote or double quote inside double quote. But, If you want do that, you must add backslash. Like this echo "<option value=\"$value\">$text</option>";
And for your problem above, I recommending to use function. You can create a function to make selected at option value.
function set_selected($val1, $val2) {
if ($val1 == $val2) {
echo 'selected="selected"';
}
}
And then, in your code:
<?php
$temp_val = 3;
echo "<td>
<select>
<option value='1' set_selected($temp_val, '1') >1</option>
...
</select>
</td>";
?>
Hope this can help you.
Upvotes: 1
Reputation: 184
Let's try this
<?php $temp_val = 3; ?>
<td>
<select>
<?php
For($i=0;$i<=5;$i++){
?>
<option value="<?= $i; ?>" <?php if($temp_val == $i) echo("selected")?>>
<?= $i ?>
</option>
<?php } ?>
</select>
</td>
Happy coding :)
Upvotes: 2