thara Posted July 21, 2015 Share Posted July 21, 2015 I have two mysql tables named facilities and other one is user_facilities. In facilities table have stored all available facilities and user_facilities table have particular facilities belong to one user.Now I need to fetch all the facilities from mysql and need to format with checkbox for each facility. While fetching every facilities I need to set checked="checked" attribute to checkboxes which are belong to current user.I can select all facility from mysql like this: $sql = "SELECT id, name FROM cuisines"; And this is how it looks my `WHILE` loop. $result = ''; // Fetch all the records: while ($stmt->fetch()) { $result = "<div class='checkbox'>\n"; $result .= " <label>\n"; $result .= " <input type='checkbox' name='facilities[]' value='{$id}'> {$name}\n"; $result .= " </label>\n"; $result .= "</div>\n"; $output[] = $result; } Can anybody tell me how add checked="checked" attribute for these checkboxes which are belong to current user?This is my user_facilities table CREATE TABLE IF NOT EXISTS user_facilities( user_id INT(4) UNSIGNED NOT NULL, facility_id INT(4) UNSIGNED NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8; Link to comment https://forums.phpfreaks.com/topic/297401-select-from-mysql-and-format-them-with-checkboxes-and-set-checked%E2%80%9Cchecked%E2%80%9D-attribute/ Share on other sites More sharing options...
Barand Posted July 21, 2015 Share Posted July 21, 2015 You need to query your facility table LEFT JOINED to the user_facility table SELECT f.facility_name , uf.user_id FROM facility f LEFT JOIN user_facilty uf ON f.facily_id = uf.facility_id AND uf.user_id = $userID; This will give results like +---------------+---------------+ | facilty_name | user_id | +---------------+---------------+ | air_con | NULL | | wi-fi | 1 | | smoking area | NULL | | bar | 1 | +---------------+---------------+ Loop through the results outputting the facility name and checkbox.Check those boxes where restaurant_id is not null air_con [ ] wi-fi [X] smoking area [ ] bar [X] Link to comment https://forums.phpfreaks.com/topic/297401-select-from-mysql-and-format-them-with-checkboxes-and-set-checked%E2%80%9Cchecked%E2%80%9D-attribute/#findComment-1516916 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.