[英]checkbox check with database data
i have two mysql tables and looks like as follows. 我有两个mysql表,如下所示。 iam using PHP and MySQL
我使用PHP和MySQL
CREATE TABLE IF NOT EXISTS `subject_category` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`sid` int(5) DEFAULT NULL,
`category` varchar(50) DEFAULT NULL,
`subject` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=191 ;
INSERT INTO `subject_category` (`id`, `sid`, `category`, `subject`) VALUES
(1, 1, 'GCE O/L', 'Sinhala'),
(2, 2, 'GCE O/L', 'Development Studies'),
(3, 3, 'GCE O/L', 'History'),
(4, 4, 'GCE O/L', 'Mathematics'),
(5, 5, 'GCE O/L', 'Citizan Education'),
and 和
CREATE TABLE IF NOT EXISTS `user_subjects` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`uid` bigint(20) NOT NULL,
`usid` int(4) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=43 ;
INSERT INTO `user_subjects` (`id`, `uid`, `usid`) VALUES
(11, 142247454430186, 1),
(12, 142247454430186, 3),
(13, 142247454430186, 5)
actually what i need is, need to create check-box array using first table data. 实际上我需要的是,需要使用第一个表数据创建复选框数组。 this part is already done.
这部分已经完成了。 then i need to checked checkbox match with second table (subject_category.id=user_subjects.usid) for the particular user_subject.uid
然后我需要检查特定user_subject.uid与第二个表(subject_category.id = user_subjects.usid)的复选框匹配
FINALLY i need 5 check boxes for data in first table and 3 of them have selected with to second table values 最后,我需要5个复选框来存储第一张表中的数据,并且其中3个已选择到第二张表中的值
please help me to solve this problem 请帮我解决这个问题
I think you start off the wrong way, you should do it in 1 step, not in two steps. 我认为你从错误的方式开始,你应该分1步,而不是分两步。
The mysql query you need is: 你需要的mysql查询是:
$query = 'SELECT sc.subject, CASE WHEN us.id IS NULL THEN "" ELSE "checked" AS checked
FROM subject_category sc
LEFT JOIN user_subjects us ON (us.usid = sc.id)';
With the result of this query you should be able to generate the checked and unchecked checkboxes at once. 使用此查询的结果,您应该能够立即生成已选中和未选中的复选框。
NOTE: I don't know which columns you need and if you needed sc.id or sc.sid in the join, but I think you can create the right query with this example. 注意:我不知道您需要哪些列,如果在连接中需要sc.id或sc.sid,但我认为您可以使用此示例创建正确的查询。
Next step PHP (with mysql_* function as the TS asks in comment, I know you should use PDO or mysqli): 下一步PHP(使用mysql_ *函数作为TS请求注释,我知道你应该使用PDO或mysqli):
$result = mysql_query($query);
while ($line = mysql_fetch_assoc($result)){
echo '<input type="checkbox" checked="'.$line['checked'].'" name="subject" value="'. $line['subject'] .'" /> '.$line['subject'];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.