PHP MySql:打印树 – 父子复选框

我有这个MySql表用于放置多个新闻类别(父/子):

 ID | NAME | PARENTID | DESC | 1 | LINUX | 0 | NULL | 2 | DEBIAN | 1 | NULL | 3 | SLAX | 1 | NULL | 4 | SLAXLIVE | 3 | NULL | 5 | SWFLIVE | 3 | NULL | 

现在我需要将此打印到jQueryCSS3 html CheckBOX如下所示:

NOTE: This is Simple Example for HTML treeview.

在此处输入图像描述

HTML:

  

PHP:

 $result = mysql_query("SELECT id, name, parent FROM " . NEWS_CATS . " ORDER BY name"); $items = array(); while($row = mysql_fetch_array($result)) { $items[] = array('id' => $row['id'], 'label' => $row['name'], 'parent' => $row['parent']); } 

我需要为每个根猫打印

并打印

为每个父类别。

我如何使用PHP/MySQL创建/打印这个:

使用递归! 注意:下面的代码对于循环图是不安全的(节点可能不是它们自己的祖先)!

 printChildren($items,0); function printChildren(array $items, $parentId){ foreach($items as $item){ if($item['parent']==$parentId){ print '
  • '; print $item['label']; //or whatever you want about the current node print '
      '; printChildren($items, $item['id']); print '
  • '; } } }