doujiufutaog59220 2013-09-28 23:47
浏览 26

使用两个查询检查复选框的值

I want to populate check boxes from a table and mark them checked if they have records on another table.

I have two queries as shown below.

$hizmetler  = "SELECT * ";
$hizmetler .= "FROM hizmetler";
$sonuc = mysqli_query($conn,$hizmetler);

and

$id= $_GET['id'];
$query_hizmet  = "SELECT * FROM firmahizmet INNER JOIN hizmetler ON firmahizmet.hizmetID=hizmetler.hizmetID ";
$query_hizmet .= "WHERE firmahizmet.firmaID = $id";
$myresult = mysqli_query($conn, $query_hizmet);

I am trying to populate checkboxes with 1st query and add checked with 2nd query.

while ($row0 = mysqli_fetch_assoc($myresult)){

    while($row2 = mysqli_fetch_assoc($sonuc)){ ?>
        <label for="chkbox"></label>
        <input id="chkbox" type="checkbox" name="<?php echo $row2["hizmetCSS"];?>" value="<?php echo $row2["hizmetCSS"];?>" <?php echo ($row0['hizmetCSS']==1 ? 'checked' : '');?> /><?php echo $row2["hizmetAdi"];
    }
}

But I think I am missing a huge point :D Tried to create a function and seek if myresult has overlapping values in sonuc, if yes I'd add checked but failed as well.

I inspected a similar post with an good idea of

' . (in_array($role_name, $user_in_role) ? ' checked="checked"' : '') . ' 

but if the record does not have any row on firmahizmet table I get the following error;

in_array() expects parameter 2 to be array

Any directions?

  • 写回答

1条回答 默认 最新

  • dsbgltg159136540 2013-09-28 23:58
    关注

    I have added an if statement to check if the array is empty or not and avoid problem, probably would be better ways to do it but it solved the issue.

    while ($row0 = mysqli_fetch_array($myresult)){
        $user_in_role[] = $row0['hizmetAdi'];
    
    }
        while($row2 = mysqli_fetch_assoc($sonuc)){
            $role_name=$row2['hizmetAdi'];?>
            <label for="chkbox"></label>
            <input id="chkbox" type="checkbox" name="<?php echo $row2["hizmetCSS"];?>" value="<?php echo $row2["hizmetCSS"];?>" <?php
            if( !empty( $user_in_role ) )
            {
            echo (in_array($role_name, $user_in_role) ? ' checked="checked"' : '');
            }
            ?> /><?php echo $row2["hizmetAdi"];
    
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本