samoht Posted July 19, 2007 Share Posted July 19, 2007 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) Quote Link to comment Share on other sites More sharing options...
thedarkwinter Posted July 19, 2007 Share Posted July 19, 2007 basename($_SERVER['SCRIPT_NAME']) will return only the name of the page without the path Quote Link to comment Share on other sites More sharing options...
samoht Posted July 19, 2007 Author Share Posted July 19, 2007 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? Quote Link to comment Share on other sites More sharing options...
thedarkwinter Posted July 19, 2007 Share Posted July 19, 2007 Can you not add that the the SQL? $query_rsSANavBars = "SELECT * FROM sanavbars WHERE (Type = 'SA') AND (FlagStatus = 'A') AND (LinkURL == '$page') ORDER BY Name"; That way you just get the on result for the page you are on. Or am i misunderstanding you? Quote Link to comment Share on other sites More sharing options...
samoht Posted July 19, 2007 Author Share Posted July 19, 2007 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" Quote Link to comment Share on other sites More sharing options...
thedarkwinter Posted July 19, 2007 Share Posted July 19, 2007 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? Quote Link to comment Share on other sites More sharing options...
samoht Posted July 19, 2007 Author Share Posted July 19, 2007 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? Quote Link to comment Share on other sites More sharing options...
samoht Posted July 19, 2007 Author Share Posted July 19, 2007 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? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.