duanchi0883649 2017-01-18 20:03
浏览 85
已采纳

使用if语句将mysql字段回显到复选框

Hello I think I am really close but I just cant seem to get my statement working correctly

I have a overtime field from Mysql and I can display it but I am trying to make it either checked or not checked depending on if it is a 1 for checked or 0 for not checked. I can show the 1 or the 0 onto a Text field but the checkbox just won't work for me.

echo '<td> Overtime <input type="checkbox" name="chg_hrs_ovt[]"  <? if($list_hours["overtime"]==1){ echo "checked=checked"} ?> >';

My complete code:

<?
// This next section is to load the text fields from the mysql
// this is to collect all of the logged hours on the job sheet and then display
// the hours already entered. 
$hours_query = "SELECT * FROM hours where job_number = '$job_number'";
$hours_result= ($mysqli-> query($hours_query));

// this next section collects the data from the parts table and then adds it to an array
// this then goes into a while statement until it has displayed all of the results it has
// depending on how many parts were used

while ($list_hours = mysqli_fetch_array($hours_result))
{
    echo " <table border ='0' table width = 70%>";
    echo '<td> <input type="hidden" name="chg_hrs_id[]" value="'.$list_hours['unique_id'].'">';
    echo '<td> Start Time  <input type="time" name="chg_hrs_str[]" value="'.$list_hours['start_time'].'" >';
    echo '<td> Finish Time <input type="time" name="chg_hrs_fin[]" value="'.$list_hours['finish_time'].'">';
    echo '<td> Date Of Work <input type="date" name="chg_hrs_date[]" value="'.$list_hours['date'].'">';
    echo '<td> Ovt Boolean <input type="ovt_Booloean" name="chg_hrs_ovtb[]" value="'.$list_hours['overtime'].'">';
    echo '<td> Overtime <input type="checkbox" name="chg_hrs_ovt[]"  <? if($list_hours["overtime"]==1){ echo "checked=checked"} ?> >';               
    echo "</tr>";
}
echo "</table>";    
?>
  • 写回答

2条回答 默认 最新

  • duanbu4962 2017-01-18 20:19
    关注

    I think you have some Syntax issues. Try the following:

    echo '<td> Overtime <input type="checkbox" name="chg_hrs_ovt[]"' . ($list_hours["overtime"]==1 ? 'checked="checked"' : '') . '>';  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器