tags:

views:

56

answers:

4

I have a few pages which I'd like to have the same background across, so I figured an external style sheet would be the way to go. However, the background changes depending on the time of day, so I had to mix some PHP into the CSS. So now I have a file, background.php:

<html>
<style type="text/css">

body
{
    background-image: url('<?php echo (day() == 1 ? 'images/day_sheep.jpg'
                                                : 'images/night_sheep.jpg'); ?>');
    background-position: 50% 50%;
    background-repeat: no-repeat;
    background-color: silver
}

a {text-decoration:none;}
a:link {color:#ff0000;}
a:visited {color:#0000FF;}
a:hover {text-decoration:underline;}

</style>
</html>

Which is being called from two different pages. One page works perfectly fine, but the other page completely broke when I added the line require_once 'background.php', by which I mean nothing displays at all anymore. The offending page is as follows:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Penelope's Conquests</title>

    <?php require_once 'background.php'; ?>

    <style type="text/css">

     table {
         margin: 10px;
         margin-left: 50%;
         margin-right: 50%;
         padding: 12px;
         border: 10px;
         text-align: center;
         background-color: #fffb40;
         border-style: ridge;
         border-collapse: separate;
         border-color: #9c6ad6;
         outline-style: inset;
           }

     td.cap {
            text-transform: capitalize;
            font-variant: small-caps;
            }

     td.str {
            font-weight: bolder;
            font-size: 1.4em;
            }

    </style>
</head>
<body>
    <h2>To date, Queen Penelope has destroyed:<br /><br /></h2>


    <?php

    require_once 'victims.php';
    require_once 'mysqlSheep.php';



   echo '<table border="3"
                frame="box"
                cellpadding="5">
         <caption>Penelope\'s Victims.</caption>
         <tr><th>Victim</th><th>Times Zapped</th>';

       $hits = mysql_query("
               SELECT *
               FROM victims
               ORDER BY amount
                          ");

       if( $hits )
       {
            while( $row = mysql_fetch_array($hits) )
            {
                   echo '<tr><td class="cap">'.$row['victim'].'</td>
                         <td>'.$row['amount'].'</td></tr>';
            }
       } 
       else
       {
            echo '<p>' . mysql_error() . '</p>';
       }

   echo '</tr></table><br /><br />';

   echo '<table border="3"
                frame="box"
                cellpadding="5">
         <caption>Button Clicks.</caption>
         <tr><th>Hour of day</th><th>Times Clicked</th>';

   $time = mysql_query("
                       SELECT *
                       FROM time
                       ORDER BY hits
                       ");

   while( $row = mysql_fetch_array($time) )
   {
       print "<tr><td class='str'>".$row['hour']."</td>";
       print "<td>".$row['hits']."</td></tr>";
   }


    ?>
</body>
</html>

Why doesn't the page want to behave with the style sheet?

+1  A: 

You've got an <html> tag inside your PHP stylesheet, which means you will get duplicate <html> tags.. not nice

Remove that and just use the <style> tags..

background.php

<style type="text/css">

body
{
    background-image: url('<?php echo (day() == 1 ? 'images/day_sheep.jpg'
                                                : 'images/night_sheep.jpg'); ?>');
    background-position: 50% 50%;
    background-repeat: no-repeat;
    background-color: silver
}

a {text-decoration:none;}
a:link {color:#ff0000;}
a:visited {color:#0000FF;}
a:hover {text-decoration:underline;}

</style>
Marko
That still doesn't fix it. :(
Andrew
@Andrew but it should. If it doesn't, validate your HTML source.
Pekka
A: 

Ohhh, the issue is that the function day() is defined in a separate file, I forgot to move it over when I was reorganizing the site.

Andrew
+1  A: 

Another option is to attach your css file with the <link> attribute.

So in your background.php place

<?php
    header("Content-type: text/css");
?>
body
{
    background-image: url('<?php echo (day() == 1 ? 'images/day_sheep.jpg'
                                                : 'images/night_sheep.jpg'); ?>');
    background-position: 50% 50%;
    background-repeat: no-repeat;
    background-color: silver
}

a {text-decoration:none;}
a:link {color:#ff0000;}
a:visited {color:#0000FF;}
a:hover {text-decoration:underline;}

And then call it using

<link rel="stylesheet" type="text/css" href="background.php" />

Placing this in 1 answer would get messy which is why I've added another one.

Marko
A: 

If you are testing your site on localhost, then try

require_once(dirname(background.php) . "/background.php");

instead of require_once(background.php);

Jevgeni Bogatyrjov