faqts : Computers : Programming : Languages : PHP : Sample Code

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

9 of 11 people (82%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How can I create a random ads script that might have links with some images?

Aug 12th, 2004 10:02
Daniel O,


#displays random ads after taking in an array of image locations/hrefs
function randomAd($ad,$gap){ /*v. 1.1.0*/
  $size=sizeof($ad);
  for($i=0;$i<$size;$i++){
    $randomNumber=floor(mt_rand(0,$size-1));
    $random[$i]=$randomNumber;
    $fail=false;
    //checks to see if the random ad IS unique and $fail becomes true
    if($i>0){
      for($j=0;$j<$i;$j++){
        $check=$random[$j];
        if($check==$randomNumber) $fail=true;
      }
    }
    //if $fail is true, then it pretends that none of this ever happened
    if($fail==true) $i--;
    //if $fail is still false, then the random ad is unique
    else if($fail==false){
      $elementsSize=sizeof($ad[$randomNumber]);
      //checks if the element is 1D or 2D
      if($elementsSize<2){
        print "$gap<tr>\n$gap\t<td><img src=\"";
        print $ad[$randomNumber];
        print "\" alt=\"\" /></td>\n$gap</tr>\n";
      }
      //a 2D element means that it has the link location in it
      else if($elementsSize==2){
        print "$gap<tr>\n$gap\t<td><a href=\"";
        print $ad[$randomNumber][0];
        echo '"><img src="';
        print $ad[$randomNumber][1];
        print "\" alt=\"\" /></a></td>\n$gap</tr>\n";
      }
  }
}