Welcome Guest, Not a member yet? Register   Sign In
Where to store a common variable
#3

[eluser]ramm[/eluser]
Nice!

I did what you said and it worked fine.

This is my config file: (The ids are optional)
Code:
$config['navigation'] = array(
            'home' => array(
                'id'     => 'home',
                'title'  => 'Home',
                'link'   => ''
            ),
            'about' => array(
                'id'     => 'about',
                'title'  => 'About Us',
                'link'   => 'about'
            ),
            'contact' => array(
                'title'  => 'Contact Us',
                'link'   => 'contact'
            )
        );

And here is the helper code
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* menu_ul
* Returns a unordered list with the selected item as current
*/
if ( ! function_exists('menu_ul'))
{
    function menu_ul($sel = 'home')
   {
        $CI =& get_instance();
        $items = $CI->config->item('navigation');
        
        $menu = '<ul class="main_nav">'."\n";
        foreach($items as $item)
        {
            $current = (in_array($sel, $item)) ? ' class="current"' : '';
            $id = (!empty($item['id'])) ? ' id="'.$item['id'].'"' : '';
            $menu .= '<li'.$current.'><a href="'.$item['link'].'">'.$item['title'].'</a></li>'."\n";
        }
        $menu .= '</ul>';
        return $menu;
    }
}

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

/**
* menu_p
* Returns a paragraph with the selected item as current
*/
if ( ! function_exists('menu_p'))
{
    function menu_p($sel = 'home', $separator = '')
   {
        $CI =& get_instance();
        $items = $CI->config->item('navigation');

        $count = count($items);
        $i = 0;
        $menu = "\n".'<p class="bottom_nav">';
        foreach($items as $item)
        {
            $current = (in_array($sel, $item)) ? ' class="current"' : '';
            $id = (!empty($item['id'])) ? ' id="'.$item['id'].'"' : '';
            $menu .= '<a'.$current.' href="'.$item['link'].'"'.$id.'>'.$item['title'].'</a>';
            $i++;
            if($count != $i)
            {
                $menu .= ' '.$separator.' ';
            }
        }
        $menu .= '</p>'."\n";
        return $menu;
    }
}


/* End of file navigation_helper.php */
/* Location: ./system/helpers/navigation_helper.php */

Use
Code:
&lt;?php echo menu_ul('about'); ?&gt;
//Outputs an unordered list with the <li class="current"> for the about link

Code:
&lt;?php echo menu_p('about', '|'); ?&gt;
//Second parameter is separator, optional


Still not sure if i'm loading the config file correctly, but it works fine.

Thank you.


Messages In This Thread
Where to store a common variable - by El Forum - 02-11-2011, 05:40 PM
Where to store a common variable - by El Forum - 02-12-2011, 10:23 PM
Where to store a common variable - by El Forum - 02-12-2011, 10:51 PM



Theme © iAndrew 2016 - Forum software by © MyBB