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 Link to comment https://forums.phpfreaks.com/topic/109394-solved-if-or-statement-help/ 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"; }?> Link to comment https://forums.phpfreaks.com/topic/109394-solved-if-or-statement-help/#findComment-561132 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' Link to comment https://forums.phpfreaks.com/topic/109394-solved-if-or-statement-help/#findComment-561141 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? Link to comment https://forums.phpfreaks.com/topic/109394-solved-if-or-statement-help/#findComment-561167 Share on other sites More sharing options...
trq Posted June 9, 2008 Share Posted June 9, 2008 Where do you define $page? Link to comment https://forums.phpfreaks.com/topic/109394-solved-if-or-statement-help/#findComment-561168 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 } ?>> Link to comment https://forums.phpfreaks.com/topic/109394-solved-if-or-statement-help/#findComment-561170 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. Link to comment https://forums.phpfreaks.com/topic/109394-solved-if-or-statement-help/#findComment-561172 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. Link to comment https://forums.phpfreaks.com/topic/109394-solved-if-or-statement-help/#findComment-561178 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. Link to comment https://forums.phpfreaks.com/topic/109394-solved-if-or-statement-help/#findComment-561180 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! Link to comment https://forums.phpfreaks.com/topic/109394-solved-if-or-statement-help/#findComment-561648 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.