Jump to content

Recommended Posts

I followed a tutorial sometime back and now I want to use this script unfortunately the tutorial at the time did not cover what I am looking for but I did end up with the code below.

 

I am wanting to have an active class dependant on users selection. I would appreciate any help.

<?php
	
	echo '<ul id="nav">';
	$cat_sql="SELECT * FROM administration";
	$cat_query=mysqli_query($con, $cat_sql);
	// calling data put into associative array
	$cat_rs=mysqli_fetch_assoc($cat_query);				
		do{ ?>
		<li><a <?php if ($current_page == "name") { ?><?php } ?>href="index.php?page=<?php echo $cat_rs['link_name']; ?>"><?php echo $cat_rs['name']; 		 
			 ?></a></li>
<?php
	} while ($cat_rs=mysqli_fetch_assoc($cat_query)); 
	echo "</ul>";
?>
<!-- need to add - class='active' -->

The code above gets the field names for the buttons but I am unable to work out how to have an active class.  The code is messy but have been trying all sorts to sort this.

 

Thanks again for any pointers/help.

 

:pirate:

Link to comment
https://forums.phpfreaks.com/topic/298727-php-active-class-for-nav-buttons/
Share on other sites

$curPg = $_SERVER['PHP_SELF']; // OR $curPg = $_SERVER['REQUEST_URI'];
while($cat_rs = mysqli_fetch_assoc($cat_query)){
	print("<li><a href='index.php?page={$cat_rs['link_name']}'");
	if($cat_rs['link_name'] == $curPg){
		print(" class='active'");
	}
	print(">{$cat_rs['name']}</a></li>");
}

You can use PHP_SELF or REQUEST_URI depending on what's stored in the 'link_name' column of your administration table. Or, you may have to do a bit more finessing on the data before the comparison. The important thing is that you get the current page and compare it to the value of link_name. If they match, you're on the current page and append the active class to that link.

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.