2014-11-24 105 views
0

這是antivirus_detail表:PHP:顯示一個列陣列字段

+-----------------------------+--------------+------+-----+---------+----------- 

| Field      | Type   | Null | Key | Default | Extra 
| 
+-----------------------------+--------------+------+-----+---------+----------- 

| Emp_id      | int(11)  | YES | MUL | NULL | 

| Computer_id     | int(11)  | YES | MUL | NULL | 

| Antivirus_id    | int(11)  | NO | PRI | NULL |auto_incre 
                      ment 
| Antivirus_name    | varchar(50) | YES |  | NULL | 

| Antivirus_key    | varchar(50) | YES |  | NULL | 

| Antivirus_installation_date | date   | YES |  | NULL | 

| Antivirus_expiration_date | date   | YES |  | NULL | 

這是hardware_description表:

+------------------+--------------+------+-----+---------+----------------+ 
| Field   | Type   | Null | Key | Default | Extra   | 
+------------------+--------------+------+-----+---------+----------------+ 
| Computer_id  | int(11)  | NO | PRI | NULL | auto_increment | 
| Emp_id   | int(11)  | NO | MUL | NULL |    | 
| PC_type   | varchar(20) | YES |  | NULL |    | 
| Operating_system | varchar(20) | YES |  | NULL |    | 
| Product_key  | varchar(30) | YES |  | NULL |    | 
| Assign_date  | date   | YES |  | NULL |    | 

EMP_ID是從一個不同的員工表& COMPUTER_ID這兩個表的外鍵是來自hardware_description表的防病毒表中的外鍵。

這是我的PHP代碼:

<?php require_once 'includes/dbconnection.php'; ?> 
<?php require_once 'includes/functions.php'; ?> 

<?php 
    $id="1"; 

    $sql ="SELECT b.*, c.* FROM hardware_description b, antivirus_detail c WHERE b.Computer_id = c.Computer_id AND c.Emp_id = '$id'"; 

    $result = mysqli_query($connection, $sql); 
    //var_dump($result); 

?> 

    <table cellpadding="5" border="0"> 
     <tr> 
      <th style="text-align: left;">Employee ID</th> 
      <th style="text-align: left;">Computer ID</th> 
      <th style="text-align: left;">Operating System</th> 
      <th style="text-align: left;">PC Type</th> 
      <th style="text-align: left;">Antivirus ID</th> 

     </tr> 
    <?php while($row = mysqli_fetch_array($result)) { ?> 
     <tr> 
      <td><?php echo $row['Emp_id']; ?></td> 
      <td><?php echo $row['Computer_id']; ?></td> 
      <td><?php echo $row['Operating_system']; ?></td> 
      <td><?php echo $row['PC_type']; ?></td> 
      <td><?php echo $row['Antivirus_id']; ?></td> 

     </tr> 
    <?php 
    } 
    ?> 
    </table> 

現在,這顯示:

Employee ID Computer ID Operating System PC Type Antivirus ID 
1   2   Windows XP  Desktop 1   
1   2   Windows XP  Desktop 2 

我所要的輸出是這樣的:

Employee ID Computer ID Operating System PC Type Antivirus ID 
1    2   Windows XP  Desktop 1,2 

我希望所有的殺毒軟件跳到計算機ID的ID將顯示爲數組。我如何實現這一目標?

+0

你想用兩個查詢來做嗎? – 2014-11-24 06:24:27

+0

@IndrasinhBihola我不介意兩個疑問。只需要結果。 – Yugal1458 2014-11-24 06:25:23

回答

3

在您的查詢中,您必須使用GROUP_CONCAT來連接Antivirus_ids。

把你的SQL查詢改成這樣;

$sql ="SELECT b.Emp_id, b.Computer_id, b.Operating_system, b.PC_type, GROUP_CONCAT(c.Antivirus_id SEPARATOR ', ') as concatenated_ids FROM hardware_description b, antivirus_detail c WHERE b.Computer_id = c.Computer_id AND c.Emp_id = '$id' GROUP BY c.Antivirus_id"; 

而且在獲取數據方面改變這個$row['Antivirus_id'];$row['concatenated_ids'];

+0

非常感謝,這正是我一直在尋找的。 – Yugal1458 2014-11-24 09:14:50

+0

太好了。乾杯!! – JazyK 2014-11-24 09:16:52

+0

@Yugal1458 please,接受他的回答,然後 – 2014-11-24 09:18:46