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

时间:2013-09-28 23:47:34

标签: php mysql

我想从表中填充复选框,并检查它们是否在另一个表上有记录。

我有两个查询,如下所示。

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

$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);

我正在尝试使用第一个查询填充复选框,并使用第二个查询添加选中。

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"];
    }
}

但我认为我错过了一个重要的观点:D尝试创建一个函数并寻求myresult是否在sonuc中具有重叠值,如果是的话我会添加已检查但也失败了。

我检查了一个类似的post并且很好地了解了

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

但是如果记录在firmahizmet表上没有任何行,我会收到以下错误;

  

in_array()期望参数2为数组

任何指示?

1 个答案:

答案 0 :(得分:0)

我添加了一个if语句来检查数组是否为空并避免出现问题,可能是更好的方法,但它解决了这个问题。

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"];

    }