使用cakePhp树的导航菜单

时间:2014-04-15 16:16:01

标签: php cakephp

我正在使用CakePHP树来生成我的导航菜单。 我已成功生成它。现在我需要在导航行为中显示它们。 我试过了。它正在工作但是在这个基本代码之后我不知道该怎么做...我怎么能建立导航..

更新回答:):

navigation menu using cakePhp tree

我的HomeController.php

App::uses('AppController', 'Controller');
class HomeController extends AppController {
    var $name='home';
    public function index() {
        $this->layout = "front_end_index";
        $this->loadModel('Category');
        $category_tree = $this->Category->generateTreeList();
        $categories_array = array();
        foreach($category_tree as $k => $v)
        {
            $categories_array[$k] = $this->Category->find('first', array('conditions' => array('Category.id' => $k)));
        }

        $this->set('cat_info',$categories_array);

    }
}

我的category.ctp

<?php foreach($cat_info as $cat_info) {
      if($cat_info['Category']['parent_id'] == ''){ ?>
         <ul class="menus">
        <li><h4><?php echo $cat_info['Category']['name'] ;?></h4></li>
    <?php } ?>
        </ul>
<?php } ?>

导航阵列示例:

Array
(
    [1] => Array
        (
            [Category] => Array
                (
                    [id] => 1
                    [parent_id] => 
                    [lft] => 1
                    [rght] => 42
                    [name] => My Categories
                    [url] => http://google.com
                )
        )
    [2] => Array
        (
            [Category] => Array
                (
                    [id] => 2
                    [parent_id] => 1
                    [lft] => 2
                    [rght] => 17
                    [name] => Fun
                    [url] => http://google.com
                )
        )
    [3] => Array
        (
            [Category] => Array
                (
                    [id] => 3
                    [parent_id] => 2
                    [lft] => 3
                    [rght] => 8
                    [name] => Sport
                    [url] => http://google.com
                )
        )

3 个答案:

答案 0 :(得分:0)

以@Isaac Rajaei为参考。尝试 -

HomesController.php

App::uses('AppController', 'Controller');
class HomeController extends AppController {
    var $name='home';
    public function index() {
        $this->layout = "front_end_index";
        $this->loadModel('Category');
        $category_tree = $this->Category->find('all');

        $this->set('categories', $category_tree);

    }

}

在视图

index.ctp

<?php
    function treeForm(array &$elements, $parentId = 0, $elkey = null) {
        $branch = array();

        foreach ($elements as $key=>$element) {
            if ($element[$elkey]['parent_id'] == $parentId) {
                $children = treeForm($elements, $element[$elkey]['id'], $elkey);
                if ($children) {
                    $element['children'] = $children;
                }
                $branch[$element[$elkey]['id']] = $element;
                unset($elements[$key]);
            }
        }
        return $branch;
    }

    //Build Menu       
    function convertToMenu($arr, $elmkey) {
        echo "<ul>";
        foreach ($arr as $val) {
                if (!empty($val['children'])) {
                        echo "<li>" . $val[$elmkey]['name'];
                        convertToMenu($val['children'], $elmkey);
                        echo "</li>";
                } else {
                        echo "<li>" . $val[$elmkey]['name'] . "</li>";
                }
        }
        echo "</ul>";
    }


        $menuTree = treeForm($categories, 0, 'Category'); // Preparing $categories.
        convertToMenu($menuTree, 'Category');// changing to list, you can add <a> inside it.
?>

答案 1 :(得分:0)

我在定制后用@fazal和@@ Isaac Rajaei的解决方案解决了我的问题。 希望这对SO求助者来说是完全有用的。 :)

<强> HomesController.php

 <?php
    App::uses('AppController', 'Controller');
    class HomeController extends AppController {
        var $name='home';
        public function index() {
            $this->layout = "front_end_index";
            $this->loadModel('Category');
            $category_tree = $this->Category->find('threaded');     
            $this->set('categories', $category_tree);

        }

    }

<强> category.ctp

function convertToMenu($arr, $elmkey) {
        echo "<ul class='menus'>";
        foreach ($arr as $val) {
                if (!empty($val['children'])) {
                        echo "<li>" . $val[$elmkey]['name'];
                        convertToMenu($val['children'], $elmkey);
                        echo "</li>";
                } else {
                        echo "<li><h4>" . $val[$elmkey]['name'] . "</h4></li>";
                }
        }
        echo "</ul>";
    }


 convertToMenu($categories, 'Category');// changing to list, you can add <a> inside it.

答案 2 :(得分:0)

抱歉,我在输入答案时犯了一些错误。我应该在这里回答。

以前的treeForm方法没有用0生成所有父id,我改变了这样的代码:

控制器:

$categories = $this->Category->findAllByParentId(0);
$data = array(
        'categories' => $categories,
    );
    $this->set(compact('data'));

treeForm方法:

function treeForm(array &$elements, $parentId = 0, $elkey = null) {
    $branch = array();
    foreach ($elements as $key=>$element) {
        if ($element[$elkey]['parent_id'] == $parentId) {
            //adjustment was here, re-find again all children data, so it will work
            //recursively method
            $findChildren = ClassRegistry::init($elkey)->findAllByParentId($element[$elkey]['id']);
            $children = $this->treeForm($findChildren, $element[$elkey]['id'], $elkey);

            if ($children) {
                $element['children'] = $children;
            }
            $branch[$element[$elkey]['id']] = $element;
            unset($elements[$key]);
        }
    }

    return $branch;
}