Welcome Guest, Not a member yet? Register   Sign In
my own helper creating menu
#1

[eluser]Krystian[/eluser]
Hi,

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

helper

Code:
<?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
Code:
<?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

Code:
<?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

Code:
<?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='" . $row['>" . $row['label'] . "</a>";
    
                display_children($row['id'], $level + 1);
    
                echo "</li>";
    
            } elseif ($row['Count']==0) {
    
                echo "<li><a href='" . $row['>" . $row['label'] . "</a></li>";
    
            } else;
    
        }
    
        echo "</ul>";
    
    }
?&gt;
thank in advance
Krystian
#2

[eluser]Lorin[/eluser]
I'm not sure, that is reason for your problem, but i don't see:
Code:
$this->load->helper('yourhelper');
in your controller.

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

[eluser]Krystian[/eluser]
I`m autoloading it.
I`m a little bit confused if I can use

Code:
$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

[eluser]Lorin[/eluser]
In helper, you don't have a class. I think you can't use $this->db->get('menu');
#5

[eluser]Krystian[/eluser]
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 Smile
#6

[eluser]Lorin[/eluser]
What about to use [Models] rather than Helper?
#7

[eluser]Krystian[/eluser]
yeah, of course I can create model ( recursive ), but look on my old function again

Code:
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='" . $row['>" . $row['label'] . "</a>";

            display_children($row['id'], $level + 1, $dbh);

            echo "</li>";

        } elseif ($row['Count'] == 0) {

            echo "<li><a href='" . $row['>" . $row['label'] . "</a></li>";
            //echo "<li class='level".$level."'><a class='level".$level."' href='" . $row['>" . $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

[eluser]Lorin[/eluser]
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

[eluser]Lorin[/eluser]
For example I made simple script to create menu from database:

model:
Code:
&lt;?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();
        }

    }
?&gt;

controller:
Code:
&lt;?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);
        }

    }
?&gt;

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

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

[eluser]Krystian[/eluser]
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 Smile
See you later
and thanks for your posts




Theme © iAndrew 2016 - Forum software by © MyBB