Welcome Guest, Not a member yet? Register   Sign In
Include a menu on every page? use url helper in a class?
#1

[eluser]haloace[/eluser]
I'm new to CI and the MVC style. What's the best way to include a menu on every page? Can I use a ci helper in a class? Basically, I tried to create a class with my menu (using the url helper) and load it in the controller on each page, but it's not working.

//controller function
function index()
{
$this->load->library('Menu');
$menu = new Menu();

$this->load->view('header',$menu);
$this->load->view('home');
$this->load->view('footer',$menu);


}

//class
class Menu {

$this->load->helper('url');
$this->$url['home'] = anchor(base_url(), 'home', array('title' => 'home'));
$this->$url['about'] = anchor('about/', 'about', array('title' => 'about'));
$this->$url['forum'] = anchor('forum/', 'forum', array('title' => 'forum'));
$this->$url['faq'] = anchor('faq/', 'faq', array('title' => 'faq'));

}


Thanks for any help.
#2

[eluser]DogWin[/eluser]
我用jquery的click事件在同一页表达的.
menu
Code:
<a href="#" id="one">one</a>
<a href="#" id="two">two</a>
<a href="#" id="three">three</a>

<div id="show"></div>

js code like this:
$(document).ready(function(){
  $("#one").click(function(){
     $("#show").load('controller/function');
  });
});
希望能帮助你。
#3

[eluser]daparky[/eluser]
This should help you: http://codeigniter.com/wiki/Header_and_f...very_page/
#4

[eluser]John_Betong[/eluser]
&nbsp;
I have just written a common menu for our latest project. It checks the URL against the menu item title and if there is a match then the class changes to highlight the particular page item title.
&nbsp;

Code:
// CSS file
  #navigation a.on {color:#fff}
  #navigation a.off{color:#bbb}
  #navigation a    {display:inline; background:url(/images/nav.png);text-decoration:none}

  // _top_menu.php file declated in  MY_Controller.php
&lt;?php
  $a =    array
                (
                    'home'      => 'c_five/home',
                    'about'     => 'c_five/about',
                    'archives'  => 'c_five/archives',
                    'contact'   => 'c_five/contact',
                    'writing'   => 'c_five/writing',
                    'subscribe' => 'c_five/subscribe'
                );
    foreach($a as $title => $url):
        $toggle = strpos($this->uri->uri_string, $url) ? 'on' : 'off';
        echo anchor($url, $title, "class='$toggle'") .nbs(15);
    endforeach;                
    unset($toggle);unset($a);
//
&nbsp;
&nbsp;
&nbsp;
edit: spelling and formatting
#5

[eluser]haloace[/eluser]
Thanks all for your input. Learning new techniques is always good. I ended up going for a simple approach.
Code:
//controller
$this->load->helper('url');
include("menu.inc");

//menu.inc
<div>
<a href="&lt;?php base_url() ?&gt;;" title="Home Page">Home</a>
<a href="/forum/" title="Drop us a note.">Forum</a>
<a href="/about/" title="Learn about us">About</a>
</div>
#6

[eluser]jedd[/eluser]
[quote author="haloace" date="1256893585"]
Learning new techniques is always good.
Code:
include("menu.inc");
[/quote]

I'd suggest this is actually a very old technique.

And I'd strongly recommend you don't do this. At the very least turn it into a helper.

If you want to learn a new technique, and actually take advantage of CodeIgniter's features, use Colin's templating system, or the view-preparation in MY_Controller approach.
#7

[eluser]haloace[/eluser]
[quote author="jedd" date="1256921226"][quote author="haloace" date="1256893585"]
Code:
include("menu.inc");
[/quote]

I'd suggest this is actually a very old technique.

And I'd strongly recommend you don't do this. At the very least turn it into a helper.
[/quote]

What is the disadvantage of this technique?
#8

[eluser]NateL[/eluser]
[quote author="John_Betong" date="1256844243"]&nbsp;
I have just written a common menu for our latest project. It checks the URL against the menu item title and if there is a match then the class changes to highlight the particular page item title.
&nbsp;

Code:
// CSS file
  #navigation a.on {color:#fff}
  #navigation a.off{color:#bbb}
  #navigation a    {display:inline; background:url(/images/nav.png);text-decoration:none}

  // _top_menu.php file declated in  MY_Controller.php
&lt;?php
  $a =    array
                (
                    'home'      => 'c_five/home',
                    'about'     => 'c_five/about',
                    'archives'  => 'c_five/archives',
                    'contact'   => 'c_five/contact',
                    'writing'   => 'c_five/writing',
                    'subscribe' => 'c_five/subscribe'
                );
    foreach($a as $title => $url):
        $toggle = strpos($this->uri->uri_string, $url) ? 'on' : 'off';
        echo anchor($url, $title, "class='$toggle'") .nbs(15);
    endforeach;                
    unset($toggle);unset($a);
//
&nbsp;
&nbsp;
&nbsp;
edit: spelling and formatting[/quote]

I was JUST trying to figure this part of my site out. Very helpful code for me! Thanks a bunch!

For some reason tho, every instance that gets echoed out in the foreach statement is in the "off" state. Any ideas why?

Code:
$a =    array
      (
          'home'          => '/home',
          'about'         => '/about',
          'portfolio'      => '/portfolio',
          'contact'       => '/contact'
      );
    
    foreach($a as $title => $url):
        $toggle = strpos($this->uri->uri_string, $url) ? ' class="active"' : '';
        echo "<li $toggle>".anchor($url, $title)."</li>";
    endforeach;                
    unset($toggle);
    unset($a);
#9

[eluser]InsiteFX[/eluser]
Looks like you need to setup the CSS file and the navigation divisions in code.

Enjoy
InsiteFX
#10

[eluser]NateL[/eluser]
[quote author="InsiteFX" date="1256984997"]Looks like you need to setup the CSS file and the navigation divisions in code.

Enjoy
InsiteFX[/quote]

My CSS is set up and working properly. I tested this by swapping the "class='active'" to the "off" position and they all lit up as "active"

if I put in this line of code:

echo $this->uri->uri_string;

it echos "about" - so I'm expecting to see my About page with the active list item, but it doesn't work for me.




Theme © iAndrew 2016 - Forum software by © MyBB