MyBB Community Forums

Full Version: Display a random image
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I want to be able to display a random image on index page. The image would be pulled from a set folder on my server.

What would i need to do to be able to do this?
A very simple way would be generating a random number between 1 and n, where n is the total number of pictures. Then you rename your pictures with numbers from 1 to n and show them f.e. like this:
srand((double)microtime()*1000000); 
$rand = rand(0,20); 

$randimg = "<img src=\"http://youwebserver.com/folder/".$rand.".jpg\" alt=\"\" />";
Thanks so far. Where would i put that code?
Where do you want to show the image? If you only want it to show on the index page you can put the code in the index.php (before
eval("\$index = \"".$templates->get("index")."\";");
) and then add the variable $randimg to the template index.
What if i wanted to show it on every page.
The same thing in global.php.
Great ok thanks so in global.php where do i put -

srand((double)microtime()*1000000);
$rand = rand(0,20);

$randimg = "<img src=\"http://youwebserver.com/folder/".$rand.".jpg\" alt=\"\" />";

And then i take it i put $randimg in the header.
Add it before
// Set up some of the default templates
eval("\$headerinclude = \"".$templates->get("headerinclude")."\";");
(Somewhere around line 338)
Try putting it after

// Run global_start plugin hook now that the basics are set up
$plugins->run_hooks("global_start");
I put it after

// Set up some of the default templates
eval("\$headerinclude = \"".$templates->get("headerinclude")."\";");

and it works most of the time but some times no image shows.
Pages: 1 2