duankeye2342 2013-12-02 06:07
浏览 43
已采纳

是否根据mysql表值选中是否显示复选框

I have a form that has fields that auto populate with data from mysql table. Specifically two checkbox input fields for two types of roles; instructor and contact. I initially I stored values the values in the table as either 1 or 0 to signify true or false. A person can be one or the other, neither or both. When I try to display those values through a checkbox I am not getting a result. If the value of in the table for a particular role is 1 I want to have the checkbox checked. I am getting no results.

DB query and display:

    $db_select  = $db_con->prepare("
    SELECT     a.name, 
               a.academy_id,
               p.contact_role,
               p.instructor_role
        FROM academy a
        LEFT JOIN person p ON a.academy_id = p.academy_id
        WHERE a.academy_id = :id
    ");
    if (!$db_select) return false;
    if (!$db_select->execute(array(':id' => $id))) return false;
    $results = $db_select->fetchAll(\PDO::FETCH_ASSOC);
    if (empty($results)) return false;
    $result = '';
    foreach ($results as $value){   
        $result .= "<ul>";
        $result .= "<li><input id=\"person_contact\"  name=\"person_contact\" type=\"checkbox\"". $contact_role = $value2['contact_role'] == "1" ? "checked='checked'"  : "" ." />Concact</li>";
        $result .= "<li><input id=\"person_instructor\"  name=\"person_instructor\" type=\"checkbox\"". $instructor_role = $value2['instructor_role'] == "1" ? "checked='checked'"  : "" ."/>Instructor</li>";
        $result .= "</ul>";
    $s++;   
    }   
  • 写回答

2条回答 默认 最新

  • douren5490 2013-12-02 06:32
    关注

    Updated: What is $value2 ? you used $value in your foreach statement.

    also change

    $contact_role = $value2['contact_role'] == "checked"
    

    to

    $contact_role = $value['contact_role'] == "1"
    

    and check again

    Also for html messed up:

    $result="";
    $strChecked="";
    foreach ($results as $value){   
            $result .= "<ul>";
            if ($value2['contact_role'] == "1")
                $strChecked = "checked='checked'";
    
            $result .= "<li><input id='person_contact'  name='person_contact' type='checkbox'". $strChecked ." />Concact</li>";
            $result .= "<li><input id='person_instructor'  name='person_instructor' type='checkbox'".  $strChecked ."/>Instructor</li>";
            $result .= "</ul>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决