新手练手,欢迎吐槽<?php <br />
<br>
/**<br>
* 通用自定义定义标签<br>
* 用法:<br>
* 1:载入<br>
* <taglib></taglib><br>
* 2:调用<br>
* <list><br>
* {$v.id} -- {$v.name}<br><br>
* </list><br>
* 说明:<br>
* 1.可以直接在标签上面写sql属性<br>
* 2.限制查询条数请用 limit属性<br>
* 3.要数据分页请用page属性,可传2个参数,用逗号分隔,前一个参数当前页码,后一个参数每页查询几条<br>
* 4.显示分页请在视图页面输出 {$pageStr}<br>
* 5.调用模型的方法 用 method="getOne(1,2,3)" 【引号里面直接调用方法】<br>
*/<br>
<br>
namespace Think\Template\TagLib;<br>
<br>
use Think\Template\TagLib;<br>
<br>
class Lx extends TagLib<br>
{<br>
<br>
protected $tags = array(<br>
'list' => array('attr' => 'table,order,limit,where,join,field,page,relation,method,cache,dump,sql', 'close' => 1),<br>
);<br>
<br>
public function _list($attr, $content)<br>
{<br>
$defaultAttr = explode(',', $this->tags['list']['attr']);<br>
$vars = array();<br>
foreach ($defaultAttr as $v) {<br>
if ($attr[$v]) {<br>
$vars[$v] = $attr[$v];<br>
}<br>
}<br>
if (!isset($vars['table']) || empty($vars['table'])) {<br>
return false;<br>
}<br>
//是用模型中的特定方法还是select<br>
if (isset($vars['method'])) {<br>
$select = '->' . $vars['method'];<br>
} else {<br>
$select = "->select()";<br>
}<br>
//limit and page<br>
$limit = '';<br>
<br>
if (!isset($vars['page'])) {<br>
if (isset($vars['limit'])) {<br>
$limit = '->limit("' . $vars['limit'] . '")';<br>
}<br>
} else {<br>
$limit = '->page("$page,$rows")';<br>
}<br>
//dump 调试<br>
$dump = '';<br>
if (isset($vars['dump']) && $vars['dump'] == "true") {<br>
$dump = '<?php dump($data) ?>';<br>
}<br>
//getLastSql 调试<br>
$lastSql = '';<br>
if (isset($vars['sql']) && $vars['sql'] == "true") {<br>
$lastSql = ' echo "<br>" . M()->getLastSql(); ';<br>
}<br>
unset($vars['page'], $vars['limit'], $vars['method'], $vars['dump'], $vars['sql']);<br>
$tmpStr = '';<br>
$where = '';<br>
$join = '';<br>
foreach ($vars as $k => $v) {<br>
if ($k == 'table') {<br>
continue;<br>
}<br>
if ($k == 'where') {<br>
$where = $v;<br>
}<br>
if ($k == 'join') {<br>
$join = $v;<br>
}<br>
$tmpStr .= '->' . $k . '("' . $v . '")';<br>
}<br>
$str = '<?php <br />
$page = I("p", false) ? I("p") : I("page");<br>
$page = empty($page) ? 1 : $page;<br>
$rows = I("rows", 15);<br>
$table = ' . ucfirst($vars['table']) . ';<br>
$data = D("$table")' . $tmpStr . $limit . $select . ';<br>
foreach($data as $k=>$v): ?>';<br>
$str .= $content;<br>
$str .= '<?php <br />
if(!is_array($v)){<br>
break ;<br>
}<br>
endforeach;<br>
'.$lastSql.'<br>
$total = M("$table")->where("' . $where . '")->join("' . $join . '")->count();<br>
$Page = new \Think\Page($total,$rows);<br>
$pageStr = $Page->show();<br>
?>';<br>
$str .= $dump;<br>
return $str;<br>
}<br>
<br>
<br>
}
AD:真正免费,域名+虚机+企业邮箱=0元