Welcome Guest, Not a member yet? Register   Sign In
Menu - selected item
#1

[eluser]renathy[/eluser]
I have created navigation menu for my website. I do not know how to manage "selected menu item" in my code.

I have the following structure:

-> base controller BaseController extends CI_Controller
-> other controllers, say WelcomePage extends BaseController, ContactPage extends BaseController etc.

BaseController loads Menu library and calls "show_menu" that takes data from database and returns html code for menu.
Menu is dataase driven, so it is loading from database table "navigation". Navigation contains fields (id, title, page(actually this is controller name)).
So, show_menu generates links in the following format anchor($row['url'], $row['title']);

The problem is that I dont understand how to get selected menu item in my Menu class show_menu method.
The only way I have figured out is to use the following code in show_menu:

Code:
...
// get menu items from database
$q = $this->CI->db->query('select * from navigation');
foreach($q->result() as $q_row)
{      
    if ($this->CI->router->class == $q_row->page)
    {
        ... menu item is selected ...
    }
    // other stuff
...

Is it a normal way?
I am new in CI and also I am familiar only with querystring urls where I would send get parameter with menu item id and then compare it with $q_row['id'] from database.
#2

[eluser]InsiteFX[/eluser]
Add these to your BaseController.
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {

    // --------------------------------------------------------------------

    /**
     * Class Variables - protected, private, public and static variables.
     * --------------------------------------------------------------------
     */
    public $controller;            // holds the controller being run.
    public $method;                // holds the method being run.


    // --------------------------------------------------------------------

    /**
     * __construct
     *
     * Constructor    PHP 5+.
     *
     * @access    public
     * @return    void
     */
    public function __construct()
    {
        parent::__construct();

        // Work out controller and method, make them accessable throught the CI instance
        $this->controller    = $this->router->fetch_class();
        $this->method         = $this->router->fetch_method();

    }

}

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB