調用方式如下:其中DebugStr這個函數就是類似一個echo。
復制代碼 代碼如下:
DebugStr('$Adv->getContentById($id); $id為廣告編號, 僅調用一條,返回內容為字符串');
echo Adv::getContentById(35);
DebugStr('$Adv->getContentByIdJS($id); $id為廣告編號, 僅調用一條,返回內容為JS字符串');
echo Adv::getContentByIdJS(35);
DebugStr('$Adv->getContentByOrder($id); $id為廣告編號, 僅調用ID倒序一條,返回內容為字符串');
echo Adv::getContentByOrder(4);
DebugStr('$Adv->getContentByOrderJS($id); $id為廣告編號, 僅調用ID倒序一條,返回內容為JS字符串');
echo Adv::getContentByOrderJS(4);
echo '<hr>';
類如下:
復制代碼 代碼如下:
<?php
/**
* 說明:廣告類,方便廣告列表、內容的調用。
* 需要:數據庫類
* 支持:僅支持PHP5,單件模式
*
* @author Zerolone
* @version 2011-1-6 11:32:06
* 調用方法
* Adv::getContentById($id); $id為廣告編號, 僅調用一條,返回內容為字符串
* Adv::getContentByIdJs($id); $id為廣告編號, 僅調用一條,返回內容為JS字符串
* Adv::getContentByOrder($order); $order為對應, 僅調用ID倒序一條,返回內容為字符串
* Adv::getContentByOrderJs($order); $order對應順序, 僅調用ID倒序一條,返回內容為JS字符串
*/
class Adv {
static $Id = 0; //編號
static $Order = 0; //順序
static $JS = 0; //是否使用JS, 0為不使用
/**
* 根據Id, 返回廣告內容,僅調用一條
*
* @param 編號 $id
*
*/
public static function getContentById($id){
self::$Id = $id;
return self::getContent();
}
/**
* 根據Id, 返回廣告內容Js,僅調用一條
*
* @param 編號 $id
*
*/
public static function getContentByIdJS($id){
self::$Id = $id;
self::$JS = 1;
return self::getContent();
}
/**
* 根據Order, 返回廣告內容,僅調用一條
*
* @param 編號 $Order
*
*/
public static function getContentByOrder($order){
self::$Order = $order;
return self::getContent();
}
/**
* 根據Order, 返回廣告內容Js,僅調用一條
*
* @param 編號 $Order
*
*/
public static function getContentByOrderJS($order){
self::$Order = $order;
self::$JS = 1;
return self::getContent();
}
/**
* 生成一個廣告內容,只調用一條
*
* @return 廣告內容
*/
private function getContent(){
$ReturnContent='';
//------------------0-------1--------2--------3
$SqlStr = 'SELECT `pic`, `width`, `height`, `url` FROM '.TABLE_ADV;
if(self::$Id){
$SqlStr.= ' WHERE `id`=' . self::$Id;
}else{
$SqlStr.= ' WHERE `order`=' . self::$Order;
$SqlStr.= ' ORDER BY `id` DESC ';
}
$SqlStr.= ' LIMIT 1';
$MyDatabase=Database::Get();
$MyDatabase->SqlStr = $SqlStr;
if ($MyDatabase->Query ()) {
$DB_Record = $MyDatabase->ResultArr [0];
$FileName = $DB_Record[0];
$Width = $DB_Record[1];
$Height = $DB_Record[2];
$Url = $DB_Record[3];
}
//判斷類型
$FileName_Ext=strtoupper(pathinfo($FileName, PATHINFO_EXTENSION));
if ($FileName_Ext=='SWF'){
//Flash廣告
$ReturnContent = '<table width="' . $Width .'" border="0" cellspacing="0" cellpadding="0">';
$ReturnContent.= '<tr>';
$ReturnContent.= '<td height="' . $Height . '">';
$ReturnContent.= '<table width="100%" border="0" cellpadding="0" cellspacing="0">';
$ReturnContent.= '<tr>';
$ReturnContent.= '<td>';
$ReturnContent.= '<div style="position:absolute; width:0px; height:0px; z-index:1">';
$ReturnContent.= '<div style="position:absolute; left:0px; top:0px; width:' . $Width . 'px; height:' . $Height . 'px; z-index:2; filter:alpha(opacity=0)">';
$ReturnContent.= '<table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">';
$ReturnContent.= '<tr>';
$ReturnContent.= '<td style="cursor:hand" onClick=window.open("'. $Url . '");> </td>';
$ReturnContent.= '</tr>';
$ReturnContent.= '</table>';
$ReturnContent.= '</div>';
$ReturnContent.= '</div>';
$ReturnContent.= '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http<SPAN class=makelinks_urlspan>://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab<!--makelinks--><A class=makelinks_url title=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab style="MARGIN: 1px; VERTICAL-ALIGN: middle" href="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab" target=_blank><IMG style="FLOAT: none; MARGIN: 1px; VERTICAL-ALIGN: middle; WIDTH: 18px; BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; HEIGHT: 18px; BORDER-BOTTOM-STYLE: none" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAYAAABWzo5XAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAPkgAAD5IBBKh+oAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAANFSURBVDiNlZJLaNxVFIe/+3/OZJJMokMzxqTJOJ2kmkYbtBCxosSC0NiN0l11aUXcduNKKF0otBBcuXXhQlCkohUtSIvaSDBVqyEvjW3TxDwmk2Smmf/jPlwkMzXFLDxw+cHhnu+c+ztXGGMYGnrlmcf7jrydakp38T8iCqurt25OjXz8yQefOUtLS6nnnxv+sNBz6BEhBI7dAMDy8gz79hV2FQqh+XtxlrZsTz3XlesbmP3reLe9sLz25OCRl87M/fErm5tFWloewhjwPQfLSiAEgEFrEMT8cO0rcrkncBwbpTTJZNqfnBidc6Io8m3bvq+zYPznyxTyTzE1/SO+30CptM4LQyexLIvpqSs0pzNks30A2I7nWnu9P44ijAHHbuTw4WH8xHazUmkeIUQdUos6yLYt7sxP8tuNy9ytLOF6HkKAZdtMz1wnkfAByGS60EZw+9bYLpADUAo06439pHr7iYVg+i64+4eZi2Fm8SovHk0zsdCAvWRoefRlAgEBsLYc0pZy74FSrqA77dTpxtTMgo3mJMeODTH2y0c83OjU87V7nhCoWG+DPFvgO7sNr0WDb9Pa2kpUKaLKC7S1daK1IVYaKTWhVMRa4fxn9b9NFBaJRAIlV/lp/CLVqiTbfohstkCqKUMsQWm9N2hs7BJSlVlZWcN1XV597RSTk7P09uY5d+5d8gcO0pE7geM2E8Z7gKTStDzQSa4bLlwYoVwuMzg4yMDAAEEQcPbsO8zPz/PN1Tky2ceIJfdAShuk1EiliaWm9cEDjF//nPPn3yMIAlzXRSmF1ppstg1jNOul30mmC0i1M1E1UMTK1CE1LZZC8vkcxhjCMEQphZQSpRTpdJow2EDr7RVbIKlUYyrVmPLWtlZ2VDidjI6OEQQBYRgShiFxHCOlZGJikmRTd/2rWO0dHcurxWJc2ZLUTnlHUy09XPr6O4IgIAgCoigiiiKklHx7ZZzWzEG00kRBZUUUi8Xm02+NfJErDB2N1bbRUun6yHf+/JJnBzvxfQ/PdXE9G8e2+fTiNdrzJ1m8fWMuu/90rzDG8PqZLca/f+NNL9Hcf/8GtZJWGGwmtVaW1tKuaSKZ3nJtb7oh/fT7PX2nNv4BjlSkNxLrX2YAAAAASUVORK5CYII="></A></SPAN>#version=7,0,19,0" width="'.$Width.'" height="'.$Height.'">';
$ReturnContent.= '<param name="movie" value="'.$FileName.'">';
$ReturnContent.= '<param name="quality" value="high">';
$ReturnContent.= '<param name="wmode" value="transparent">';
$ReturnContent.= '<embed src="'.$FileName.'" quality="high" wmode="transparent" pluginspage="http<SPAN class=makelinks_urlspan>://www.macromedia.com/go/getflashplayer<!--makelinks--><A class=makelinks_url title=http://www.macromedia.com/go/getflashplayer style="MARGIN: 1px; VERTICAL-ALIGN: middle" href="http://www.macromedia.com/go/getflashplayer" target=_blank><IMG style="FLOAT: none; MARGIN: 1px; VERTICAL-ALIGN: middle; WIDTH: 18px; BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; HEIGHT: 18px; BORDER-BOTTOM-STYLE: none" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAYAAABWzo5XAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAPkgAAD5IBBKh+oAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAANFSURBVDiNlZJLaNxVFIe/+3/OZJJMokMzxqTJOJ2kmkYbtBCxosSC0NiN0l11aUXcduNKKF0otBBcuXXhQ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" width="'.$Width.'" height="'.$Height.'"></embed></object>';
$ReturnContent.= '</td>';
$ReturnContent.= '</tr>';
$ReturnContent.= '</table>';
$ReturnContent.= '</td>';
$ReturnContent.= '</tr>';
$ReturnContent.= '</table>';
}else{
//圖片廣告
$ReturnContent = '<a target="_blank" href="'.$Url.'"><img src="'.$FileName.'" width="'.$Width.'" height="'.$Height.'" border="0" /></a>';
}
//如果為調用JS方式
if(self::$JS){
$ReturnContent = 'document.write("'.addslashes($ReturnContent).'");';
}
//重置默認值,這裡還是采用單件模式
self::$Id = 0;
self::$Order = 0;
self::$JS = 0;
return $ReturnContent;
}
}
?>