I downloaded a class from http://www.phpclasses.org/browse/package/1978.html
Here is the code

<?php
/*
* AdRotator : Simple adrotator class
* vedanta dot barooah at gmail dot com
*/

# version 1.2

class AdRotator{
	var $iMax="";
	var $imageIndex="";
	var $imageNames="";
	var $linkNames="";

function AdRotator($images,$links=NULL){
	if(!is_array($images)) die("error: cant find image array");
	else{
		$this->iMax=count($images);
		$this->imageNames=$images;
		$this->linkNames=$links;
	}
}

function rotate($num){
	if($num > $this->iMax) die("error: not enough images to randomize");
	$image_index=array();
	while(count($image_index)<$num){
		$pin=rand(0,$this->iMax-1);
		if(!in_array("$pin",$image_index))array_push($image_index,$pin);
	}
	$this->imageIndex=$image_index;
}

function getImages(){
	$tmp=array();
	for($idx=0;$idx<count($this->imageIndex);$idx++) array_push($tmp,$this->imageNames[$this->imageIndex[$idx]]);
	return $tmp;
}

function showAds($orientation=NULL /* say VERTICAL if you need vertical orientation */,$link_style=NULL,$target="_blank" /* target to open pages in */,$image_style=NULL /* this is the css class that can be used */){
	if(!$link_style){$link_style="text-decoration:none;";}
	if(!$image_style){$image_style="background:#f0f0f0;";}
	$html='';
	for($idx=0;$idx<count($this->imageIndex);$idx++){
		$html.="<a target='".$target."'  style='".$link_style."' href='".$this->linkNames[$this->imageIndex[$idx]]."'><img border=0 src='".$this->imageNames[$this->imageIndex[$idx]]."' style='".$image_style."'></a>\n";
		if($orientation=="VERTICAL")$html.="<br>\n";
	}
	return $html;
}

}
?>

This code goes in the page to display the ads

// include the main class file
 include("adrotator.class.php");
// make a image pool as an array
$image_array = array($image_path);
$links_array = array($ad_url);
 // instance of the class
$o = new AdRotator($image_array,$links_array);
// set the number of images to be shown
//$o->rotate(5);
// get the images in an array
//$img=$o->getImages();

I am getting an error that there are not enough images in the array to rotate.
You can see the image and link arrays at this page

If I specify more that 1 the error comes up. Currently I have the line
$o->rotate(5);
commented out so I donot get the error.

You can see from my page that I am getting data. The first display is all the data in the table. The second is the image paths and the third is the urls for the image links.

I am trying but this stuff is getting real frustrating. Maybe it is my in-expirence with using PHP classes.

There must be an easy way to shuffle ads in a vertical format from data taken from a table. Then Display it.

