tags:

views:

38

answers:

3

I'm working on a pretty simple page for my "internship." I can't go into much detail, but I can say that it involves sheep, lasers, and everything you've ever wanted. Anyways, I'm trying to get it so that the background changes depending on whether the sun is out or not. I have a function working properly to determine whether it is daytime or not, but I can't get it to display the images.

I've never used CSS before, let alone mixed it with PHP, so I'm pretty sure the issue is this block of code here:

<style type="text/css">
  body
  {
    background-image: url(
    <?php if( day() == 1 ) { ?> 'day_sheep.jpg'); <?php }
          else             { ?> 'night_sheep.jpg)'; <?php } ?>
    background-position: 50% 50%;
    background-repeat: no-repeat;
  }
</style>
+4  A: 

I think you need something like this:

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

Alternatively, you could trim it down by doing something similar to this:

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

Theres no ; after background-image attribute, and 'day_sheep.jpg') must be 'day_sheep.jpg)';
Code:

<style type="text/css">
  body
  {
    background-image: url(<?php if( day() == 1 ) { ?> 'day_sheep.jpg' <?php }
          else             { ?> 'night_sheep.jpg' <?php } ?>);
    background-position: 50% 50%;
    background-repeat: no-repeat;
  }
</style>
Bang Dao
-1 for misunderstanding how PHP closing tags work. If you do it this way you will end up with a CSS declaration similar to this: `background-image: url('day_sheep.jpg';);` The escaping out of PHP negates the need of the semicolon there. Since you have escaped out, you are in HTML, and the semicolon will simply print on screen.
Joseph
+3  A: 

You could also do this (which I think is better)

<body class="<?php echo (day() == 1) ? "day" : "night"; ?>">

Then you have these two classes:

.day {
  background:url('day_sheep.jpg');
}
.night {
  background:url('night_sheep.jpg');
}

EDIT:

If day is being specifically set to "1" inside the day function, this would be decent, but if you wanted to just catch any "true" value you could also use this in the body tag's "class" attribute:

<?php echo (day()) ? "day" : "night"; ?>
NateDSaint