thinkphp自定义标签

我自定义标签是pc。


新建app/Lib/TagLib目录,新建app/Lib/TagLib/TagLibPc.class.php

<?php
	class TagLibPc extends TagLib
	{
		protected $tags = array(
			'pc' => array('attr' => 'limit,order,position','close' =>1) //attr自定义标签的属性列表,close是否闭合(0 或者1 默认为1,表示闭合
		);
		protected $position_arr = array('1'=>'随机关键词','2'=>'随机其他词','3'=>'空链接','9'=>'友情链接');
		
		public function _pc ($attr,$content){ //content是循环的内容,如<li>$v['name']</li>
			$attr 		=		$this->parseXmlAttr($attr);
			$limit		=		$attr['limit']; 
			$order		=		$attr['order']; 
			$num		=		empty($attr['num']) ? 10 : $attr['num'];
			$position	=		$attr['position'];
			if(!empty($position))
			{
				$str .= '<?php ';
				$str .= '$position_data = get_position_data('.$position.','.$num.');'; //这行很关键
				$str .= 'foreach($position_data as $v){ ';
				$str .= 'extract($v);';
				$str .= '?>';
				$str .= $content;
				$str .= '<?php } ?>';
			}else{
				$str = '暂无数据';
			}
			return $str;
		}	
	}

common/common.php

//获取推荐位数据
	function get_position_data($position,$num=10)
	{
		$sql = null;$position_data=null;
		if($num>30 || $num<=0) $num=10;
		switch($position)
		{
			case '9':
				$sql = 'SELECT id,name,keyname FROM `hao_friend_links` WHERE id >= (SELECT floor( RAND() * ((SELECT MAX(id) FROM `hao_friend_links`)-(SELECT MIN(id) FROM `hao_friend_links`)) + (SELECT MIN(id) FROM `hao_friend_links`))) ORDER BY id LIMIT '.$num; 
				break;
			case '1':
				$sql = 'SELECT id,name,keyname FROM `hao_fan_setting` WHERE id >= (SELECT floor( RAND() * ((SELECT MAX(id) FROM `hao_fan_setting`)-(SELECT MIN(id) FROM `hao_fan_setting`)) + (SELECT MIN(id) FROM `hao_fan_setting`))) ORDER BY id LIMIT '.$num; 
				break;
			case '2':
				$sql = 'SELECT id,name,keyname FROM `hao_fan_setting` WHERE id >= (SELECT floor( RAND() * ((SELECT MAX(id) FROM `hao_fan_setting`)-(SELECT MIN(id) FROM `hao_fan_setting`)) + (SELECT MIN(id) FROM `hao_fan_setting`))) ORDER BY id LIMIT '.$num; 
				break;
			case '3':
				$empty_dir_name_arr = array();
				for($i=0;$i<$num;$i++)
				{
					$empty_dir_prefixname = get_dir_prefixname(3,4);
					$empty_dir_prefixname = 'em'.$empty_dir_prefixname;
					$empty_dir_num		  = rand(0,100);
					$empty_dir_url		  =	"http://".$_SERVER['HTTP_HOST'].'/'.$empty_dir_prefixname.$empty_dir_num;
					array_push($empty_dir_name_arr,$empty_dir_url);
				}
				$position_data = $empty_dir_name_arr;
				break;
		}
		if(!empty($sql) && empty($position_data))
		{
			$result = mysql_query($sql);
			$position_data = array();
			while($arr = mysql_fetch_assoc($result))
			{
				array_push($position_data,$arr);
			}
		}
		return $position_data;
	}


app/Conf/config.php
'TAGLIB_LOAD'                  => true,//加载标签库打开
'APP_AUTOLOAD_PATH'=>'@.ORG,@.TagLib',
'TAGLIB_BUILD_IN'           =>'Cx,pc',


在模板中调用

<pc limit="9" position='9' order="id desc" classname='test'>
<li style="height:20px;width:500px;border-bottom:1px solid #ddd;"><a title="{$name}" href="{$name}">{$keyname}</a></li>
</pc>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值