ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

my own helper creating menu

August 13, 2011 10:05am

Subscribe [1]
  • #16 / Aug 14, 2011 9:28am

    InsiteFX

    6819 posts

    Thats because your model is not in the helper!

    InsiteFX

  • #17 / Aug 15, 2011 10:11am

    Krystian

    69 posts

    ok, try to not hate me 😊

    look I don`t know how to use your tips and try to do this in model just for test with html tags

    model

    function getMenu($parent, $level)
        {
            
            $query = $this->db->query("SELECT a.id id, a.label, a.link, Deriv1.Count FROM `menu` a  LEFT OUTER JOIN (SELECT parent, COUNT(*) AS Count FROM `menu` GROUP BY parent) Deriv1 ON a.id = Deriv1.parent WHERE a.parent=" . $parent);
    
            $menu .= "<ul id='nav'>";
    
               
            foreach($query->result() as $position)
            {
                if($position->Count > 0) {
                    
                    $menu .= "<li><a >link . "'>" . $position->label . "</a>";
    
                        $this->getMenu($position->id, $level + 1);
    
                    $menu .= "</li>";
                    
                }elseif($position->Count == 0) {
                
                    $menu .= "<li><a >link . "'>" . $position->label . "</a></li>";
                    
                }else;
    
                }
    
               $menu .= "</ul>";
               
               return $menu; 
        }

    in controller

    $data['menuHTML'] = $this->menu_model->getMenu(0, 1);
            
    $this->load->view('main_view', $data);

    in view simple echo $menuHTML

    and it seems like recursive function is not working because I get only the first level.
    And notice undefined var menu ;/;/
    BUT of course I would like to do in helper

  • #18 / Aug 15, 2011 10:34am

    InsiteFX

    6819 posts

    I would make into Class Library! Thats how I do mine.

    InsiteFX

  • #19 / Aug 15, 2011 10:37am

    Krystian

    69 posts

    ok, based on your previous advices

    library, I used CI super object

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
    
    class Mymenu {
        
    
        public function create_menu($parent, $level) 
        {
            $CI =& get_instance();
            
            $query = $CI->db->query("SELECT a.id id, a.label label, a.link link, Deriv1.Count count FROM `menu` a  LEFT OUTER JOIN (SELECT parent, COUNT(*) AS Count FROM `menu` GROUP BY parent) Deriv1 ON a.id = Deriv1.parent WHERE a.parent=" . $parent);
    
            $menu .= "<ul id='nav'>";
    
               
            foreach($query->result() as $position)
            {
                if($position->count > 0) {
                    
                    $menu .= "<li><a >link . "'>" . $position->label . "</a>";
    
                        $menu .= $this->create_menu($position->id, $level + 1);
    
                    $menu .= "</li>";
                    
                }elseif($position->count == 0) {
                
                    $menu .= "<li><a >link . "'>" . $position->label . "</a></li>";
                    
                }else;
    
                }
    
               $menu .= "</ul>";
               
               return $menu;        
        }
    }
    
    /* End of file Mymenu.php */

    then in controller

    $data['menu_header'] = $this->mymenu->create_menu(0, 1);

    everything works fine. Thanks!

    Why I get Undefined variable: menu ( this is not a problem, but why when I code in the first line var $menu = ‘’ I get parse error ? and when $menu = ‘’ not? all systems libraries have got var )

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases