2017-07-31 39 views
0

我需要從數據庫中獲取具有我在[a_users_has_interest]表中的所有興趣的特定用戶,並將其顯示在複選框中,但我也想要顯示所有興趣同時,和用戶的利益選擇PHP和MySQL獲取多個選擇檢查的checboxes

事情是這樣的:

Ej

注:我有以下表,我連着的SQL和代碼示例

a_interest:所有利益

a_users:所有用戶

a_users_has_interest:

SELECT * 
FROM a_users_has_interest UHI 
LEFT JOIN a_interest I ON I.id = UHI.interest_id 
WHERE UHI.user_id = '2' 
:誰擁有權益

-- ---------------------------- 
-- 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'); 

我的示例代碼來獲取數據的所有用戶

這顯示我的用戶與選項,但我想要顯示用戶出現的所有興趣和興趣,並顯示其興趣和興趣的ID,然後它們是空的。

感謝您的幫助

回答

1

喜用下面的代碼

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 
     } 
    ?> 
+0

的'die'就足夠了。沒有必要嚷嚷。 – tadman

+0

@tadman對不起,我不是我的意圖。只是想讓他得到清晰的圖片。 –

+0

@ChannaveerHakari我半開玩笑。即使PHP本身很大程度上不區分大小寫,嘗試和使用規範形式的方法也很重要。它有助於促進一致性並使文檔中的內容更清晰。 – tadman