Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Css and PHP

  1. #1
    New to the CF scene
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Css and PHP

    Hello,

    I am trying to have a random background image load each time I have my page load. To do so, I tried the following:

    Code:
    <?php
    $backs[0] = "background1.gif";
    $backs[1] = "background2.gif";
    $backs[2] = "background3.gif";
    $randBackNum = rand(0,2);
    ?>
    
    body {  background: url($backs[$randBackNum]) 50% 50% no-repeat fixed; }
    Except that it seems I can't call the php variable from the css statement because I'm outside the PHP tags. However, if I try to stick the css statement into the PHP tags I naturally get an error as it's trying to interpret that statement as php code....

    As you may have assumed I do not know much about this subject matter; css and php are new to me, I only know basic html, so I would appreciate any help that can be provided. I hope that what I am asking for is simple enough. Thanks!

    -Veni

    P.S. The css formatting is correct for what I want to do when I hard code in an image file.

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,338
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    PHP Code:
    body {  background: url(<?php print $backs[$randBackNum]; ?>) 50% 50% no-repeat fixed; }
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •