Location: PHPKode > scripts > PHPAds classes > phpads-classes/PHPAds.class.php
<?php
/*
* PHP Ad Rotators
* @author Gobinath <hide@address.com>
* @package PHPads
* @version 1.0.1
*/

/*
* Class to display random advt..
*/
class PHPAds{
    
	/*
	* @File Name for storing the Ads Impression	 
	* @var String
	* @access Public
	*/
	var $fileName = 'adrotator.txt';
	 
	/*
	* @Directory Containing the Ad Images 
	* @var String
	* @access Public
	*/	 
	var $AdsDir = NULL;
	 
	/*
	* @Array variable to Store Ads Image Names
	* @var String
	* @access Public
	*/		 
	var $AdsImg = array();    // @Array Initialization
	
	/*
	* @Array variable to Store Final Display Image Path
	* @var String
	* @access Public
	*/		 
	var $AdsImgDisplay = array();    // @Array Initialization	
	 
	/*
	* @Store the Random Image Number
	* @var String
	* @access Public
	*/		 
	var $imgNumber = 0;       // @Variable Initialization
	
	/*
	* @Variable to Store Number of Ads, for Display
	* @var String
	* @access Public
	*/		 
	var $numAds = 0;       // @Variable Initialization	

    /*
    * Class constructor
    *
    * @param string $dir
    * @param string $fName
    * @access public
    */
	 function PHPAds($dir,$num=NULL,$fName=NULL){
		$this->fileName = $fName;  // Assigns the Value to Global variable
		$this->AdsDir = $dir;
		$this->numAds = $num;
		$this->getImagesList();     // Call to getImagesList() Member
		$this->getDisplayImage();   // Call to getDisplayImage() Member
	 }
	 
     /*
     * @get the List of Avaliable Images in the Directory 
     *
     * @access public
     */	 
	 function getImagesList() {
        //
		// @Checks whether the Ads Image Directory Exist
		if($handle = @opendir($this->AdsDir)){   
		       // @Reads the Ads directory
               while(($imgfile = @readdir($handle)) != false){  
			        // @add checks for other image file types here, if need other ext can be added here
			        if ( preg_match("/(\.gif|\.jpg)$/", $imgfile) ) {  //Check for Extension
					   $this->AdsImg[$imgCounter] = $imgfile;    // Adds the File name to the Array   
					   $imgCounter += 1;     // Increase the Counter By 1
					}
               }
			   closedir($handle);     // Close the Directory
		}
	 }

     /*
     * Pick Random Images from the Image array
     *
     * @param string $num
     * @access public
     */
     function getDisplayImage(){
		$i = 0;
  	    $imgDisCount = 0;  // Variable used as the Counter for Display image array
	    shuffle($this->AdsImg);    // Shuffles the AdsImg array
        if($this->numAds == NULL) {  // @Check Whether the param $num is NULL or not
    	  $this->imgNumber = rand(0, (count($this->AdsImg)-1));   // @Assigns a random number to the Variable
		  $this->AdsImgDisplay[0]= $this->AdsDir."/".$this->AdsImg[$this->imgNumber];  // @Add the File path to the AdsImgDisplay
		}
		else{
		  while ($imgDisCount < $this->numAds){
		     $key = array_search($this->AdsImg[$i], $this->AdsImgDisplay); // @Check whether the File available in the Array
			 if($key == NULL){   // @Checks the Key is NULL
			     $this->AdsImgDisplay[$imgDisCount] = $this->AdsDir."/".$this->AdsImg[$i]; // @Assign the Image path to the Display Image array
				 $imgDisCount += 1;     // Increment the Display image Counter
			 }
			 $i += 1;
		  }		    
		}
	 }	
	  
     /*
     * Display Advt. Images
     *
     * @access public
     */
     function AdsDisplay(){	 
	    for($i=0;$i < count($this->AdsImgDisplay);$i++){
		  echo "<img src=\"".$this->AdsImgDisplay[$i]."\"><br><br>";     // Display Random Ads from Array
		}
	 }
}
?>
Return current item: PHPAds classes