[thelist] PHP Includes for Random Sidebar Boxes

jono at charlestonwebsolutions.com jono at charlestonwebsolutions.com
Thu Mar 2 11:53:52 CST 2006


> Steve Clason Wrote:
>
>
> <?php
>
> // Function randomImages randomly selects a number of images
> // from a folder and returns them encoded in html tags,
> // one after the other.
> // arguments are:
> //    $num: number of images to display
> //    $folder: path to where the images reside (eg. /images)
> //    $toFolder: prefix for folder path, depends on OS
> //      (like c:/websites/jamaicaJoe)
>
> //  S. Clason
> //  www.topdogstrategy.com
> //  1 Dec 05
>
> // Random number generator
> // selects a ramdom number from within a range
> // takes a number as an argument
>
> function randomNumber($count) {
>    $ran = mt_rand(0, $count-1);
>    return $ran;
>   }
>
> function randomImages ($num, $toFolder, $folder) {
>
>    //make an array of allowed extensions
>    $extList = array();
>    $extList['gif'] = 'image/gif';
>    $extList['jpg'] = 'image/jpeg';
>    $extList['jpeg'] = 'image/jpeg';
>    $extList['png'] = 'image/png';
>
>    //make sure the folder ends in '/'
>    if (substr($folder,-1) != '/') {
>    	$folder = $folder.'/';
>    }
>
>    //make array with files in folder matching ext list
>    $imgList = array();
>    $handle = opendir($toFolder . $folder);
>    while ($file = readdir($handle)) {
>      $file_info = pathinfo($file);
>    	if (isset( $extList[ strtolower( $file_info['extension'])])) {
>        $imgList[] = $file;
>    	}
>    }
>    closedir($handle);
>
>    //make an array of $num length random images
>    $imgCount = count($imgList);
> 	if ($imgCount >= $num) { //make sure enough images in folder
>      $i = 0;
>      $imgs = array();
>      while ($i<$num) {
>        $imageNumber = randomNumber($imgCount);
>
>        $imgPath = $imgList[$imageNumber];
>        //make sure it's not in the array before including
>        if (in_array($imgPath, $imgs)){
>          //do nothing
>        }else{
>          $imgs[$i] = $imgPath;
>          $i++;
>        }
>      }
>    }else{ //create an blank image if not enough in the folder
>    	$im = '<img src="" width="100" height="100" alt="No images are
> available">';
>      $result = $im;
>    }
>
>    //prepare the output
>    $count = count($imgs);
>    for ($j=0; $j<$count; $j++){
>      //format the result for html display
>      @$result .= "<img src=\"" . $folder . $imgs[$j] . "\">";
>    }
>    return $result;
> }
> ?>
>
>


Thanks for the example.  I've looked at three different PHP rotators, and
they all deal with image rotation.  All three are slightly hampered by the
lack of alt tags, or width and height attributes, etc.

All three suggest that it's possible to modify the code to work with
text/html in addition to GIF, JPG, PNG, etc.  I ended up getting a image
rotation example from ALA to work on a [1]test page.

I am NO WHERE NEAR PHP savvy enough to be able to rework the code to get
that working.  I was able to get a random image rotation to work on a
[1]test page, but what I really want to do is swap, randomly, the entire
<dl> area where the images appear.  I have set up another [2]test page
that discusses the rotation of the entire <dl>.

[1]http://www.charlestonwebsolutions.com/test_case/index_test_random.html

[2]http://www.charlestonwebsolutions.com/test_case/index_test_random_dl.html

It seems like it should be easier than it is, considering how easy it is
to do a regular include.  I think I'm gonna' need to cough up a few bucks
and hit a PHP class that the local university?


-- 
Jono Young
Designer | Developer | Illustrator
Charleston Web Solutions
Bringing Higher Standards to the Lowcountry
http://www.charlestonwebsolutions.com



More information about the thelist mailing list