通过tp取数据的相关操作类 自己写的.和大家分享一个,觉得好的.支持一下
_pageshow = null;
static $_iterateparsecache = array();
$cacheiterateid = md5(serialize($tag));
if ($_iterateparsecache[$cacheiterateid])
return $_iterateparsecache[$cacheiterateid];
$this->templatemodule = $this->setmodule($tag['module']);
if (isset($tag['join'])) {
$this->view($tag['join'],
$this->setviewfield($tag['field']));
unset($tag['field']);
}
$this->setparose($tag);
if (empty($tag['_type']) || $tag['_type'] == _select) {
$list = $this->_select($tag['pagesize'],
$tag['limit'],
$tag['function']);
} else {
$list = call_user_func(array($this, $tag['_type']),
$tag['field'],
$tag['limit'],
$tag['function']);
}
if (false !== $tag['output']) {
return service(showme)->showtable($list,
$this->templatemodule,
($tag['_listfields']
? $tag['_listfields']
: _listfields));
}
return $list;
}
/**
* 确定模型
* @param type $name
* @return type
*/
public function setmodule($name = ) {
$modulename = $name
? ($name == false
?
: $name)
: module_name;
return d(ucwords(parse_name($modulename,
1)));
}
/**
* 对条件过滤
* 过滤规则 : 如本类有的方法,全部交由本类处理,如本类没有的该当 ,全部交由tp的model 类处理
* join 和 table 参数,需要带表前辍
*/
private function setparose($tag = array()) {
if (is_array($tag) && !empty($tag)) {
if (empty($tag['field']) && method_exists($this->templatemodule,
getsqlfields)) {
$_listfields = isset($tag['_listfields'])
? $tag['_listfields']
: _listfields;
$tag['field'] = $this->templatemodule->getsqlfields($this->templatemodule,
$_listfields);
}
$tag['field'] = $this->checkpk($tag['field']);
$insertpears = array('where', 'alias', 'field', 'order', 'limit', 'page', 'group', 'having', 'union', 'distinct', 'lock', 'relation');
foreach ($tag as $key => $value) {
if (in_array($key,
$insertpears)) {
call_user_func(array($this->templatemodule, $key),
$value);
// call_user_func(array(method_exists($this, $key) ? $this : $this->templatemodule, $key), $value);
}
}
if (isset($tag['cache'])) {
$key = md5(serialize($tag));
$this->templatemodule->cache($key);
}
$this->option = $tag;
}
}
public function getpagevar() {
$tpl = %s
;
if (!is_null($this->_pageshow)) {
return sprintf($tpl,
$this->_pageshow);
}
return ;
}
private function _select($pagesize = true, $limit = 20, $func = false) {
if ($pagesize) {
$_modulelistcount = $this->templatemodule->count(*);
if ($_modulelistcount > 0) {
$page = setpage($_modulelistcount,
(int) $pagesize);
$this->setparose($this->option);
$list = $this->templatemodule->limit($page->firstrow . ',' . $page->listrows)->select();
//分页处理 需优化
$this->_pageshow = $page->show(admin);
//查询完成后.清空条件.以免影响下次查询
$this->option = null;
}
} else {
$list = $this->templatemodule->limit($pagesize)->select();
$this->option = null;
}
// dump($this->templatemodule->getlastsql());
if (false !== $func && !empty($list)) {
if (method_exists($this->templatemodule,
$func))
return call_user_func(array($this->templatemodule, $func),
$list);
if (function_exists($func))
return call_user_func($func,
$list);
}
return $list;
}
public function view($join = array(), $viewfields = array()) {
if (!empty($viewfields)) {
$this->templatemodule = new viewmodel();
}
$thisviewfields = array_merge($viewfields,
is_string($join)
? array($join)
: $join);
$this->templatemodule->setproperty(viewfields,
$thisviewfields);
}
private function setviewfield($field = array()) {
if (is_string($field))
$field = explode(,,
$field);
return array($this->templatemodule->getmodelname() => $field);
}
/**
* 检测字段中是否存在主键 如没有就补上
* @param type $field
* @return type
*/
private function checkpk($field = array()) {
$pk = $this->templatemodule->getpk();
if (is_array($field) && false === array_search($pk,
$field)) {
$field = array_merge(array($pk),
$field);
}
if (is_string($field)) {
$te = explode(,,
$field);
if (false === array_search($pk,
$te)) {
$field = array_merge(array($pk),
$te);
$field = implode(,,
$field);
}
}
return $field;
}
public function _getfield($field, $limit = 1000, $function = false) {
$list = $this->templatemodule->limit($limit)->getfield($field);
if (false !== $function && !empty($list)) {
if (method_exists($this->templatemodule,
$function))
return call_user_func(array($this->templatemodule, $function),
$list);
if (function_exists($function))
return call_user_func($function,
$list);
}
return $list;
}
}以下代码是在模板中套用.类式于其他cms调用模板数据一样. array('attr' => 'module,where,order,field,limit,pagesize,cache,group,join,id,key,mod,function', 'level' => 5),
'get' => array(attr => 'sql,cache,pagesize,limit,show,id,key,mod', 'level' => 3),
'template' => array(attr => file, close => 0)
);
function __construct() {
parent::__construct();
$this->tableprefix = c(db_prefix);
}
public function _list($attr, $content) {
static $_iterateparsecache = array();
$cacheiterateid = md5($attr . $content);
/**
* 标签变量解析
*/
$tag = $this->parsetag($this->parsexmlattr($attr,
'list'));
/**
* 数据获取
*/
$tag = array_merge($tag,
array(output => false));
$list = service(sql)->select($tag);
/**
* 生成模板php代码并返回
*/
$key = isset($tag['key']) ? $tag['key'] : key;
$id = isset($tag['id']) ? $tag['id'] : list;
$mod = isset($tag['mod']) ? intval($tag['mod']) : 2;
$this->tpl->set($cacheiterateid,
$list);
$parsestr = ;
$parsestr .= 'get(page' . $cacheiterateid . ');$_resule = $this->get(\'' . $cacheiterateid . '\');if ($_resule): $' . $key . '=0;';
$parsestr .= 'foreach($_resule as $key=>$' . $id . '):';
$parsestr .= '++$' . $key . ';$mod = ($' . $key . ' % ' . $mod . ' );?>';
$parsestr .= $content . '';
//模拟内存缓存
$_iterateparsecache[$cacheiterateid] = $parsestr;
return $parsestr;
}
/**
* 模板包含标签
* 格式
*
* @staticvar array $_admintemplateparsecache
* @param type $attr 属性字符串
* @param type $content 标签内容
* @return array
*/
public function _template($attr, $content) {
static $_admintemplateparsecache = array();
$cacheiterateid = md5($attr . $content);
if (isset($_admintemplateparsecache[$cacheiterateid])) {
return $_admintemplateparsecache[$cacheiterateid];
}
//分析admintemplate标签的标签定义
$tag = $this->parsexmlattr($attr,
'template');
$file = explode(/,
$tag['file']);
$counts = count($file);
if ($counts return false;
} else if ($counts $file_path = directory_separator . administrator . directory_separator . tpl . directory_separator . $tag['file'];
dump($file_path);
} else {
$file_path = directory_separator . $file[0] . directory_separator . tpl . directory_separator . $file[1] . directory_separator . $file[2];
}
//模板路径
$templatepath = app_path . c(app_group_path) . $file_path . c(tmpl_template_suffix);
//判断模板是否存在
if (!file_exists_case($templatepath)) {
return false;
}
//读取内容
$tmplcontent = file_get_contents($templatepath);
//解析模板内容
$parsestr = $this->tpl->parse($tmplcontent);
$_admintemplateparsecache[$cacheiterateid] = $parsestr;
return $_admintemplateparsecache[$cacheiterateid];
}
private function parsetag($tag = array()) {
if (!empty($tag)) {
foreach ($tag as $key => $value) {
$tempvalue = str_replace(array(@#_),
array($this->tableprefix),
$value);
if (preg_match(/(array|\\$)/i,
$tempvalue))
eval(\$tempvalue= . $tempvalue . ;);
$newtag[$key] = $tempvalue;
}
$tag = $newtag;
}
return $tag;
}
}
?>
ad:真正免费,域名+虚机+企业邮箱=0元
