Hi, I am developing a dynamic website and i want to highlight the current page but i'm having some trouble in applying the styles to the links. Here is what i've got so far:

<?php
function checkPage($fPage)
{
  $fCurrent = $_GET['title'];
  if($fPage = $fCurrent)
  {
	  
     $fStyle = 'class="activelink"';
     return $fstyle;
  }
}
?> 

//Navigation Code

<?php do { ?>       
        <div class="nav"><a <?php checkpage($row_nav_links['page_title'])?> href="index2.php?page=<?php echo $row_nav_links['id']; ?>&title=<?php echo $row_nav_links['page_title']; ?>"><?php echo $row_nav_links['link_text']; ?></a></div>
        <?php } while ($row_nav_links = mysql_fetch_assoc($nav_links)); ?>

i got the code from a website and i'm obviously missing something but i'm new to php and cant find out why. I was also going to add an else statement to the function as i also need to apply an inactivelink class to the remaining links.

Any help is appreciated.

Recommended Answers

What do you want to do?
What is your level of programming?

Jump to Post

how do you know the active page?

Jump to Post

All 5 Replies

What do you want to do?
What is your level of programming?

I was trying to highlight the link for the current page by applying an activelink class to current page link and inactive for the rest.

The way i think it should work is by calling the function when each link is created which would check the current row_page_title against the url variable "title" and if they match add class="activelink" to the matched link and class="inactivelink" to the remaining links.

I am new to php so i'm not sure if its a syntax error or just a flaw in the way it works but as i said i'm using code from a post on a different website and trying to adapt it to fit my needs.

If there is a better solution feel free to change it though

how do you know the active page?

I check the active page by comparing the url variable TITLE to the page title from my database. I am going to switch this to the page ID although i dont think that should affect the rest of the code so if i can just get that working.

I have found another solution and its finally working!!

<?php do { 
       

$navList= ''; //this declares a compound variable for posting content to the html page

    $pageid= $row_nav_links['id'];
	$title= $row_nav_links['page_title'];
	$link_text= $row_nav_links["link_text"];
    if($pageid == $_GET['page']){
        $active='class="activelink"';
    }else{
        $active='class="inactivelink"';
    }
    
    $navList.= '<div class="nav"><a href="index3.php?page=' . $pageid . '&title='. $title.'"'. $active .'>'. $link_text .'</a></div>';
	echo $navList;

 } while ($row_nav_links = mysql_fetch_assoc($nav_links)); ?>

then i just created a couple of css classes for the active and inactive state

a.inactivelink {
	colour: #fff;
	color: #000;
}
a.activelink {
	colour: #ddd;
	color: #FFF;
	text-decoration: none;
}

Thanks for trying to help though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.