I have two tables I need to join and need to check a html check box of the repeating region if the contact_id field is in the list.
Table one "contact_to_category"- id, contact_id and category_id
Table two "category" - category_id, category, status
I am trying to create an update page for the contact and show all categories from table two and check the checkbox if the clients contact_id is in table one. This way the person editing can see what categories are already active.
Here is what I have tried but cannot figure out how to check the checkboxes if their id is listed in table one? I have tried joins but don't have a variable to say if a=b then echo "checked" in the repeating region of checkboxes.
Thanks!
$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)); ?>
You are doing a LEFT JOIN
and when the category_id is not in the first table there will be null row so this is how you can checked your check boxes i have added one column from the first table contact_to_category.category_id AS second_cat_id
to check whether it exists or not
$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)); ?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.