首頁 >php教程 >php手册 >分享一个调用广告位的代码(函数或Widget类)

分享一个调用广告位的代码(函数或Widget类)

WBOY
WBOY原創
2016-06-07 11:44:211006瀏覽

自己写的一个调用广告位的函数和Widget类
首先是函数:缓存,自定义广告位模板。<?php <br /> function Ad($id=0){<br>     if(intval($id)){<br>         $ad = S("ad_".$id);<br>         if(empty($ad)){<br>             $ad = '';<br>             $adinfo = M('adplace')->find($id);<br>             if(!$adinfo || $adinfo['status']==0){<br>                 return '广告位已经关闭';<br>             }else{<br>                 import(Think.Core.View);<br>                 $view = new View;<br>                 $template = APP_PATH."Tpl/Home/Ad/".($adinfo['tpl']?$adinfo['tpl']:'index').".html";<br>                 if(!is_file($template)){return '模板路径错误';}<br>                 $to = APP_PATH.'Runtime/Cache/'.GROUP_NAME.'/'.md5($template).'.php';<br>                 $width = $adinfo['adwidth'];<br>                 $height = $adinfo['adheight'];<br>                 $typeid = $adinfo['typeid'];<br>                 switch($adinfo['typeid']){<br>                     case 1:<br>                         $adlist = M('ad')->where(array('pid'=>$adinfo['id'],'status'=>1))->order('sort asc,id desc')->select();<br>                         foreach($adlist as $t){<br>                             $v['title'] = $t['title'];<br>                             $v['thumb'] = $t['ad_file'];<br>                             $v['linkurl'] = $t['ad_url'] ? $t['ad_url'] : $t['url'];<br>                             $tags[] = $v;<br>                         }<br>                         if($tags){<br>                             $view->display($template);<br>                             if(!is_file($to)){return '配置错误';}<br>                             ob_start();<br>                             include $to;<br>                             $ad = ob_get_contents();<br>                             ob_clean();<br>                         }<br>                     break;<br>                     case 2:<br>                         $adlist = M('ad')->where(array('pid'=>$adinfo['id'],'status'=>1))->order('sort asc,id desc')->select();<br>                         foreach($adlist as $t){<br>                             $v['title'] = $t['title'];<br>                             $v['thumb'] = $t['ad_file'];<br>                             $v['linkurl'] = $t['ad_url'] ? $t['ad_url'] : $t['url'];<br>                             $tags[] = $v;<br>                         }<br>                         if($tags){<br>                             $view->display($template);<br>                             if(!is_file($to)){return '配置错误';}<br>                             ob_start();<br>                             include $to;<br>                             $ad = ob_get_contents();<br>                             ob_clean();<br>                         }<br>                     break;<br>                     case 3:<br>                         $adlist = M('ad')->where(array('pid'=>$adinfo['id'],'status'=>1))->order('sort asc,id desc')->find();<br>                         if($adlist){<br>                             $view->display($template);<br>                             if(!is_file($to)){return '配置错误';}<br>                             ob_start();<br>                             include $to;<br>                             $ad = ob_get_contents();<br>                             ob_clean();<br>                         }<br>                     break;<br>                 }<br>                 //生成缓存<br>                 S("ad_".$id,$ad);<br>             }<br>         }<br>         return $ad;<br>     }else{<br>         return '没有广告位';<br>     }<br> }<br> ?>下面这个是用Widget类,好像用不了缓存,其实是不会写,脑子不会转弯了。<?php <br /> class AdWidget extends Action{<br>     public function show($id=0){<br>         if($id){<br>             $adinfo = M('adplace')->find($id);<br>             if(!$adinfo || $adinfo['status']==0){<br>                 echo '';<br>             }else{<br>                 $this -> assign('id' , $id);<br>                 $this -> assign('width' , $adinfo['adwidth']);<br>                 $this -> assign('height' , $adinfo['adheight']);<br>                 $typeid = $adinfo['typeid'];<br>                 $this -> assign('typeid' , $typeid);<br>                 switch($adinfo['typeid']){<br>                     case 1:<br>                         $adlist = M('ad')->where(array('pid'=>$adinfo['id'],'status'=>1))->order('sort asc,id desc')->select();<br>                         foreach($adlist as $t){<br>                             $v['title'] = $t['title'];<br>                             $v['thumb'] = $t['ad_file'];<br>                             $v['linkurl'] = $t['ad_url'] ? $t['ad_url'] : $t['url'];<br>                             $tags[] = $v;<br>                         }<br>                         if($tags){<br>                             $this->assign('tags',$tags);<br>                             $this->display("Ad:index");<br>                         }<br>                     break;<br>                     case 2:<br>                         $adlist = M('ad')->where(array('pid'=>$adinfo['id'],'status'=>1))->order('sort asc,id desc')->select();<br>                         foreach($adlist as $t){<br>                             $v['title'] = $t['title'];<br>                             $v['thumb'] = $t['ad_file'];<br>                             $v['linkurl'] = $t['ad_url'] ? $t['ad_url'] : $t['url'];<br>                             $tags[] = $v;<br>                         }<br>                         if($tags){<br>                             $this->assign('tags',$tags);<br>                             $this->display("Ad:index");<br>                         }<br>                     break;<br>                     case 3:<br>                         $adlist = M('ad')->where(array('pid'=>$adinfo['id'],'status'=>1))->order('sort asc,id desc')->find();<br>                         if($adlist){<br>                             $this->assign('adlist',$adlist);<br>                             $this->display('Ad:index');<br>                         }<br>                     break;<br>                 }<br>             }<br>         }else{<br>             echo '';<br>         }<br>     }<br> }<br> ?>调用方法:{:Ad(3)}<br> <hr> <br> {:R('Ad/show',array('id'=>3),'Widget')}我个人喜欢那个Ad($id)函数,调用方便,还能缓存。不知道效率上哪个好一点。对Widget类,不是太明白,刚刚看了资料临时写的。

AD:真正免费,域名+虚机+企业邮箱=0元

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn