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

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

[eluser]InsiteFX[/eluser]
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!
Code:
<?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

[eluser]Krystian[/eluser]
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
Code:
$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

[eluser]InsiteFX[/eluser]
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.
Code:
function getChild($id)
        {
            $menu = "";
            $str = "";
            $s = "select id,title,parentid,link from ms_product where parentid = '$id' ";
            $res = $this->select_row($s);
            $menu .= '<div id="'.$id.'" style="display:none; position:absolute;">';
            $menu .= '<table border="1" cellspacing="0" cellpadding="0" style="border: 1px solid #FDCB55;  border-collapse:collapse;">';
            for ($i=0;$i<count($res);$i++)
            {
                $cnt_of_child = $this->recordCount("select id from ms_product where parentid = '".$res[$i][id]."' ");
                if ($cnt_of_child > 0)
                    $str = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="arrow_white.gif">';
                else
                    $str = " ";


                $menu .= '<tr height="20"><td align="left"  class="aerial12">';
                $menu .= '<div style="padding-left:10px;padding-right:5px; width:125px;" >';


                $menu .= $res[$i][title].$str;
                $menu .= '</div>';
                $menu .= '</td><td align="left" valign="top">';
                $menu .= $this->getChild($res[$i][id]);
                $menu .= '</td></tr>';
            }
            $menu .= '</table>';
            $menu .= '</div>';
            return $menu;
        }
See how getChild calls itself.

InsiteFX
#15

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

Code:
$query = $this->CI->db->get('menu');
#16

[eluser]InsiteFX[/eluser]
Thats because your model is not in the helper!

InsiteFX
#17

[eluser]Krystian[/eluser]
ok, try to not hate me Smile

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

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

[eluser]InsiteFX[/eluser]
I would make into Class Library! Thats how I do mine.

InsiteFX
#19

[eluser]Krystian[/eluser]
ok, based on your previous advices

library, I used CI super object

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




Theme © iAndrew 2016 - Forum software by © MyBB