[thelist] PHP Includes for Random Sidebar Boxes

Steve Clason stevec at topdogstrategy.com
Thu Mar 2 09:27:13 CST 2006


jono at charlestonwebsolutions.com wrote:

> I have about 6 different sidebar boxes I would like to randomly insert in
> my pages.  Ideally, I would only like to have two or three sidebar boxes
> in each page's sidebar.
> 
> I am very new to PHP and includes, but I think that an include can do the
> trick as follows:
> 
> 
> 1. Save each snippet of html for the 6 boxes in a directory called sb-boxes
> 
> 2. Name each snippet sb-box1.inc, sb-box2.inc, sb-box3.inc, and so on
> 
> 3. In my page(s) add something like the following:
> <div id="sidebar">
> <!-- START: Sidebox -->
>       <?php include('http://path/to/sb-boxes'); ?>
> <!-- END: Sidebox -->
> 
> <!-- START: Sidebox -->
>       <?php include('http://path/to/sb-boxes'); ?>
> <!-- END: Sidebox -->
> </div>
> 
> I know that the include code above will not do any randomizing, that is
> just as an example of how I think it might possible work.  How do I go
> about inserting the snippets randomly using this, or a similar approach?

Your approach is OK. Below is a function I wrote for randomly selecting 
and displaying a number of image files from a directory.  I think you 
could modify it pretty easily to randomly select a number of html files 
from a directory and include them in the way you described.

It might get you started anyway.

-- 

Steve Clason
Web Design & Development
Boulder, Colorado, USA
www.topdogstrategy.com
(303)818-8590


<?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;
}
?>






More information about the thelist mailing list