dd

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

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