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 14 of 14
  1. #1
    New Coder
    Join Date
    Feb 2008
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Load background image depending on dynamic variable

    Hi all,

    I'm receiving an id using $_REQUEST["id"]; in a page called page.php
    This id will be used to change the background image of page.php depending on its value. To do so I'm using:

    Code:
    $num = $_REQUEST["id"];
     
    echo"
    <body  style='background-image: url(/images/";
    if ($num == 63){
    echo"background1.jpg";
    }else{
    echo"background2.jpg";
    }
    echo");width:100%;height:100%;text-align:center;margin:0 auto;'>....
    The id changes every now and then and in all cases it is stored in $num = $_REQUEST["id"]; with no problem, works great, I've been testing it.
    The issue is that once the background is loaded it does not change even if $num equals to 63.
    Cache?? Should I preload the images? What can I do??

    Thanks a ton!!

  • #2
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,374
    Thanks
    8
    Thanked 1,075 Times in 1,066 Posts
    I don't really see a reason why it shouldn't work, but doing it
    like below might be an easier way? Use the ID as part of your background image filename.
    So in the example below, if ID = 2 the style would be:
    background: url(/images/background2.jpg)

    PHP Code:
    <?php
    $num 
    $_REQUEST["id"];

    $style="background: url(/images/background".$num.".jpg); width:100%; height:100%; text-align:center; margin:0 auto; ";
    ?>
    <body style="<?=$style?>">
    ...
    EDIT: "background.jpg" would be the default background if ID does not exist.



    .
    Last edited by mlseim; 03-23-2009 at 02:10 PM.

  • #3
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Just a tip, I don't recommend using "id" it's a primary name for other things. Such as for users is id, and other tables like news would have id.

  • #4
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,374
    Thanks
    8
    Thanked 1,075 Times in 1,066 Posts
    I wouldn't use $_REQUEST either, but to each their own.

  • #5
    New Coder
    Join Date
    Feb 2008
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks to both for answering.
    In both solutions I get the first background but when the id changes it does not change to the second background

    Hope the following may give you a clue:
    In Firebug's console:
    The id is posted. The Post tab shows the id and its value correctly every time a new id is posted. Then in the Response tab for each id I have the correct html. If I copy and paste this html code into a new html document, it shows perfectly in the browser.
    But that's in the Response Tab for each id posted.

    Back in page.php, if I echo $num it shows in the Response tab and also changes every time to its new value coming from the newly posted id. But the echo does not show in page.php just in the Response Tab ... what's going on???
    Happens using $_REQUEST["id"]; and using $_POST["id"]; (and also using echo or print)

    Thanks once again!

  • #6
    New Coder
    Join Date
    Mar 2009
    Location
    Chicago, IL
    Posts
    69
    Thanks
    0
    Thanked 15 Times in 15 Posts
    Copy/paste your whole page.php in here or if it's too big - in pastebin.com .

  • #7
    New Coder
    Join Date
    Feb 2008
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts
    steelaz, page.php is as simply as this:

    Code:
    $num = $_REQUEST["id"];
     echo $num; 
    echo"
    <body  style='background-image: url(/images/";
    if ($num == 63){
    echo"background1.jpg";
    }else{
    echo"background2.jpg";
    }
    echo");width:100%;height:100%;text-align:center;margin:0 auto;'>....
    the rest is just an iframe.

    The iframe contains the page that is posting the id
    It is posting the id using jQuery
    Code:
       
    var thepage = "page.php";
    $.post(thepage,{"id":value dynamically coming from database});
    Any ideas on how to catch this id in page.php?
    Thanks!

  • #8
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    If your going to have a dozen background, store them in the database. If you are storing them in the database then it's super easy, just query for the background and display it. Doesn't require REQUEST, and for this name it "bgid" instead of "id"

  • #9
    New Coder
    Join Date
    Feb 2008
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts
    masterofollies, thanks, I know, but I'm afraid I cannot use database for this matter (

    C'mon, must be a way of getting that id (now I'm calling it "myid" just in case, thanks), it's there, it's already posted. Once I have it, changing the background is piece of cake..

  • #10
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,169
    Thanks
    19
    Thanked 65 Times in 64 Posts
    Can you post a link to the page? Its probably a caching issue but maybe not. Seeing the actually page might help us diagnose the problem.

  • #11
    New Coder
    Join Date
    Feb 2008
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Good idea NancyJ.

    Here's the page:
    http://www.vivocom.es/test/aa/page.php

    And a screenshot so you can see how myid it is echoed in the Response tab (also in the post tab shows myid and the value) but not in page.php:
    http://www.vivocom.es/test/aa/screenshot_page.gif

    If you click in another video, you'll see that myid changes but not the background..

    Thanks everyone once again!

  • #12
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,374
    Thanks
    8
    Thanked 1,075 Times in 1,066 Posts
    You're using an <iframe> ... you didn't mention that before.
    Anything you click within that <iframe> stays in the frame and
    has no affect on the rest of your page. Your <body> has already
    been loaded and now you're working inside the <iframe>.

    Also, declare a content-type and other header information,
    as not declaring puts the browser into "quirks" mode. Not really
    part of your problem, but it will affect other things.

    Just for fun, eliminate the <iframe> and see what happens.

  • #13
    New Coder
    Join Date
    Feb 2008
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Cannot eliminate the iframe, the page inside of it is the one that is posting myid ...
    So, any way of making myid jump from the iframe to the parent page?
    Can I include the page inside of the iframe in the parent page using another formula?

    Thanks!

  • #14
    New Coder
    Join Date
    Feb 2008
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In order to avoid iframes I'm trying to use a php's include but it does not load the javascript of the included page ... has ever happpened to you?


  •  

    Posting Permissions

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