我有一个用于循环复选框的php echo语句:
echo "<input name='battery[$i]' type='checkbox' id='battery[$i]' value='Yes' style='margin-top:10px; margin-left:15px;' />";
我想根据查询结果为其添加此条件:
<?php if($rowbill['battery'] == "Yes") print "checked"; ?>
但我很难获得连接的正确语法,请一些帮助将不胜感激。
答案 0 :(得分:5)
试试这个:
echo "<input name='battery[$i]'" .($rowbill['battery'] == "Yes" ? "checked='checked'" : ""). " type='checkbox' id='battery[$i]' value='Yes' style='margin-top:10px; margin-left:15px;' />";
答案 1 :(得分:2)
为了便于阅读,我更喜欢在var
之前设置$checked = $rowbill['battery'] == "Yes"? ' checked="checked"' : '';
之后:
echo "<input name='battery[$i]' type='checkbox' id='battery[$i]' value='Yes' style='margin-top:10px; margin-left:15px;'$checked />";
否则,只有一个
echo "<input name='battery[$i]' type='checkbox' id='battery[$i]' value='Yes' style='margin-top:10px; margin-left:15px;'".($rowbill['battery'] == "Yes"? ' checked="checked"' : '')." />";
答案 2 :(得分:1)
试试这个:
'<input name="'.battery[$i].'" type="checkbox" id="'.battery[$i].'" value="Yes" style="margin-top:10px; margin-left:15px;"'.($rowbill['battery'] == 'Yes' ? ' checked="checked"' : '').'>';
请注意,当您需要使用''
作为输出的一部分时,使用""
分隔符可以更轻松地进行连接。