我有两个表需要连接,并且如果 contact_id 字段在列表中,则需要选中重复区域的 html 复选框。
Table one "contact_to_category"- id, contact_id and category_id
Table two "category" - category_id, category, status
我正在尝试为联系人创建一个更新页面,并显示表二中的所有类别,并选中该复选框(如果客户 contact_id 位于表一中)。这样编辑人员就可以看到哪些类别已经处于事件状态。
这是我尝试过的方法,但不知道如何检查复选框(如果它们的 id 列在表一中)?我尝试过连接,但没有变量来表示如果 a=b 则在复选框的重复区域中回显“已选中”。
谢谢!
$query_flags = "SELECT a.category, a.status, a.category_id FROM category a LEFT JOIN contact_to_category ON contact_to_category.category_id = a.category_id AND contact_to_category.contact_id = '$contactid' AND a.status = 1";
<?php do { ?>
<table width="327" border="0" cellspacing="0" cellpadding="1">
<tr>
<td width="20" align="right"><input name="catlist[]" type="checkbox" id="catlist[]" value="<?php echo $row_flags['category_id']; ?>" />
<label for="catlist[]"></label></td>
<td width="226"><?php echo $row_flags['category']; ?></td>
</tr>
</table>
<?php } while ($row_flags = mysql_fetch_assoc($flags)); ?>
最佳答案
您正在执行LEFT JOIN
,当category_id不在第一个表中时,将会出现空行,因此这就是您检查复选框的方式,我已从第一个表中添加了一列contact_to_category.category_id AS secondary_cat_id
检查是否存在
$query_flags = "SELECT a.category, a.status, a.category_id
,contact_to_category.category_id AS second_cat_id FROM category a
LEFT JOIN contact_to_category ON contact_to_category.category_id = a.category_id
AND contact_to_category.contact_id = '$contactid' AND a.status = 1";
$row_flags=mysql_query($query_flags); i assume you have done this in your code
<?php do { ?>
<table width="327" border="0" cellspacing="0" cellpadding="1">
<tr>
<td width="20" align="right">
<?php if(!empty($row_flags['second_cat_id '])){ ?>
<input name="catlist[]" type="checkbox" checked="checked" id="catlist[]" value="<?php echo $row_flags['category_id']; ?>" />
<?php } else{?>
<input name="catlist[]" type="checkbox" id="catlist[]" value="<?php echo $row_flags['category_id']; ?>" />
<?php } ?>
<label for="catlist[]"></label></td>
<td width="226"><?php echo $row_flags['category']; ?></td>
</tr>
</table>
<?php } while ($row_flags = mysql_fetch_assoc($flags)); ?>
关于php - mysql 表连接和复选框重复区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17755293/