FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Random Image





Hobbit
I searched and couldn't find anything. Does someone know how to have a random image with a link and some text?

Look at my site www.bandoftheweek.com , on the right look at "featured band" well I want that to randomize every time it is refreshed. Please post here if you know how, thanks.
wumingsden
Hobbit wrote:
I searched and couldn't find anything. Does someone know how to have a random image with a link and some text?

Look at my site www.bandoftheweek.com , on the right look at "featured band" well I want that to randomize every time it is refreshed. Please post here if you know how, thanks.


Create a new folder, place all of the images that you want to be rotated in this folder. Save the code below as index.php and place it in the folder with the images:

Code:

<?php

//////////////////////////////////////////////////////////////////////
/*                                                                  */

/*               Copyright to Mezmorizing Creations                 */

/*                                                                  */

//////////////////////////////////////////////////////////////////////



if ($dir = opendir("."))

{

     $list = buildimagearray($dir);

     displayrandomimage($list);

}

function buildimagearray($dir)

{

     while (false !== ($file = readdir($dir)))

     {

          if (!is_dir($file) && getimagesize($file))

          {

               $list[] = $file;

          }

     }

     return $list;

}

function displayrandomimage($list)

{

     srand ((double) microtime() * 10000000);

     $sig = array_rand ($list);



     $size = getimagesize ($list[$sig]);

     $fp = fopen($list[$sig], "rb");



     if ($size && $fp)

     {

          header("Content-type: {$size['mime']}");

          fpassthru($fp);

          exit;

     }

}

?>


Wherever you want the images to be displayed use the following link, http://www.yoursite.com/*name_of_image_folder*/index.php

The images will then randomly load on every refresh. Make sure that in the folder there are only images and the index.php file.
n0obie4life
Note that you cannot use it on forums.
Rhysige
That code is useless because it only displays random images not the details that go with them.

if the data is stored in a mysql database use something like this
Code:

$imageQuery = mysql_query("SELECT * FROM `bands`");

while($row = mysql_fetch_object($imageQuery)){
       $imageArray[] = $row;
}

$rand = rand(0,count($imageArray);

$details = $imageArray['$rand'];


From there $details should be an object with all information for that row of the table... havnt tested it obviously.
wumingsden
note that most templates cannot process php which is why it will not work.
Hobbit
Rhysige wrote:
That code is useless because it only displays random images not the details that go with them.

if the data is stored in a mysql database use something like this
Code:

$imageQuery = mysql_query("SELECT * FROM `bands`");

while($row = mysql_fetch_object($imageQuery)){
       $imageArray[] = $row;
}

$rand = rand(0,count($imageArray);

$details = $imageArray['$rand'];


From there $details should be an object with all information for that row of the table... havnt tested it obviously.

True I do want the info to go with it, not just the image. Plus the image is also a link.

I'm still very confused, I don't know that much php, and of course I don't know mysql.
Traveller
Alternatively, you could use preg_find() to search for only image files, so you wouldn't have to worry about having any other files in the directory.

Then, if you don't wish to use MySQL, you could have a corresponding .TXT file for each image file, and the .TXT file would contain the link and the text.

The MySQL approach would be the best way, though, and easier to maintain. The code from wumingsden would just be changed to build the array from the MySQL fetch instead of from the directory listing.

I'm sure wumingsden can handle that, though, so I won't step on his toes.
Rhysige
What are you using to store the information??
Hobbit
I'm using php includes. http://www.bandoftheweek.com/rightsidebar.php
Jamatu
n0obie4life wrote:
Note that you cannot use it on forums.

Yes you can...

Change
Code:
http://www.yoursite.com/*name_of_image_folder*/index.php


To
Code:
http://www.yoursite.com/*name_of_image_folder*/index.php/whatever.png


EG

http://195.137.71.221/images/jd4s.php = http://195.137.71.221/images/jd4s.php/jd4s.png

And in image tags (right click and view location)
Related topics
Random image for your gallery
Dynamic PHP Images
Mk portal & Coppermine...
question : how to display random image in main page?
Random Post on phpBB
Jamatus' Dynamic image store
Random Image Display
PHP Image Questions
Display random image on foreign site
Random images from directory
Random image script
random image code?
I want to output an img url from a random image script
IE8 not allowing new random image on each page (PHP and CSS)
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.