mitcho Posted June 9, 2008 Share Posted June 9, 2008 Hi, I'm using php to detect which page of my website the user is on and then using that to determine which button in the menu to show as active. but in one case, there is 4 pages which fall under the same button, so i needed to write an IF OR statement so that if the page being viewed is any of the 4 specified page, then the button will remain active. code used is below: <div class="main_menu" <?php if ($page == 'website-portfolio.php' || 'graphic-portfolio.php' || 'multimedia-portfolio.php' || 'academic-portfolio.php') { ?>id="active"<?php }; ?>> However this doesnt work, this button is ALWAYS shown as active even when not on of the the 4 mentioned pages - its active all the time on every page of the site. Can someone please help me to fix this OR statement? Thanks Quote Link to comment Share on other sites More sharing options...
beboo002 Posted June 9, 2008 Share Posted June 9, 2008 instead of if or u can use if then else if condition use this code <?php if ($page == 'website-portfolio.php') { echo "active website-portfolio.php"; } else if ($page == 'graphic-portfolio.php') { echo "active graphic-portfolio.php"; } else if ($page == 'multimedia-portfolio.php') { echo "active multimedia-portfolio.php"; } else { echo "active academic-portfolio.php"; }?> Quote Link to comment Share on other sites More sharing options...
revraz Posted June 9, 2008 Share Posted June 9, 2008 In other words, you have to compare $page each time in your OR statement. Right now you are saying IF $page == 'xxx' || 'yyy' but you should say IF $page == 'xxx' || $page == 'yyy' Quote Link to comment Share on other sites More sharing options...
mitcho Posted June 9, 2008 Author Share Posted June 9, 2008 I tried both methods and neither worked: <div class="main_menu" <?php if ($page == 'website-portfolio.php') { ?>id="active"<?php }elseif ($page == 'graphic-portfolio.php'){ ?>id="active"<?php }elseif ($page == 'multimedia-portfolio.php'){ ?>id="active"<?php }elseif ($page == 'academic-portfolio.php'){ ?> id="active"<?php }; ?>> and <div class="main_menu" <?php if ($page == 'website-portfolio.php' || $page == 'graphic-portfolio.php' || $page == 'multimedia-portfolio.php' || $page == 'academic-portfolio.php') { ?>id="active"<?php }; ?>> Am i doing something wrong? Quote Link to comment Share on other sites More sharing options...
trq Posted June 9, 2008 Share Posted June 9, 2008 Where do you define $page? Quote Link to comment Share on other sites More sharing options...
saint959 Posted June 9, 2008 Share Posted June 9, 2008 <div class="main_menu" <?php if ($page == 'website-portfolio.php' || $page == 'graphic-portfolio.php' || $page == 'multimedia-portfolio.php' || $page == 'academic-portfolio.php') { ?>id="active"<?php }; ?>> is your problem not the ; after your closing the if statement? EDIT: so should it not be: <div class="main_menu" <?php if ($page == 'website-portfolio.php' || $page == 'graphic-portfolio.php' || $page == 'multimedia-portfolio.php' || $page == 'academic-portfolio.php') { ?>id="active"<?php } ?>> Quote Link to comment Share on other sites More sharing options...
revraz Posted June 9, 2008 Share Posted June 9, 2008 Not really required if its just one line of code. Quote Link to comment Share on other sites More sharing options...
mitcho Posted June 9, 2008 Author Share Posted June 9, 2008 sorry saint959 that wasn't the problem - tried ur edit but still no luck. Quote Link to comment Share on other sites More sharing options...
revraz Posted June 9, 2008 Share Posted June 9, 2008 echo $page and see what it's set to. Quote Link to comment Share on other sites More sharing options...
mitcho Posted June 10, 2008 Author Share Posted June 10, 2008 Ok when i echoed $page i got an blank string - i had forgotten to include the $page definition on one of the pages. So, NOW testing again both revraz and saint959 solutions work! Thanks guys for helping me pinpoint the error! also, to respond to thorpe - i define $page at the top of NOW every page, as follows: <?php $page = basename($_SERVER['SCRIPT_NAME']); ?> Thanks - topic solved! 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.