0

I have a <body style="<?php include("gallery.php"); ?>" where gallery.php holds a random array of images, echoing the URL upon each reload.

I would like to have a button to refresh the background image of the body.

I currently am able to do one reload of the image, since the new URL is echoed.

EG:

$(".refresh").click(function() {
    $("body").css("background-image", "url(<?php include("gallery.php"); ?>)");
});

But after the inclusion of the gallery file in the script, it is always the same url.

EDIT My PHP is as follows:

$bg = array(
    array( "url" => "url_1" ),
    array( "url" => "url_2" ),
    ...
);


for ($i=0;$i<1;$i++) {
    $id = array_rand($bg);
    echo "{$bg[$id]['url']}";
    unset($ad[$id]);
};
2
  • 1
    Try separating client and server code and using AJAX. Commented Jan 28, 2013 at 7:45
  • 1
    What is the output of gallery.php? Also, try to get this in a Javascript array...so that you can just replace the values from the array...no need of ajax as well. Commented Jan 28, 2013 at 7:50

3 Answers 3

2

It would be in an another way and gallery.php could outputs an image (not just a url), So you can access gallery.php like this :

$(".refresh").click(function() { 
    $("body").css("background-image", 'url("http://www.mysite.com/gallery.php?'+Math.random()+'")'); 
});

Using Math.random() will passes a random number to avoid caching.

So in gallery.php you have something like :

$bg = array(
    array( "url" => "url_1" ),
    array( "url" => "url_2" ),
    ...
);

shuffle($bg); // Random order

echo file_get_contents($bg[0]['url']);
//instead of echo $url;
Sign up to request clarification or add additional context in comments.

7 Comments

According to the question gallery.php only outputs an URL, not an image. This will not work.
@GeraldSchneider oh i didn't noticed it ;)
@Omid How would I alter my PHP script? I currently have a loop that passes each url and outputs it
@Ahhhhhhhhhhhhhdfgbv Let me see, You have a set of images in your PHP file. Also have an index file that has some elements with background images. Each element should have a random background image from image set ?
@Omid So I have the PHP file with the url of the images, and an index file with the call on the <body> to get the background image. I need a button to refresh the background image url without refreshing the page.
|
1
$(".refresh").click(function() { 
    $.get('gallery.php', function(data) {
        $("body").css("background-image", "url('"+data+"')"); 
    }
});

PHP is server side scripting language and Javascript is client side scripting language. We can't use them like you were using. But you can send request from client to server to faciliate body with URL.

1 Comment

@AspiringAqib This works once, and then the refresh wont reload again
0

IMO Ajax is overkill and piping all images through a php script creates unnecessary memory overhead. For very large images it could even create memory problems on your webserver. The best method would be to put all images in a javascript array, as already suggested. You can have gallery.php output an array of URLs to the images.

[ 'http://url1/', 'http://url2', ... ] // etc

Then you can call it in your file like this:

<script type="javascript">
  var backgroundImages = <?php include("gallery.php"); ?>;
  function refresh() {
    $("body").css("background-image", "url(" + backgroundImages[Math.round(Math.random() * backgroundImages.length)] + ")"); 
  }
  $(function() { refresh(); });
  $(".refresh").click(refresh);
</script>

2 Comments

Commonly, When we use a php file want to add Expire headers and compressing images. Just selecting an image in random order would not be the matter.
@GeraldSchneider Using the include("gallery.php") echoes out a URL. I've added my PHP to see where I can change it to make this work :]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.