PHP MySql:打印树-父子复选框 [英] PHP MySql: Print Tree - Parent Child Checkbox

查看:75
本文介绍了PHP MySql:打印树-父子复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have This MySql table for put multiple news categories(parent/child) :

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:

Now I need to print this to jQuery and CSS3 html CheckBOX like this :

NOTE: This is Simple Example for HTML treeview.

HTML:

<ul>
        <li>    <a href="#">Parent</a>

            <ul>
                <li>    <a href="#">Child</a>

                    <ul>
                        <li>    <<a href="#">Grand Child</a>

                        </li>
                    </ul>
                </li>
                <li>    <a href="#">Child</a>

                    <ul>
                        <li><a href="#">Grand Child</a>
                        </li>
                        <li>    <a href="#">Grand Child</a>

                            <ul>
                                <li>    <a href="#">Great Grand Child</a>

                                </li>
                                <li>    <a href="#">Great Grand Child</a>

                                </li>
                                <li>    <a href="#">Great Grand Child</a>

                                </li>
                            </ul>
                        </li>
                        <li><a href="#">Grand Child</a>
                        </li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>

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

我需要为每个根猫打印<ul><li></li></ul>,并为每个父类别打印<ul><li><ul><li></li></ul></li></ul>.

i need to print <ul><li></li></ul> for each root cat and print <ul><li><ul><li></li></ul></li></ul> for each parent category.

我如何使用PHP/MySQL创建/打印此文件:

how do i can create/print this using PHP/MySQL:

推荐答案

使用递归!注意:以下代码对于循环图并不安全(节点可能不是其祖先)!

Use recursion! Note: the code below is not safe for cyclic graphs (nodes may not be ancestors of themselves)!

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

这篇关于PHP MySql:打印树-父子复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