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

[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;
#12

[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<<<>>><<<>>><<<>>><<<>>><<<
#13

[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;
#14

[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<<<
#15

[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;
#16

[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<<<
#17

[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
#18

[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.
#19

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




Theme © iAndrew 2016 - Forum software by © MyBB