Any help would be appreciated.

    Well.. How many images do you sepcify you have here:
    this->iMax=count($images);
    (So what does\ count($images) return? Probably less than 5, which is, according to the script, the minimum it needs, I think (Based on the 5 you specified)

      Doesn't the code count how many images are in the array I send over and place them in the iMax variable?

      I have 5 images in the array and I send over the rotate(5) but receive the error.

      I'm just a beginner at this. I figured I throw my arrays at the class and BAM. You can see in the page that I have 5 records in the array.

        Yes:
        this:

        $this->iMax=count($images);
        

        counts the number of file in the array $images

        this:

        if($num > $this->iMax) die("error: not enough images to randomize");
        		$image_index=array();
        

        checks whether you actually have the number of files in the array that you need to shuffle. It should be more or equal to the number of files you want to shuffle (Which is stored in $num).

        All I can think of that this:
        $image_array = array($image_path);
        doesn't look right.
        Have you tried to actually specify the individual files:
        $image_array = array(
        1=> img1.'jpg',
        2=> img1.'jpg',
        3=> img1.'jpg',
        4=> img1.'jpg'.
        5=> img1.'jpg');

          I am not sure if this will help, but here is a WAY easier way to randomize banners or links :

          <?
          $banner=array();
          
          
          $banner[1]='<a href="http://www.somesite.com" target="_blank"><img src="theirbanner.jpg" border=0></a>';
          $banner[2]='<a href="http://www.anothersite.com" target="_blank"><img src="theotherimage.png" border="0"></a>';
          
          /****DO NOT CHANGE*****/
          $fe=rand(0,sizeof($banner)-1);
          echo $banner[$fe];
          ?>
          

          Just add $banner[next #]='the code'; to add more.

          usage : <? include "rotatebanner.php"; ?>

            Thanks, it gave me ideas. Here is what I am doing:

            function display_ads($ad_array)
            	{
            		if ($ad_array)
            		{
            
            		$ads = array();
            		$count = 0;
            		while ($row = mysql_fetch_array($ad_array, MYSQL_NUM))
            
            			{
            				$id = $row['0'];
            				$name = $row['1'];
            				$url = $row['2'];
            				$ad_url = $row['3'];
            				$image_path = $row['4'];
            				$display = $row['5'];
            
            
            				$ads[$count] = "<center><a href=\"$ad_url\"><img src=\"$image_path\" alt=\"$url\" vspace=\"25\" border=\"0\" /></a> <br /></center>";
            				$count = $count++;
            			}
            		@mysql_free_result ($ad_array);
            		return $ads; 
            	}
            }

            Here is what it is displaying

            The first 3 bits of information are counts of the data before and after it is run through the function above. I am not sure why I am only getting the one element of the array built.

            Thanks

              why don't you try something like this :

              $count=0;
              $adsql=mysql_query("SELECT * FROM ads ORDER BY RAND() LIMIT 3");
              while($ad = mysql_fetch_object($adsql)){
              	$ads[$count] = '<center><a href="'.$ad->ad_url.'"><img src="'.$ad->image_path.'" alt="'.$ad->url.'" vspace="25" border="0"></a><br><center>";
              	$count++;
              }
              
              foreach($ads as $display){
              	echo $display;
              }
              

                WOW, that is why you guys are great. I never would have known I could do that in a query.

                Thanks for the ideas.

                Dave

                  I got the shuffel working nicely in a page.

                  But when I try to do the include it gives no appearence. Here is the include code.

                  <td width="180" align="center" valign="top" bgcolor="#999999" >
                       <?PHP include("/php/ads_left.php"); ?>	  
                  </td>

                  I have tried all the different "../", "./" or the "/" paths and what is odd is the only include that works is if I use the absolute "http://www.....". I am currently using the absolute path so you see it working here page link. Notice the left side will shuffel with refreshes but the right side is static.

                  Anyone have an idea on why I need to use the absolute http path??.

                  And Thanks, you guys have allowed me to simplify the code by a ton.

                  The code I finally worked out is:

                  Page code

                  <?PHP
                  
                  require("../.......");
                  
                  
                  $sql = NULL;
                  $result = NULL;
                  $sql = "SELECT * FROM advertisers ORDER BY RAND()";
                  $result = mysql_query($sql, $connection) or die("Could not Excute Query");
                  
                  if ($result)
                  	{
                  		$display = "";
                  		$display = display_ads($result);
                  
                  		if ($display == "")
                  			{
                  				$display = "<center><font color=\"#CC0000\" size=\"4\" face=\"Arial, Helvetica, sans-serif\">Display Error</center></font><br>";
                  			}
                  	}
                  else
                  	{
                  		$display = "";
                  		$display = "<center><font color=\"#CC0000\" size=\"4\" face=\"Arial, Helvetica, sans-serif\">SQL Error</center></font><br>";
                  	}
                  
                  ?>
                  
                  
                  <body bgcolor="#999999"">
                  	<?PHP echo "$display"; ?>
                  </body>

                  Function Code returns html for display:

                  function display_ads($ad_array)
                  	{
                  		if ($ad_array)
                  		{
                  
                  		$ads = "";
                  		while ($row = mysql_fetch_array($ad_array, MYSQL_NUM))
                  
                  			{
                  				$id = $row['0'];
                  				$name = $row['1'];
                  				$url = $row['2'];
                  				$ad_url = $row['3'];
                  				$image_path = $row['4'];
                  				$display = $row['5'];
                  
                  
                  				$ads .= "<center><a href=\"$ad_url\" target=\"_blank\"><img src=\"$image_path\" vspace=\"25\" border=\"0\" alt=\"$url\"/></a> </center><br />";
                  			}
                  		@mysql_free_result ($ad_array);
                  		return $ads; 
                  	}
                  }
                    Write a Reply...