我自定义标签是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>