显示包含两个表中数据的列表

时间:2013-01-30 03:49:53

标签: php mysql

我有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

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

嗯,您可以尝试做的只是来自两个表的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

免责声明:为简洁而跳过了错误处理,验证等。

答案 1 :(得分:0)

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

希望这有帮助