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

Link to comment
Share on other sites

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
Share on other sites


<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
Share on other sites

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