Jump to content

[SOLVED] IF OR statement help


mitcho

Recommended Posts

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

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";
}?>

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?


<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 } ?>>

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.