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

自己写的一个调用广告位的函数和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元

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download

Atom editor mac version download

The most popular open source editor