user3414678
user3414678

Reputation: 75

Change background color of a PHP include on different pages?

We are making a website for a school project and we help with some (simple) PHP.

We got a website with 4 pages, we are using includes for the header/navigation. But we want different background colors for our pages, but if we change the color, the color is going to be changed on all the sites because we use includes.

This is an example of a page:

<?php   
        $pagetitle = "Forside";
        require_once("includes/header.inc.php");
?>

###SOME CONTENT FOR THIS PAGE###

<?php   
        require_once("includes/footer.inc.php");
?>

We want to change the color of the < body > tag which is inside in "includes/header.inc.php"

But as I said if we change that color, the color is changed on all the pages where we use the header include.

Is it possible to change this with some PHP?

Our navigation is pretty simple it can bee seen here:

<nav id="menu">
        <a href="./index.php"><img class="navigation" src="./img/forside-billede.png" /></a>
        <a href="./gaestebog.php"><img class="navigation" src="./img/gaestebog-billede.png" /></a>
        <a href="./citater.php"><img class="navigation" src="./img/citater-billede.png" /></a>
        <a href="./koncept.php"><img class="navigation" src="./img/koncept-billede.png" /></a>
    </nav>

Please write if you need any more information in order to help us or if you dont understand our problem. Thanks in advance!

Upvotes: 4

Views: 3540

Answers (3)

S.Pols
S.Pols

Reputation: 3434

You can set the color on each PHP file before you include the header. Then you can use that PHP variable in css in the header.inc.php

<?php   
        $pagetitle = "Forside";
        $bodyColor = "#ff0000";
        require_once("includes/header.inc.php");
?>

###SOME CONTENT FOR THIS PAGE###

<?php   
        require_once("includes/footer.inc.php");
?>

If the css is between your header tag you can do something like this in your css:

....
....
body {
    background:<?=$bodyColor;?>;
}
....
....

If it isn't you could use the style attribute, e.g.:

....
....
<body style="background:<?=$bodyColor;?>;">
....
....

Upvotes: 2

you can change the background color in the HTML code of each of your pages using tags:

<style  type="text/css">
    body{ background-color: #ccc;}
</style>

Upvotes: 0

Pupil
Pupil

Reputation: 23978

<?php
$path_parts = pathinfo(__FILE__);
$page = $path_parts['filename'];// Get the page name
$page = ! empty($page) ? $page : 'index'; // if no page name, set it to `index`
$backgrounds = array(); // define array of background colors.
$backgrounds['index'] = 'BACKGROUND COLOR FOR THIS PAGE'; //assign colors for pages.
$backgrounds['gaestebog'] = 'BACKGROUND COLOR FOR THIS PAGE';
$backgrounds['citater'] = 'BACKGROUND COLOR FOR THIS PAGE';
$backgrounds['koncept'] = 'BACKGROUND COLOR FOR THIS PAGE';
$background = ! empty($backgrounds[$page]) ? $backgrounds[$page] : ''; // get respective color for the page.
?>

header.inc.php

<body style="background-color:<?php echo $background?>">

Upvotes: 0

Related Questions