CodeIgniter Forums
Include a menu on every page? use url helper in a class? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Include a menu on every page? use url helper in a class? (/showthread.php?tid=24033)

Pages: 1 2


Include a menu on every page? use url helper in a class? - El Forum - 10-30-2009

[eluser]John_Betong[/eluser]
[quote author="NateL" date="1256985210"][quote author="InsiteFX" date="1256984997"]

InsiteFX[/quote]

My CSS is set up and working properly.

Code:
$a =    array
      (
          'home'          => '/home',
          'about'         => '/about',
          'portfolio'      => '/portfolio',
          'contact'       => '/contact'
      );
    
    echo '>>>' .$this->uri->uri_string .'<<<';

    foreach($a as $title => $url):
        $toggle = strpos($this->uri->uri_string, $url) ? ' class="active"' : '';

        echo '>>>' .$toggle .'<<<';
        echo "<li $toggle>".anchor($url, $title)."</li>";
    endforeach;                
    unset($toggle);
    unset($a);
&nbsp;
Try this code and see what is happening. I think that you do not have the uri helper loaded.
&nbsp;


Include a menu on every page? use url helper in a class? - El Forum - 10-30-2009

[eluser]NateL[/eluser]
It is being autoloaded in my autoload.php file:

$autoload['helper'] = array('url', 'form');

edit:
The code you told me to try echos out this:
>>>about<<<>>><<<>>><<<>>><<<>>><<<


Include a menu on every page? use url helper in a class? - El Forum - 10-31-2009

[eluser]John_Betong[/eluser]
[quote author="NateL" date="1256985808"]It is being autoloaded in my autoload.php file:

$autoload['helper'] = array('url', 'form');

edit:
The code you told me to try echos out this:
>>>about<<<>>><<<>>><<<>>><<<>>><<<[/quote]
&nbsp;
OK I am back again and had a chance to look at your code and why it is not working.

In your code your $toggle is working because it outputs about.

The problem is that you have not set the third anchor parameter to toggle the CSS setting

Try this:
Code:
foreach($a as $title => $url):
        $toggle = strpos($this->uri->uri_string, $url) ? ' class="active"' : 'off';
        echo "<li $toggle>".anchor($url, $title, $toggle) ."</li>";
    endforeach;                
    unset($toggle);
    unset($a);
&nbsp;
&nbsp;
&nbsp;


Include a menu on every page? use url helper in a class? - El Forum - 10-31-2009

[eluser]NateL[/eluser]
Thanks for your help.

"about" is echoed out the first time because in the code you told me to try, I had this code:
Code:
echo '>>>' .$this->uri->uri_string .'<<<';


So, by adding "off" - my code is outputting this:

>>>about<<<>>>off<<<>>>off<<<>>>off<<<>>>off<<<


Include a menu on every page? use url helper in a class? - El Forum - 10-31-2009

[eluser]John_Betong[/eluser]
[quote author="NateL" date="1257014141"]Thanks for your help.

"about" is echoed out the first time because in the code you told me to try, I had this code:
Code:
echo '>>>' .$this->uri->uri_string .'<<<';


So, by adding "off" - my code is outputting this:

>>>about<<<>>>off<<<>>>off<<<>>>off<<<>>>off<<<[/quote]
&nbsp;
It looks as though you did not try this line of code:
Code:
echo "<li $toggle>".anchor($url, $title, $toggle) ."</li>";

Your $toggle is being populated with the correct values but it is not being passed to the anchor parameter.

The anchor parameter should return this code if and only if the third parameter is passed:
Code:
<a href='/about/'   class='on'>  about   </a>
  <a href='/contact/' class='off'> contact </a>
  <a href='/help/'    class='off'> help    </a>
&nbsp;
&nbsp;
&nbsp;


Include a menu on every page? use url helper in a class? - El Forum - 10-31-2009

[eluser]NateL[/eluser]
I'm not passing it into the anchor. It's a part of my list item.

Code:
<li class="active"><a href="/about">about</a></li>
is what I'm trying to accomplish.

Quote:Your $toggle is being populated with the correct values but it is not being passed to the anchor parameter.

No - unfortunately it isn't.

Even if I did put $toggle in as a parameter for the anchor, it would still show "off".

If I remove this line of code:
Code:
echo '>>>' .$this->uri->uri_string .'<<<';

I get this:
>>>off<<<>>>off<<<>>>off<<<>>>off<<<

That's 4 menu items, and 4 instances of "off". It *should* be:

>>>off<<<>>>active<<<>>>off<<<>>>off<<<


Include a menu on every page? use url helper in a class? - El Forum - 10-31-2009

[eluser]John_Betong[/eluser]
[quote author="NateL" date="1257016152"]I'm not passing it into the anchor. It's a part of my list item.

Code:
<li class="active"><a href="/about">about</a></li>
is what I'm trying to accomplish.

Quote:Your $toggle is being populated with the correct values but it is not being passed to the anchor parameter.

No - unfortunately it isn't.

Even if I did put $toggle in as a parameter for the anchor, it would still show "off".

If I remove this line of code:
Code:
echo '>>>' .$this->uri->uri_string .'<<<';

I get this:
>>>off<<<>>>off<<<>>>off<<<>>>off<<<

That's 4 menu items, and 4 instances of "off". It *should* be:

>>>off<<<>>>active<<<>>>off<<<>>>off<<<[/quote]
&nbsp;
I think that it is the anchor that requires the class toggle to change color and not the link.
&nbsp;
It has been a long day for me and the recent beers does not help so may I suggest that you send the relevant parts of your CSS file and the links that you want to include in your menu and tomorrow I will supply a working example. The example will be online and I will supply the code. I think this is the only way that we will not be at cross-purposes.
&nbsp;
&nbsp;
&nbsp;
edit: spelling - must get a better keyboard Smile


Include a menu on every page? use url helper in a class? - El Forum - 10-31-2009

[eluser]NateL[/eluser]
OK - I got it working.

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

I switched the positions of $url and $this->uri->uri_string. I figured that out by reading up on strpos in the PHP Manual and looking at this code example:

Code:
$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);
Oddly enough, it does not work if I remove the slash in my $a array: /about works. about does not.

Quote:I think that it is the anchor that requires the class toggle to change color and not the link.
All 3 parameters of the anchor helper are optional.


Include a menu on every page? use url helper in a class? - El Forum - 11-01-2009

[eluser]John_Betong[/eluser]
&nbsp;
&nbsp;
&nbsp;
http://johns-jokes.com/menu_home/
&nbsp;
Above is the link I promised.
&nbsp;
&nbsp;
&nbsp;