Hello and welcome to our community! Is this your first visit?
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
    Thanked 0 Times in 0 Posts

    Css and PHP


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

    $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!


    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
    Your Monitor
    Thanked 547 Times in 534 Posts
    PHP Code:
    body {  background: url(<?php print $backs[$randBackNum]; ?>) 50% 50% no-repeat fixed; }
    I can't really think of anything to write here now...


    Posting Permissions

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