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]
  • #1 / Aug 13, 2011 10:05am

    Krystian

    69 posts

    Hi,

    I`m trying to create dropdown menu from db. Now just for example

    helper

    <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    if ( ! function_exists('create_menu'))
    {
        function display_menu($parent, $level) {
        
            $query = $this->db->get('menu');
            
            return $query->result();
            
            
        
        }
    }

    controller

    <?php
    class Main extends CI_Controller {
    
        
        function __construct()
        {
            parent::__construct();
        }    
    
        public function index()
        {
            $data['title'] = "Elcometer";
            
            
            $data['menu'] = dispaly_menu(0,1);
            
            $this->load->view('main_view', $data);
        }
        
    }
    ?>

    view

    <?php 
    
        foreach($menu as $menus)
        {
            echo $menus->label;
        }
    
     ?>

    and I get Call to undefined function dispaly_menu()

    in this simple example I would like to retrieve some data from db using helper. Or maybe I should put it into model and then use model method in helper. Can someone give me a correct tip/code to do this?


    p.s. my function creating menu

    <?php
        function display_menu($parent, $level) {
        
            $result = mysql_query("SELECT a.id, a.label, a.link, Deriv1.Count FROM `tree2` a  LEFT OUTER JOIN (SELECT parent, COUNT(*) AS Count FROM `tree2` GROUP BY parent) Deriv1 ON a.id = Deriv1.parent WHERE a.parent=" . $parent);
        
            echo "<ul id='nav'>";
        
            while ($row = mysql_fetch_assoc($result)) {
        
                if ($row['Count'] > 0) {
        
                    echo "<li><a href="http://">" . $row['label'] . "</a>";
        
                    display_children($row['id'], $level + 1);
        
                    echo "</li>";
        
                } elseif ($row['Count']==0) {
        
                    echo "<li><a href="http://">" . $row['label'] . "</a></li>";
        
                } else;
        
            }
        
            echo "</ul>";
        
        }
    ?>

    thank in advance
    Krystian

  • #2 / Aug 13, 2011 1:37pm

    Lorin

    15 posts

    I’m not sure, that is reason for your problem, but i don’t see:

    $this->load->helper('yourhelper');

    in your controller.

    Where do you have the third script? Isn’t necessary to load it into helper file?

  • #3 / Aug 13, 2011 2:25pm

    Krystian

    69 posts

    I`m autoloading it.
    I`m a little bit confused if I can use

    $this->db->select($query)

    in helper function
    This function should return an array and then pass to view and loop through it.
    The third script should be paste into first one -> helper ( now is just for test )

  • #4 / Aug 13, 2011 3:04pm

    Lorin

    15 posts

    In helper, you don’t have a class. I think you can’t use $this->db->get(‘menu’);

  • #5 / Aug 13, 2011 3:10pm

    Krystian

    69 posts

    I guess so…

    so can someone give me a tip/sample code to resolve my issue?
    Basically I need to have recurive function which creates my menu and display it in view 😊

  • #6 / Aug 13, 2011 3:14pm

    Lorin

    15 posts

    What about to use [Models] rather than Helper?

  • #7 / Aug 13, 2011 3:23pm

    Krystian

    69 posts

    yeah, of course I can create model ( recursive ), but look on my old function again

    function display_children($parent, $level, $dbh) {
        
        $sql = "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;
    
        echo "<ul id='nav'>";
        
        foreach($dbh->query( $sql ) as $row)
        {
    
            if ($row['Count'] > 0) 
            {
    
                echo "<li><a href="http://">" . $row['label'] . "</a>";
    
                display_children($row['id'], $level + 1, $dbh);
    
                echo "</li>";
    
            } elseif ($row['Count'] == 0) {
    
                echo "<li><a href="http://">" . $row['label'] . "</a></li>";
                //echo "<li class='level".$level."'><a href="http://" class='level.$level.'>" . $row['label'] . "</a></li>";
    
            } else;
        }
        
        echo "</ul>";
    
    }

    This ( with CSS help ) create fine dropdown menu.
    How can I use it in model? I don`t want to have HTML tags there.

  • #8 / Aug 13, 2011 3:42pm

    Lorin

    15 posts

    In model, there will be ‘get-data’ part of scripts. In controller you will parse returned data as you wish and send them to view.

    That’s my opinion at least.

  • #9 / Aug 13, 2011 3:51pm

    Lorin

    15 posts

    For example I made simple script to create menu from database:

    model:

    <?php
        class Test_model extends CI_Model {
    
            function __construct() {
                parent::__construct();
            }
    
            function display_menu() {
    
                $this->load->database();
    
                $query = $this->db->get('users');
                return $query->result();
            }
    
        }
    ?>

    controller:

    <?php
        class Test extends CI_Controller {
    
            function __construct(){
                parent::__construct();
            }
    
            public function index() {
    
                $this->load->model('test_model','menu');
    
                $data['title'] = 'Elcometer';
                $data['menu'] = $this->menu->display_menu();
    
                $this->load->view('test_view', $data);
            }
    
        }
    ?>

    and view:

    <html>
        <head>
            <title><?php echo $title; ?></title>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        </head>
        <body>
            <h1><?php echo $title; ?></h1>
            <ul>
                <?php foreach($menu as $menus) { ?>
                    <li> <?php echo $menus->nick; ?> </li>
                <?php } ?>
            </ul>
        </body>
    </html>

    I know, it is really simple.

    Note: It’s using my ‘user’ table, because I’m too lazy to create another table for testing.

  • #10 / Aug 13, 2011 4:17pm

    Krystian

    69 posts

    I`ll try it later but as you can see that my method is a bit different.
    It`s recursive and has html tags. Try code it 😊
    See you later
    and thanks for your posts

  • #11 / Aug 13, 2011 4:33pm

    Lorin

    15 posts

    I know, but that was only for show simple example. You can rewrite it with your recursive code.

    I will should try to code it, but I’m CI newbie. Don’t wait anything.

  • #12 / Aug 13, 2011 10:06pm

    InsiteFX

    6819 posts

    Your helper does not work because your function name is different then
    the function_exists name! You need to give them both the same name!

    Also if your use function helpers and you want to use like CI’s database
    then you also need to use the CI Super Object!

    <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    if ( ! function_exists('create_menu'))
    {
        function create_menu($parent, $level) {
        
            $CI = get_instance();
    
            $query = $this->CI->db->get('menu');
            
            return $query->result();
        }
    }

    InsiteFX

  • #13 / Aug 14, 2011 5:31am

    Krystian

    69 posts

    Thank you for you reply InsiteFX. This is my 2nd project in CI.
    Two more questions:

    1. I autoload create_menu in config file and when in controller

    $data['menu'] = create_menu(0, 1);

    I get
    Fatal error: Using $this when not in object

    2. Is it good convention/idea to code all my old ( I pasted it somewhere above ) function into helper? with html tags etc…

    3. And more important now is that I don`t know how to use recursive helper

    can you please give me example pseudo code how to do this?

  • #14 / Aug 14, 2011 7:04am

    InsiteFX

    6819 posts

    1)For a helper like that you need to load it in your controller.
    Your controller is the object.

    If you need it across multiple controllers use a MY_Controller.

    2) No you should not have html code in it unless you create.

    3) A recursive function just calls itself.

    
    

    See how getChild calls itself.

    InsiteFX

  • #15 / Aug 14, 2011 8:53am

    Krystian

    69 posts

    yeah but I get this error in helper file

    Fatal error: Using $this when not in object context in C:\wamp\www\application\helpers\create_menu_helper.php on line 9

    $query = $this->CI->db->get('menu');
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases