amuck-landowner

PHP Included header class active

shovenose

New Member
Verified Provider
So, here's an example line in header.php


<li <?php if ($pageTitle=="Home") echo 'class="active"'; ?>><a href="index.php">Home</a></li>

Here is what I do at the top of each page of the site


<?php $pageTitle = 'Internet'; require_once('header.php'); ?>

Now, I have a drop down. How would I do if the title is Web Hosting or Virtual Private Servers or Dedicated Servers to active it.


li <?php if ($pageTitle=="Web Hosting") echo 'class="active"'; ?> class="dropdown">

Thanks!
 

shovenose

New Member
Verified Provider
Parse error: syntax error, unexpected '||' (T_BOOLEAN_OR) in /home/marin/public_html/header.php on line 50

Code:
<li <?php if ($pageTitle=="Web Hosting") || ($pageTitle=="Colocation") echo 'class="active"'; ?> class="dropdown">
 

Supicioso

New Member
In php you don't use || that's for C, C++, C# etc. You use OR if you want either variable to be true. Or AND if you want all variables to be true.


<li <?php if ($pageTitle=="Web Hosting" OR $pageTitle=="Colocation") echo 'class="active"'; ?> class="dropdown">
You could also use a switch.

Code:
<?php 

switch($pageTitle)
{
    case "Title1": case "Title2":
       echo 'class="active"';
       break;
}

?>
 
Last edited by a moderator:

MCH-Phil

New Member
Verified Provider
In php you don't use || that's for C, C++, C# etc. You use OR if you want either variable to be true. Or AND if you want all variables to be true.


<li <?php if ($pageTitle=="Web Hosting" OR $pageTitle=="Colocation") echo 'class="active"'; ?> class="dropdown">

In PHP both are acceptable... 

I'd suggest looking here... http://www.php.net/manual/en/language.operators.precedence.php

Also shovenose didnt we answer this a few months back for you?  I could be wrong. :(
 

Steven F

New Member
Verified Provider
In php you don't use || that's for C, C++, C# etc. You use OR if you want either variable to be true. Or AND if you want all variables to be true.

<li <?php if ($pageTitle=="Web Hosting" OR $pageTitle=="Colocation") echo 'class="active"'; ?> class="dropdown">
You could also use a switch.
Code:
<?php 

switch($pageTitle)
{
    case "Title1": case "Title2":
       echo 'class="active"';
       break;
}

?>
You're wrong. Using the or operator || is perfectly doable and probably better form, though I'm not too familiar with proper PHP programming conventions. I don't know why you'd post something like that without knowing what you're talking about... Also, please don't use a switch, they're really awful.
 

lbft

New Member
In php you don't use || that's for C, C++, C# etc.
Please read http://php.net/manual/en/language.operators.logical.php. || and or both do the same thing, except at different precedences.

Parse error: syntax error, unexpected '||' (T_BOOLEAN_OR) in /home/marin/public_html/header.php on line 50








<li <?php if ($pageTitle=="Web Hosting") || ($pageTitle=="Colocation") echo 'class="active"'; ?> class="dropdown">
The entire expression used in the if statement must be wrapped in brackets.

I have to say, this whole thread is quite hilarious in its terribleness.
 
Last edited by a moderator:

NickM

New Member
Verified Provider
I would do something like this:

Code:
<?php
$links = array(
        'Home' => '/index.php',
        'VPS' => '/vps.php',
);

$pageTitle = 'Whatever';

foreach ($links as $key => $href) {
        echo '<li class="dropdown'.($key == $pageTitle ? ' active' : '').'><a href="'.$href.'">'.$key.'</a></li>';
}

?>
 

RiotSecurity

New Member
In php you don't use || that's for C, C++, C# etc. You use OR if you want either variable to be true. Or AND if you want all variables to be true.


<li <?php if ($pageTitle=="Web Hosting" OR $pageTitle=="Colocation") echo 'class="active"'; ?> class="dropdown">

You could also use a switch.


<?php

switch($pageTitle)
{
case "Title1": case "Title2":
echo 'class="active"';
break;
}

?>
Are you fucking kidding me? Go back to school.
 

RiotSecurity

New Member
<?php

function checkPage() {

    $pages = array('Home', 'Colocation', 'Other');

    $url = $_SERVER['REQUEST_URI'];

    foreach($pages as $page_name) { if(stristr($url, $page_name)) { $page_active = $page_name; } }

}

?>

This code will have errors in it, too lazy to run it though my server to verify if it's working or not, but thats the general idea.
 
Top
amuck-landowner