Jump to content

Recommended Posts

I have a php website that i write the navigation and everything in dynamically to each page and i am trying to get it to show the current page you are on (change the css class of the current page link) but i havent been able to figure out how to do this. I would appreciate some help with this. I have tried about everything but im not sure if im just making a tiny error or what is up.

 

Menu Code that is written in dynamically(file named navigation.php)::

 

<li><a href="sw.php?page=home">Home</a></li>

<li><a href="sw.php?page=timeline">Timeline</a></li>

<li><a href="sw.php?page=countries">Countries</a></li>

<li><a href="sw.php?page=photos">Photos</a></li>

<li><a href="sw.php?page=facts">Facts</a></li>

<li><a href="sw.php?page=summary">Summary</a></li>

<li><a href="sw.php?page=memorial">Memorial</a></li>

<li><a href="sw.php?page=aftermath">Aftermath</a></li>

 

 

Menu code that is written dynamically to every page:

 

<ul id="navmenu">

<?php include("navigation.php"); ?>

</ul>

 

 

Is this what you're looking for?

 

<li><a<?php echo (($current_page == 'sw.php?page=home') ? ' class="current_page"' : ''); ?> href="sw.php?page=home">Home</a></li>
<li><a<?php echo (($current_page == 'sw.php?page=timeline') ? ' class="current_page"' : ''); ?>  href="sw.php?page=timeline">Timeline</a></li>

 

Etc.

Is this what you're looking for?

 

<li><a<?php echo (($current_page == 'sw.php?page=home') ? ' class="current_page"' : ''); ?> href="sw.php?page=home">Home</a></li>
<li><a<?php echo (($current_page == 'sw.php?page=timeline') ? ' class="current_page"' : ''); ?>  href="sw.php?page=timeline">Timeline</a></li>

 

Etc.

 

I put my menu all together with it and it comes back with an error.

 

This i sthe code:

<li><a<?php echo (($current_page == 'sw.php?page=home') ? ' class="current_page"' : ''); ?> href="sw.php?page=home">Home</a></li>
<li><a<?php echo (($current_page == 'sw.php?page=timeline') ? ' class="current_page"' : ''); ?>  href="sw.php?page=timeline">Timeline</a></li>
<li><a<?php echo (($current_page == 'sw.php?page=countries') ? ' class="current_page"' : ''); ?> href="sw.php?page=countries">Countries</a></li>
<li><a<?php echo (($current_page == 'sw.php?page=photos') ? ' class="current_page"' : ''); ?>  href="sw.php?page=photos">Photos</a></li>
<li><a<?php echo (($current_page == 'sw.php?page=facts') ? ' class="current_page"' : ''); ?> href="sw.php?page=facts">Facts</a></li>
<li><a<?php echo (($current_page == 'sw.php?page=summary') ? ' class="current_page"' : ''); ?>  href="sw.php?page=summary">Summary</a></li>
<li><a<?php echo (($current_page == 'sw.php?page=memorial') ? ' class="current_page"' : ''); ?> href="sw.php?page=memorial">Memorial</a></li>
<li><a<?php echo (($current_page == 'sw.php?page=aftermath') ? ' class="current_page"' : ''); ?>  href="sw.php?page=aftermath">Aftermath</a></li>

 

 

And for each link it comes back with the error that there is an undefined variable

You have to define $current_page yourself. There's quite a few different ways to do this, and it will depend on what is most efficient for you. One method if executed from sw.php would be:

 

$current_file = basename(__FILE__);
$key = NULL;
$value = NULL;
$get_string = '';
$start = true;
foreach ($_GET as $key => $value) {
    if ($start) {
        $get_string .= '?';
    }
    else {
        $get_string .= '&';
    $get_string .= $key.'='.$value;
}
$current_page = $current_file.$get_string;

Actually, the double = signs caused the error, removing those fixed the error, and the links work.. however the function of changing class to current page doesnt work..

 

The double equal needs to be in there, = is an assign operator that will always be true when assigning a string to a variable. You should not change the ==.

In my haste, I forgot a small detail, apologies, try this instead:

 

$current_file = basename(__FILE__);

$key = NULL;

$value = NULL;

$get_string = '';

$start = TRUE;

foreach ($_GET as $key => $value) {

    if ($start) {

        $get_string .= '?';

        $start = FALSE;

    }

    else

        $get_string .= '&';

    $get_string .= $key.'='.$value;

}

$current_page = $current_file.$get_string;

Well that code doesn't bring back an error now. So thats good i guess, however it's not changing the class of the current page menu button. I put the code in the header.php page which dynamically writes it to all the other page, it should work shouldn't it?

Well that code doesn't bring back an error now. So thats good i guess, however it's not changing the class of the current page menu button. I put the code in the header.php page which dynamically writes it to all the other page, it should work shouldn't it?

 

No, as I indicated this script must be in the sw.php file (and any other file that the user would see in their browser's navigation bar that needs this functionality). Sorry if I didn't emphasize that more clearly.

 

One method if executed from sw.php would be:
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.