du90093662774150 2013-04-15 14:00
浏览 38

PHP创建树

My mysql table looks like this: enter image description here

I need to make 3 level tree, without House_NR.

-Country
 |--City 1
 |  |--Street 1
 |  |--Street 2
 |
 |--City 2
 |--City 3
 |  |--Sreet 1
 |  |--Steet 2

In PHP I make this sql query:

SELECT
    l.Country_ID,
    c1.Name AS Country_Name,
    l.City_ID,
    c2.Name AS City_Name,
    l.Street_ID,
    s.Name AS Street_Name,
    l.House_NR
FROM Location l
JOIN Country c1 ON c1.id = l.Country_ID
JOIN City c2 ON c2.id = l.City_ID
JOIN Street s ON s.id = l.Street_ID

And I have this function:

function getTree($data) {
    $tree = prepareTree($data);
    drawTree($tree);
}

function prepareTree($data) {
    $nodeList = array();
    $tree = array();

    foreach ($data as $key) {
        $tree[$key["Country_ID"]]["name"] = $key["Country_Name"];
        $tree[$key["Country_ID"]]["id"] = $key["Country_ID"];
        $tree[$key["Country_ID"]]["level"] = 1;

        $tree[$key["Country_ID"]]["children"][$key["City_ID"]]["name"] = $key["City_Name"];
        $tree[$key["Country_ID"]]["children"][$key["City_ID"]]["id"] = $key["City_ID"];
        $tree[$key["Country_ID"]]["children"][$key["City_ID"]]["level"] = 2;

        $tree[$key["Country_ID"]]["children"][$key["City_ID"]]["children"][$key["Street_ID"]]["name"] = $key["Street_Name"];
        $tree[$key["Country_ID"]]["children"][$key["City_ID"]]["children"][$key["Street_ID"]]["id"] = $key["Street_ID"];
        $tree[$key["Country_ID"]]["children"][$key["City_ID"]]["children"][$key["Street_ID"]]["level"] = 3;
    }

    return $tree;
}

function drawTree($data, $sub = false) {
    echo ($sub) ? "<ul class=\"tree-listCat\">" : "<ul class=\"tree-list\">";

    foreach($data as $key) {
        if ($key["children"] != null) {
            echo "<li class=\"tree-itemCat\"><span class=\"tree-itemSel\"><a href=\"switch.php?action=search&type=tree&level=" . $key["level"] . "&id=" . $key["id"] . "\">" . $key["name"] . "</a></span></li>";
            drawTree($key["children"], true);
        } else
            echo "<li class=\"tree-item\"><span class=\"tree-itemSel\"><a href=\"switch.php?action=search&type=tree&level=" . $key["level"] . "&id=" . $key["id"] . "\">" . $key["name"] . "</a></span></li>";
    }

    echo "</ul>";
}

This function works fine, but there some problem in function where function draw a html version of tree now I have this:
action=search&type=tree&level=" . $key["level"] . "&id=" . $key["id"], but I need to have something like this:

If only country without child then action=search&type=tree&country=id
If only country and city without child street, then action=search&type=tree&country=id&city=id
If all arguments country, city and steet, then action=search&type=tree&country=id&city=id&street_id=id

And also function prepare tree looks bad (In my mind). In few words I need make a tree from Location database.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示
    • ¥15 求三国群英传pl国战时间的修改方法
    • ¥15 matlab代码代写,需写出详细代码,代价私
    • ¥15 ROS系统搭建请教(跨境电商用途)
    • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。