gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:15:43 26 4
gpt4 key购买 nike

我想填充一个表中的复选框,并在另一个表上有记录时将其标记为选中。

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

$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() expects parameter 2 to be array

有什么指示吗?

最佳答案

我添加了一个 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"];

}

关于php - 使用两个查询检查复选框的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19072895/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com