HelloDarkness HelloDarkness - 3 months ago 13
PHP Question

Change background colour in navbar if url contains right string

I would like to make navbar, in which background colour of one element would change if user would be on that subpage.

Code for checking if url contains chosen string:

<?php
$url = $_SERVER['REQUEST_URI'];
//echo ($url);
if (strpos($url, 'index.php') == true) {
//echo 'Current page contains index.php';
$atm = "#2275A8";
} else {
$atm = "#00427A";
}
?>


This is part from same file saved as .php file

<?php
header("Content-type: text/css");
?>
.ico1 {
background: <?php echo $atm; ?>;
}


If i put my "ckecking part" od code on index, it is working nicely; returning true or false, but for some reason it's not passing parameter into style file.

So now i put "checking part" into style file but, no mater on which subpage i am, its returning same result (always true even if 'index' is not part of url).

Any idea on how to deal with this? :)

Answer

style.css

.ico1 {
   background: color1;
}

.ico2 {
   background: color2;
}

index.php

<?php
$url = $_SERVER['REQUEST_URI'];
//echo ($url);
if (strpos($url, 'index.php') == true) {
    //echo 'Current page contains index.php';
    $atm = "ico1";
} else {
    $atm = "ico2";
} 
?>

...

<div class="<?php echo $atm;?>"> // navbar
Comments