我需要从数据库中获取具有[a_users_has_interest]表中所有兴趣的特定用户,并在Checkbox中显示它们,但我也希望同时显示所有兴趣,并且用户的兴趣是选
像这样的东西:
注意:我有以下表格,我附上了SQL和代码示例
a_interest:所有兴趣
a_users:所有用户
a_users_has_interest:所有有兴趣的用户
-- ----------------------------
-- Table structure for a_interest
-- ----------------------------
DROP TABLE IF EXISTS `a_interest`;
CREATE TABLE `a_interest` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of a_interest
-- ----------------------------
INSERT INTO `a_interest` VALUES ('1', 'Deportes');
INSERT INTO `a_interest` VALUES ('2', 'Salud');
INSERT INTO `a_interest` VALUES ('3', 'Belleza');
INSERT INTO `a_interest` VALUES ('4', 'Amor');
INSERT INTO `a_interest` VALUES ('5', 'Internet');
-- ----------------------------
-- Table structure for a_users
-- ----------------------------
DROP TABLE IF EXISTS `a_users`;
CREATE TABLE `a_users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of a_users
-- ----------------------------
INSERT INTO `a_users` VALUES ('1', 'User 1');
INSERT INTO `a_users` VALUES ('2', 'User 2');
-- ----------------------------
-- Table structure for a_users_has_interest
-- ----------------------------
DROP TABLE IF EXISTS `a_users_has_interest`;
CREATE TABLE `a_users_has_interest` (
`user_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`interest_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`user_id`,`interest_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of a_users_has_interest
-- ----------------------------
INSERT INTO `a_users_has_interest` VALUES ('1', '1');
INSERT INTO `a_users_has_interest` VALUES ('1', '2');
INSERT INTO `a_users_has_interest` VALUES ('1', '3');
INSERT INTO `a_users_has_interest` VALUES ('2', '1');
INSERT INTO `a_users_has_interest` VALUES ('2', '2');
我获取数据的示例代码:
SELECT *
FROM a_users_has_interest UHI
LEFT JOIN a_interest I ON I.id = UHI.interest_id
WHERE UHI.user_id = '2'
这向我显示了具有选项的用户,但是我希望显示用户所感兴趣的所有兴趣以及用户感兴趣的ID所显示的兴趣,然后它们是空的。
您好使用以下代码
db_connect.php
$host = 'localhost';
$user = 'root';
$password = 'root';
$database = 'skerp';
$connection_error = 'Sorry!!! We are experiencing problems with the database settings';
$link = mysqli_connect($host, $user, $password, $database) or DIE($connection_error);
以下是我使用它的代码
<?php
require_once('db_connect.php');
$getAllInterests = mysqli_query($link, "SELECT * FROM a_interests");
$userInterests = mysqli_query($link, "SELECT * FROM a_user_has_interests WHERE user_id = 1";
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
while($getAllInterest = mysqli_fetch_assoc($getAllInterests)){
while($userInterest = mysqli_fetch_assoc($userInterests)){
?>
<input
<?php echo ($userInterest['id'] == $getAllInterest['id']) ? 'checked="checked"' : '' ?>
type="checkbox" name="interests[]" value="<?php echo $getAllInterest['id'] ?>"> <?php echo $getAllInterest['name'] ?>
<?php
}
}
?>
</body>
</html>
第二种方法:
您可以使用in_array检查以下代码,而不是在循环内使用循环
$getAllInterests = mysqli_query($link, "SELECT * FROM a_interests");
$userInterests = mysqli_query($link, "SELECT interest_id FROM a_user_has_interests WHERE user_id = 1";
<?php
while($getAllInterest = mysqli_fetch_assoc($getAllInterests)){
?>
<input
<?php echo (in_array($getAllInterest['id'], $userInterests)) ? 'checked="checked"' : '' ?>
type="checkbox" name="interests[]" value="<?php echo $getAllInterest['id'] ?>"> <?php echo $getAllInterest['name'] ?>
<?php
}
?>