PHP if 语句在 echo 中创建 HTML 选项列表
我有一个包含 10 个数字的 HTML 选项列表。我希望根据从数据库检索的值预先选择这些选项之一。如果检索到的值为 0 或 FALSE,则应选择 value="0",如果检索到的值为 1,则应选择 value="1",等等。
我查看过: if 块位于 echo 语句内? 但是,我无法使用其信息来解决我的问题。
我还能这样处理吗?我在第一个 if 语句调用时遇到解析错误,我的语法肯定是严重错误的。
代码:
$uv = FALSE;
while ($r2_rows = mysqli_fetch_array($r2, MYSQLI_ASSOC)) {
if (array_key_exists($r1_rows['article_id'], $r2_rows)) {
$uv = $r2_rows['user_vote']; // Get user_vote value.
$selected = 'selected="selected"';
echo'
<td align="left">
<select class="articlevote" name="article_vote">
<option id="vote0" value="0"'.if($uv == 0 || $uv == FALSE) $selected.'>No Vote</option>
<option id="vote1" value="1"'.if($uv == 1) $selected.'>1</option>
<option id="vote2" value="2"'.if($uv == 2) $selected.'>2</option>
<option id="vote3" value="3"'.if($uv == 3) $selected.'>3</option>
<option id="vote4" value="4">4</option>
<option id="vote5" value="5">5</option>
<option id="vote6" value="6">6</option>
<option id="vote7" value="7">7</option>
<option id="vote8" value="8">8</option>
<option id="vote9" value="9">9</option>
<option id="vote10" value="10">10</option>
</select>
</td>';
} // End of IF - article_id has a user vote.
} // End of WHILE - checking current article_id against every user_id vote.
I have a HTML option list of 10 numbers. I want one of these options to be pre-selected based on a value retrieved from a database. If the retrieved value is 0 or FALSE then value="0" should be selected, if the retrieved value is 1 then value="1" should be selected, etc.
I have looked at: if block inside echo statement? however, I haven't been able to fix my problem using its information.
Can I even go about it this way? I get a parse error on the first if statement call, my syntax is surely horribly wrong.
The Code:
$uv = FALSE;
while ($r2_rows = mysqli_fetch_array($r2, MYSQLI_ASSOC)) {
if (array_key_exists($r1_rows['article_id'], $r2_rows)) {
$uv = $r2_rows['user_vote']; // Get user_vote value.
$selected = 'selected="selected"';
echo'
<td align="left">
<select class="articlevote" name="article_vote">
<option id="vote0" value="0"'.if($uv == 0 || $uv == FALSE) $selected.'>No Vote</option>
<option id="vote1" value="1"'.if($uv == 1) $selected.'>1</option>
<option id="vote2" value="2"'.if($uv == 2) $selected.'>2</option>
<option id="vote3" value="3"'.if($uv == 3) $selected.'>3</option>
<option id="vote4" value="4">4</option>
<option id="vote5" value="5">5</option>
<option id="vote6" value="6">6</option>
<option id="vote7" value="7">7</option>
<option id="vote8" value="8">8</option>
<option id="vote9" value="9">9</option>
<option id="vote10" value="10">10</option>
</select>
</td>';
} // End of IF - article_id has a user vote.
} // End of WHILE - checking current article_id against every user_id vote.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用三元运算符
?:
Use the ternary operator
?:
您可以将该过程分为两部分:
在此处构建您的选择菜单:
查看示例 http://codepad.org/1O0PpQ7R
You could break the process into two parts :
Build your selectmenu here:
see sample http://codepad.org/1O0PpQ7R