明輝手游網(wǎng)中心:是一個免費提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺!

使用PHP完成與ASP Banner組件相似的類

[摘要]<?php /******************************************************* 作者: 吳振亨 (Biz)--phpscript E-mail:wuzhenheng@china.com web: http://phpscript.osh.c...
<?php
/*******************************************************
作者: 吳振亨 (Biz)--phpscript
E-mail:wuzhenheng@china.com
web: http://phpscript.osh.com.cn
文件名:AdRotator.class.php3                 
版權(quán)聲明:                      

如果您使用了本文件,請給作者寫一封E-mail   
如有問題歡迎與作者聯(lián)系
謝謝您的關(guān)注!                     
********************************************************/

/*
廣告條管理程序
public method
    GetAdvertisement($adfile)
     
parameters
    Target=
    Width=
    Height=
    Border=
    Redirect=
     
*/
class AdRotator {
    var $Target="_blank";
    var $Width="460";
    var $Height="60";
    var $Border=0;
    var $Redirect="";
    var $BannerData=array ();
    var $RandTemp=1;
     
function AdRotator()
    {
    }
/*
public  
返回廣告的鏈接字串
*/
function GetAdvertisement($adfile)
    {
    if(count($this->BannerData)==0)
        {
        $this->BannerData=$this->ReadAdFile($adfile);
        }
    $AdData=$this->BannerData;
     
    $uplimited=count($AdData);
     
    //隨機產(chǎn)生一個ID
    srand((double)microtime()*1000000000000000000);
    $id=@rand(1,$uplimited);
                
        while($this->RandTemp==$id)
               {
            srand((double)microtime()*1000000000000000000);
               //$id=@rand(1,count($AdData));
               $id=@rand(1,$uplimited);
               }
           $this->RandTemp=$id;
         
       //echo $id;
       //echo count($AdData);
       //echo $AdData[5]["image"];
         
         
        $LinkString=" <a href="".$this->Redirect;
        $LinkString.="?url=".$AdData[$id]["url"];
        $LinkString.="" Target="".$this->Target;
        $LinkString.="" Title="".$AdData[$id]["title"];
        $LinkString.="" >";
        $LinkString.=" <img border="".$this->Border;
        $LinkString.="" src="".$AdData[$id]["image"];
        $LinkString.="" width="".$this->Width;
        $LinkString.="" height="".$this->Height;
        $LinkString.="" >";
        $LinkString.="</a>";
         
        //echo $LinkString;
        return $LinkString;
    }
/*
private
讀入文件的基本設(shè)置
*/
function ReadAdFile($adfile)
    {
    $FP=fopen($adfile,"r");
    while($buffer=@fgets($FP,1024))
        {
        if(!eregi("#",$buffer))
            {
            if(eregi("^*",$buffer))
                {
                $switchto="off";
                }

            $SetTemp=split("=",$buffer);

            if($switchto=="")
                {
                switch     ($SetTemp[0])
                    {
                    case "REDIRECT":
                    $this->Redirect=$SetTemp[1];
                    break;
                     
                    case "WIDTH":
                    $this->WIDTH=$SetTemp[1];
                    break;
                     
                    case "HEIGHT":
                    $this->HEIGHT=$SetTemp[1];
                    break;
                     
                    case "BORDER":
                    $this->BORDER=$SetTemp[1];
                    break;
                    case "TARGET":
                    $this->Target=$SetTemp[1];
                    break;
                    }
                }
            else
                {
                switch     ($SetTemp[0])
                    {
                    case "image":
                    $i++;
                    $BannerData[$i]["image"]=$SetTemp[1];
                    //echo "<br>";echo $buffer;echo $SetTemp[1];
                    //echo $BannerData[$i]["image"];
                    break;
                     
                    case "url":
                    $BannerData[$i]["url"]=$SetTemp[1];
                    break;
                     
                    case "title":
                    $BannerData[$i]["title"]=$SetTemp[1];
                    break;
                     
                    case "rate":
                    $BannerData[$i]["rate"]=$SetTemp[1];
                    break;
                    }//end of switch
                }//end of if($switch)
            }//end of if(#)
         
        }//end of while

    return $BannerData;
     
    }//end of function

}//end of class

/*
使用方法
廣告設(shè)置文件書寫格式banner.txt
------開始---
REDIRECT=./adredir.asp
WIDTH=468
HEIGHT=60
BORDER=0
*

#注釋

#某某公司廣告1
image=../banner/1.gif
url=http://www.asps0ite.com1
title=One
rate=30
----結(jié)束-----
說明:
與ASP的廣告組件相仿
*表示設(shè)置,其他為圖片的設(shè)置 #:表示注釋

----使用-----
$banner=new AdRotator();
echo $banner->GetAdvertisement("./banner.txt");
echo $banner->GetAdvertisement("./banner.txt");
echo $banner->GetAdvertisement("./banner.txt");
*/
?>  

【本文版權(quán)歸作者與奧索網(wǎng)共同擁有,如需轉(zhuǎn)載,請注明作者及出處】