簡體   English   中英

顯示包含兩個表中數據的列表

[英]Displaying a list with data from two tables

我有2個表,我想從中提取數據。 第一個表稱為類別,其結構如下:

---------------------------------
id  |   name            |  parent |
--------------------------------- |
1   |  Desktop Courses  |     0   |
2   |  Adobe            |     1   |
3   |  CS6              |     2   |
4   |  IT Courses       |     0   | 
5   |  Microsoft        |     4   |
6   |  Server 2008      |     5   |

我正在使用以下代碼將數據顯示為列表:

<?php
  //Connect to mysql server
  $cn = mysql_pconnect("server", "username", "password");
  mysql_select_db("database");
  $rs = mysql_query("SELECT id, parent, name FROM course_categories", $cn);
  $childrenTree = array(); 
  $categoryNames = array(); 

  while($row = mysql_fetch_array($rs)){
     list($id, $parent, $name) = $row;     
     $categoryNames[(string)$id] = $name;
     $parent = (string)$parent;
     if(!array_key_exists($parent, $childrenTree)) 
         $childrenTree[$parent] = array();
     $childrenTree[$parent][] = (string)$id;
  }


 function renderTree($parentid = "0"){
    global $categoryNames;
    global $childrenTree;
    if($parentid != "0") echo "<li> ", $categoryNames[$parentid], "\n";
    $children = $childrenTree[$parentid];
    if(count($children) > 0){ //If node has children
       echo "<ul>\n";
       foreach($children as $child)
          renderTree($child);
       echo "</ul>\n";
    }
    if($parentid != "0") echo "</li>\n";
 }
 renderTree();  
?>

因此,這顯然會拉出如下數據:

Desktop Courses
  Adobe
    CS6
IT Courses
  Microsoft
    Server 2008

現在,我還有一個表來顯示結構如下的課程:

---------------------------------------------------
id    |      categoryid   |      course            |
---------------------------------------------------|
1     |          3        |       Photoshop CS6    |
2     |          6        |       Active Directory |

現在,我想將課程中的數據合並到類別列表中,但是我不確定如何做到這一點,以便顯示如下:

Desktop Courses
  Adobe
    CS6
      Photoshop CS6
IT Courses
  Microsoft
    Server 2008
      Active Directory

任何幫助將非常感激。

好吧,您可以嘗試做的就是從兩個表中獲取UNION ALL數據,如下所示:

SELECT id, parent, name, 'category' as `type` FROM course_categories
 UNION ALL
SELECT id, categoryid, course, 'course' as `type` FROM courses

其他列type將添加到結果集中,您可以稍后區分類別和課程。 這樣,您將利用現有的php代碼。 不過,您需要進行調整以適應此字段。

編輯:這種方法的問題是來自兩個表的ID相交。 要緩解此問題,您可以:

  • 將課程ID轉換為預定值(例如1000)。
SELECT id, parent, name, 'category' as `type` FROM course_categories
 UNION ALL
SELECT (1000 + id) AS id, categoryid, course, 'course' as `type` FROM courses
  • 首先為類別和不同范圍的課程定義ID

為了鼓勵您考慮從已棄用的mysql_切換到PDO這是您的數據訪問代碼的外觀:

<?php

$childrenTree = array(); 
$categoryNames = array();

//Connect to mysql server
$db = new PDO('mysql:host=server;dbname=db;charset=UTF-8', 'user', 'password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

$sql = "SELECT id, parent, name, 'category' as `type` FROM course_categories
         UNION ALL
        SELECT (1000 + id) AS id, categoryid, course, 'course' as `type` FROM courses";
foreach ($db->query($sql) as $row) {
     //your initial logic wrapped in the PDO row retrieval foreach loop
     list($id, $parent, $name) = $row;     
     $categoryNames[(string)$id] = $name;
     $parent = (string)$parent;
     if(!array_key_exists($parent, $childrenTree)) 
         $childrenTree[$parent] = array();
     $childrenTree[$parent][] = (string)$id;        
}
//close connection to the db
$db = null;

//Rest of your code goes here intact

免責聲明:為簡潔起見,跳過了錯誤處理,驗證等內容。

select u_id, course_name, cat_id from (select id as 'u_id', name as 'course_name', parent as 'cat_id' from course_categories union select id as 'u_id', course as 'course_name', categoryid as 'cat_id')

希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM