搜索
系统检测到您的用户名不符合规范:

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

浏览:3567 发布日期:2013年08月06日 分类:功能实现
自己写的一个调用广告位的函数和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类,不是太明白,刚刚看了资料临时写的。
收藏
hoogl
积分:4986 等级:LV4
热点推荐
(追記) (追記ここまで)
最新更新

我们

合作

网站

信息

ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。

AltStyle によって変換されたページ (->オリジナル) /