views:

419

answers:

4

Below is my example script:

<li><a <?php if ($_GET['page']=='photos' && $_GET['view']!=="projects"||!=="forsale") { echo ("href=\"#\" class=\"active\""); } else { echo ("href=\"/?page=photos\""); } ?>>Photos</a></li>
<li><a <?php if ($_GET['view']=='projects') { echo ("href=\"#\" class=\"active\""); } else { echo ("href=\"/?page=photos&view=projects\""); } ?>>Projects</a></li>
<li><a <?php if ($_GET['view']=='forsale') { echo ("href=\"#\" class=\"active\""); } else { echo ("href=\"/?page=photos&view=forsale\""); } ?>>For Sale</a></li>

I want the PHP to echo the "href="#" class="active" only when it is not on the two pages:

?page=photos&view=forsale

or

?page=photos&view=projects

I've also tried this and it doesnt work:

<li><a <?php if ($_GET['page']=='photos' && ($_GET['view']!=='projects' || $_GET['view']!=='forsale')) { echo ("href=\"#\" class=\"active\""); } else { echo ("href=\"/?page=photos\""); } ?>>Photos</a></li>
+3  A: 

You can't do:

if ($var !== 'a' || !== 'b') ...

You have to do:

if ($var !== 'a' || $var !== 'b') ...

If you want to clean that code up I would suggest:

function active_view($content, $url, $view) {
  if ($_GET['view'] == $view) {
    return link($content, '#', 'active');
  } else {
    return link($content, $url);
  }
}

function active_page_view() {
  $args = func_get_args();
  $content = array_shift($args);
  $url = array_shift($args);
  $page = array_shift($args);
  if ($_GET['page'] == $page && !in_array($view, $args)) {
    return link($content, '#', 'active');
  } else {
    return link($content, $url);
  }
}

function link($content, $href, $class) {
  $ret = '<a href="' . $href . '"';
  if ($class) {
    $ret .= ' class="' . $class . '"';
  }
  $ret .= '>' . $content . '</a>';
  return $ret;
}

and then your code becomes:

<li><?php echo active_page_view('Photos', '/?page=photos', 'photos', 'projects', 'forsale'); ?></li>
<li><?php echo active_view('Projects', '/?page=photos&view=projects', 'projects'); ?></li>
<li><?php echo active_view('For Sale', '/?page=photos&view=forsale', 'project'); ?></li>

The above is illustrative rather than being final and complete. The point I'm trying to get across is you want to get in the habit of using some kind of templating mechanism even if you don't use a templating library (eg Smarty). You rarely want to embed complex logic into what is basically a view. If you use a library of functions (or objects) to create your markup it gives you a lot of control to escape special characters, automatically put in attributes, validate what you're putting in or whatever.

In this example, you probably want to have a data structure that represents your site navigation into which you enter what the current page is and it compares it to all the entries when dynamically constructing the navigation and automatically manipulates the links.

cletus
+2  A: 

In addition to the problem cletus pointed out you also have an issue with the precedence of the operators. && has a higher precedence than ||. Therefore

if (
  $_GET['page']=='photos'
  && $_GET['view']!=="projects"
  || $_GET['view']!=="forsale"
)

is equivalent to

if (
  ( $_GET['page']=='photos' && $_GET['view']!=="projects" )
  || $_GET['view']!=="forsale"
)

But you obviously want

if (
  $_GET['page']=='photos'
  && ( $_GET['view']!=="projects" || $_GET['view']!=="forsale" )
)

Maybe not for two alternatives but if you have more options you might want to consider using !in_array(). e.g.

if (
  'photos'=$_GET['page']
  && !in_array($_GET['view'], array("projects","forsale"))
)
VolkerK
The in_array worked beautifully, thank you!
arsoneffect
A: 

<?php $view = $_GET['view'];
if($_GET['page'] == 'photos' && ($view =='projects' || $view == 'forsale'))
{
echo '<li><a href="#" class="active">Photos</a></li>';
}
else
{
echo '<li><a href="/?page=photos">Photos</a></li>';
} ?>

Chris Perkins
A: 
if( $_GET[ 'page' ] != "photos" && $_GET[ 'view' ] != "forsale") {
   ...
} 
else if( $_GET[ 'page' ] != "photos" && $_GET[ 'view' ] != "projects") {
    ...
}
cdnicoll