Welcome Guest, Not a member yet? Register   Sign In
Converting an app to CI (conceptualizing the breakdown)
#8

[eluser]Mirage[/eluser]
Hi -

Not certain if you're just doing this for testing purposes, but you wouldn't make a specific controller for building a menu.

You're essentially on the right path, though it seems like you have a bit of trouble with passing things around and OO practices in general.

Controller
Code:
class SomeController extends Controller {

    // Contsructor
    function SomeController() {
        parent::Controller();
        // we always need the menu, so we can load that model here
        $this->load->model('MenuModel','',true);
    }


    // show some page
    function index() {
        $menu=$this->MenuModel->buildMenu();
        $this->load->view('index_view',compact('menu');        
    }

}


Model
Code:
class MenuModel extends Model {

    // Contsructor
    function MenuModel() {
        parent::Model();
    }


    // build the entire menu as a nested array
    function buildMenu() {
        $menu=$this->getMainMenu();

        if(!is_array($menu)) {
            return array();
        }

        foreach($menu as $idx=>$name) {
            $menu[$idx]['submenu']=$this->getSubMenu($name);
        }

        return $menu;
    }


    // grab the top level menu
    function getMainMenu() {
        $this->db->select('main_menu as name');
        $this->db->group_by('main_menu');
        $query = $this->db->get('inbook_links');
        return ($query && $query->num_rows()>0 ? $query->result_array() : array());
    }

    // grab a submenu given a parent menu
    function getSubMenu($parent_menu) {
        $this->db->select('sub_menu as name');
        $this->db->where('main_menu', $parent_menu);
        $query=$this->db->get('inbook_links);
        return ($query && $query->num_rows()>0 ? $query->result_array() : array());
    }
}


View
Code:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
    &lt;head&gt;
        &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1” /&gt;
        &lt;title&gt;Untitled Document&lt;/title&gt;
    &lt;/head&gt;

    &lt;body&gt;
        <div class="mC">
        &lt;?php foreach($menu as $topId=>$topMenu): ?&gt;
        &lt;div class="mH" onmouseover="toggleMenu(’&lt;?php echo "menu{$topId}" ?&gt;’)"&gt;&lt;?php echo $topMenu['name']?&gt;</div>
            &lt;?php if(!empty($topMenu['submenu'])):?&gt;
            <div id="&lt;?php  echo "menu{$topId}" ?&gt;” class="mL" style="display:none;">
                &lt;?php foreach($topMenu['submenu'] as $subId=> $subMenu): ?&gt;
                <div class="mH">&lt;?php echo $subMenu['name']?&gt;</div>
                &lt;?php endforeach;?&gt;
            &lt;?php endif;?&gt;
        &lt;?php endforeach;?&gt;
        </div>
    &lt;/body&gt;      
&lt;/html&gt;


Code is not tested, but I hope this gets you on your way.

Good luck!
m


Messages In This Thread
Converting an app to CI (conceptualizing the breakdown) - by El Forum - 06-17-2008, 10:42 AM
Converting an app to CI (conceptualizing the breakdown) - by El Forum - 06-17-2008, 01:17 PM
Converting an app to CI (conceptualizing the breakdown) - by El Forum - 06-17-2008, 10:52 PM
Converting an app to CI (conceptualizing the breakdown) - by El Forum - 06-18-2008, 08:08 AM
Converting an app to CI (conceptualizing the breakdown) - by El Forum - 06-19-2008, 12:51 AM
Converting an app to CI (conceptualizing the breakdown) - by El Forum - 06-19-2008, 02:18 AM
Converting an app to CI (conceptualizing the breakdown) - by El Forum - 06-19-2008, 02:31 AM
Converting an app to CI (conceptualizing the breakdown) - by El Forum - 06-19-2008, 09:54 AM



Theme © iAndrew 2016 - Forum software by © MyBB