Jump to content

find only the current page in url


samoht

Recommended Posts

hello all,

 

I would like to check for the current page in the url to set a class in a navigation bar.

 

this is what I have:

<ul class="<?php if($row_rsSANavbars['LinkURL'] == $_SERVER['SCRIPT_NAME']){echo 'current'; }else{ echo 'adminnav';} ?> one"

 

The problem is that my LinkURL will be only the page (e.g. page.php) where $_SEVER['SCRIPT_NAME'] returns the whole path (e.g. folder1/page.php)

 

How do I return only the page?

 

Does $_SERVER['QUERY_STRING'] return everything after a ? (e.g. page1.php?somequery)

Link to comment
Share on other sites

Thanks for that!

 

now I have another problem. My query:

$query_rsSANavBars = "SELECT * FROM sanavbars WHERE Type = 'SA' AND FlagStatus = 'A' ORDER BY Name";
$rsSANavBars = mysql_query($query_rsSANavBars, $connection1) or die(mysql_error());
$row_rsSANavBars = mysql_fetch_assoc($rsSANavBars);
$totalRows_rsSANavBars = mysql_num_rows($rsSANavBars);

returns the proper records but the

if($row_rsSANavBars['LinkURL'] == $page)

will only look at the fist record. so I need to loop through the records - or store their LinkURL's in an array and then check to see if the match the current page.

 

How might this be done?

Link to comment
Share on other sites

I'm not sure what good that will do me since that will change my query down to just one record.

 

I want all the records to show and I want to be able to check each child against the current page - so when the user navigates to the second or third child the parent still has a class of "current" 

Link to comment
Share on other sites

oh okay then use a while loop:

 

<?php
//...
$rsSANavBars = mysql_query($query_rsSANavBars, $connection1) or die(mysql_error());
$totalRows_rsSANavBars = mysql_num_rows($rsSANavBars);
while ($row_rsSANavBars = mysql_fetch_assoc($rsSANavBars))
{
// blah blah whatever you want to do for each row
   if($row_rsSANavBars['LinkURL'] == $page) 
// blah blah
}

 

is that what you are looking for?

Link to comment
Share on other sites

I think so but it's not working for me.

 

I have:

<?php
$page = basename($_SERVER['SCRIPT_NAME']);
$NavBarsExist = false;

?>

<div id="branding"><!--<p><?php echo $arlink; ?></p>-->
<img src="assets/heading-matchbg.jpg" class="nextgen" title="Next Generation Web Technologies" alt="Next Generation Web Technologies">
<h1><?php echo $_SESSION['SessOrgName']; ?> : Admin, Welcome <?php echo $_SESSION['UserName']; ?>.</h1>
<a href="index.php"><img src="assets/logo_small.png" class="logo" border="0"></a>
<div id="mainnav">
<ul class="<?php 
			mysql_select_db($database_connection1, $connection1);
			$query_rsSANavBars = "SELECT * FROM sanavbars WHERE Type = 'SA' AND FlagStatus = 'A' ORDER BY Name";
			$rsSANavBars = mysql_query($query_rsSANavBars, $connection1) or die(mysql_error());
			$row_rsSANavBars = mysql_fetch_assoc($rsSANavBars);
			$totalRows_rsSANavBars = mysql_num_rows($rsSANavBars);
			while ($row_rsSANavBars = mysql_fetch_assoc($rsSANavBars))
			{
				if($row_rsSANavBars['LinkURL'] == $page){
					echo 'current'; 
				}else{ 
					echo 'adminnav';
				}
			} ?> one" id="parent1"><li><a href=""><b>Secure Access</b><!--[if IE 7]><!--></a><!--<![endif]-->
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul class="sub" id="navlight">
<?php

if ($totalRows_rsSANavBars > 0) {
	do {
		$SANavBarId = $row_rsSANavBars['NavBarId'];
		mysql_select_db($database_connection1, $connection1);
		$query_rsSAUserNavBars = "SELECT * FROM sausernavbars WHERE SAUserId = '$SAUserId' AND SANavBarId = '$SANavBarId' AND FlagStatus = 'A'";
		$rsSAUserNavBars = mysql_query($query_rsSAUserNavBars, $connection1) or die(mysql_error());
		$row_rsSAUserNavBars = mysql_fetch_assoc($rsSAUserNavBars);
		$totalRows_rsSAUserNavBars = mysql_num_rows($rsSAUserNavBars);

		// show navbar if user has access to it
		if ($totalRows_rsSAUserNavBars != 0 || ($totalRows_rsCheckEmpty == 0 && $row_rsSANavBars['NavBarId'] == '112')) {
			$NavBarsExist = true; ?>
			<li><a href="<?php echo $row_rsSANavBars['LinkURL'] ?>" class="blackRed"><?php echo $row_rsSANavBars['Name']; ?></a></li><?php 
		}
	} while ($row_rsSANavBars = mysql_fetch_assoc($rsSANavBars));
}
if ($totalRows_rsSANavBars == 0 || !$NavBarsExist)
	echo '<li><a href="">not available</a></li>'; ?>
</ul>

Which turns up the "not available" as the child link

 

also I have 5 parent tabs that I want to write this for. currently I am just working on the first one - but I was hoping to be able to write one function or loop that would take care of each parent navigation.

 

any ideas?

 

Link to comment
Share on other sites

ok I think I am getting closer.

 

$page = basename($_SERVER['SCRIPT_NAME']);
mysql_select_db($database_connection1, $connection1);
$query_rsSANavBars = "SELECT * FROM sanavbars WHERE FlagStatus = 'A' ORDER BY Name";
$rsSANavBars = mysql_query($query_rsSANavBars, $connection1) or die(mysql_error());
$row_rsSANavBars = mysql_fetch_assoc($rsSANavBars);
$totalRows_rsSANavBars = mysql_num_rows($rsSANavBars);

$links = $row_rsSANavBars['LinkURL'];
$arrlinks[$links][] = $row;
$c = "NA";
foreach($arrlinks as $l)
{
if($l = $page){$c = $row_rsSANavBars['Type'];}
}

 

and then

 

<ul class="<?php if($c == 'SA'){echo 'current';}else{ echo 'adminnav';}  ?> one">

 

gets me almost there, but for some reason $c is returning a 'Type' but not one that matches the current $page

Type is just a field in my db that tells me which parent I'm dealing with (e.g. "SA" is the Type for all the links under "site admin")

 

any idea why this is not changing with the page?

Link to comment
Share on other sites

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.