首页  >  文章  >  php教程  >  发布一个比比较全面的取数据的类(结合TP)

发布一个比比较全面的取数据的类(结合TP)

WBOY
WBOY原创
2016-06-07 11:43:46909浏览

通过TP取数据的相关操作类 自己写的.和大家分享一个,觉得好的.支持一下
<?php <br /> <br> 类 SqlService {<br> <br>         protected $templateModule = NULL;<br>         公共 $_PageShow = NULL;<br>         protected $option = array();<br> <br>         公共函数选择($tag = array()){<br> <br>                 $this->_PageShow = NULL;<br>                 静态 $_iterateParseCache = array();<br>                 $cacheIterateId = md5(serialize($tag));<br>                 if ($_iterateParseCache[$cacheIterateId])<br>                         返回 $_iterateParseCache[$cacheIterateId];<br> <br> <br>                 $this->templateModule = $this->SetModule($tag['module']);<br> <br>                 if (isset($tag['join'])) {<br>                         $this->view($tag['join'],<br>                                 $this->SetviewField($tag['field']));<br>                         取消设置($tag['field']);<br>                 }<br> <br>                 $this->SetParose($tag);<br> <br>                 if (empty($tag['_type']) || $tag['_type'] == “_select”) {<br> <br>                         $list = $this->_select($tag['pagesize'],<br>                                 $tag['限制'],<br>                                 $tag['function']);<br>                 } 其他 {<br>                         $list=call_user_func(array($this,$tag['_type']),<br>                                 $tag['字段'],<br>                                 $tag['限制'],<br>                                 $tag['function']);<br>                 }<br> <br> <br>                 if (FALSE !== $tag['output']) {<br>                         返回服务(“ShowMe”)->ShowTable($list,<br>                                        $this->templateModule,<br>                                         ($tag['_listFields']<br>                                                 ? $tag['_listFields']<br>                                                 : “_listFields”));<br>                 }<br>                 返回 $list;<br>         }<br> <br>         /**<br>          * 确定模型<br>          * @param type $name<br>          * @return type<br>          */<br>         公共函数 SetModule($name = "") {<br>                 $moduleName = $name<br>                         ? ($name == “假”<br>                                 ? “”<br>                                 : $name)<br>                         : MODULE_NAME;<br>                 返回 D(ucwords(parse_name($moduleName,<br>                                         1)));<br>         }<br> <br>         /**<br>          * 对条件过滤 <br>          * 过滤规则 : 如本类有的方法,全部交由本类处理,如本类没有的该当 ,全部交由TP的MODEL 类处理 <br>          * join 和 table 参数,需要带表前辍 <br>          */<br>         私有函数 SetParose($tag = array()) {<br> <br>                 if (is_array($tag) &&!empty($tag)) {<br> <br>                         if (empty($tag['field']) && method_exists($this->templateModule,<br>                                         “getSqlFields”)) {<br> <br>                                $_listFields = isset($tag['_listFields'])<br>                                         ? $tag['_listFields']<br>                                         : “_listFields”;<br>                                 $tag['field'] = $this->templateModule->getSqlFields($this->templateModule,<br>                                         $_listFields);<br>                         }<br> <br>                         $tag['field'] = $this->CheckPK($tag['field']);<br>                         $insertPears = array('where', 'alias', 'field', 'order', 'limit', 'page', 'group', 'having', 'union', 'distinct', 'lock', '关系');<br>                         foreach($tag 作为$key=>$value){<br>                                 如果(in_array($key,<br>                                                 $insertPears)) {<br>                                         call_user_func(array($this->templateModule, $key),<br>                                                 $值);<br>                                         //   call_user_func(array(method_exists($this, $key) ? $this : $this->templateModule, $key), $value);<br>                                 }<br>                         }<br>                         if (isset($tag['cache'])) {<br>                                 $key = md5(serialize($tag));<br>                                 $this->templateModule->cache($key);<br>                         }<br>                         $this->option = $tag;<br>                 }<br>         }<br> <br>         public function getPageVar() {<br>                 $tpl = "<div class=&#039;row&#039;><div class=&#039;pagination&#039;>%s</div></div>”;<br>                 if (!is_null($this->_PageShow)) {<br>                         返回 sprintf($tpl,<br>                                 $this->_PageShow);<br>                 }<br>                 返回“”;<br>         }<br> <br>         私有函数_select($Pagesize=TRUE,$limit=20,$func=FALSE){<br> <br>                 if ($Pagesize) {<br>                         $_moduleListCount = $this->templateModule->count("*");<br>                         if ($_moduleListCount > 0) {<br>                                 $Page=SetPage($_moduleListCount,<br>                                         (int) $Pagesize);<br>                                 $this->SetParose($this->option);<br>                                 $list = $this->templateModule->limit($Page->firstRow . ',' . $Page->listRows)->select();<br>                                 //分页处理需优化<br> <br>                                 $this->_PageShow = $Page->show("Admin");<br>                                 //查询完成后.清空条件.邻近影响下次查询<br>                                 $this->选项 = NULL;<br>                         }<br>                 } 其他 {<br>                         $list = $this->templateModule->limit($Pagesize)->select();<br>                         $this->选项 = NULL;<br>                 }<br> <br> <br>                 //   dump($this->templateModule->getLastSql());<br> <br> <br>                 if (FALSE!==$func&&!empty($list)){<br>                        if (method_exists($this->templateModule,<br>                                         $func))<br>                                 return call_user_func(array($this->templateModule, $func),<br>                                         $列表);<br> <br> <br>                         if (function_exists($func))<br>                                 返回 call_user_func($func,<br>                                         $列表);<br>                 }<br> <br>                 返回 $list;<br>         }<br> <br>         公共函数视图($join = array(),$viewFields = array()){<br>                 if (!empty($viewFields)) {<br>                         $this->templateModule = new ViewModel();<br>                 }<br> <br>                 $thisViewFields = array_merge($viewFields,<br>                         is_string($join)<br>                                 ? 数组($join)<br>                                 : $加入);<br>                 $this->templateModule->setProperty("viewFields",<br>                         $thisViewFields);<br>         }<br> <br>         私有函数 SetviewField($Field = array()) {<br> <br>                 if (is_string($Field))<br>                         $Field = 爆炸(",",<br>                                 $字段);<br>                 return array($this->templateModule->getModelName() => $Field);<br>         }<br> <br>         /**<br>          * 检测字段中是否存在主键,如果没有就补上<br>          * @param 类型 $field<br>         * @return 类型<br>          */<br>         私有函数 CheckPK($field = array()) {<br>                 $pk = $this->templateModule->getPK();<br> <br> <br>                 if (is_array($field) && FALSE === array_search($pk,<br>                                 $字段)) {<br>                         $field = array_merge(array($pk),<br>                                 $字段);<br>                 }<br>                 if (is_string($field)) {<br>                         $te = 爆炸(",",<br>                                 $字段);<br>                         if (FALSE === array_search($pk,<br>                                         $te)) {<br>                                 $field = array_merge(array($pk),<br>                                         $te);<br>                                 $field = 内爆(",",<br>                                         $字段);<br>                         }<br>                 }<br> <br>                 返回 $field;<br>         }<br> <br>         公共函数_getField($field, $limit = 1000, $function = FALSE) {<br>                 $list = $this->templateModule->limit($limit)->getField($field);<br> <br>                 if (FALSE!==$function&&!empty($list)){<br>                         if (method_exists($this->templateModule,<br>                                         $函数))<br>                                return call_user_func(array($this->templateModule, $function),<br>                                         $list);<br> <br>                         if (function_exists($function))<br>                                 return call_user_func($function,<br>                                         $list);<br>                 }<br>                 return $list;<br>         }<br> <br> }以下代码是在模板中套用.类式于其他CMS调用模板数据一样.<?php<br /> <br /> class TagLibAdmin extends TagLib {<br /> <br />         protected $tags = array(<br />             'list' => array('attr' => 'module,where,order,field,limit,pagesize,cache,group,join,id,key,mod,function', 'level' => 5),<br>             'get' => array("attr" => 'sql,cache,pagesize,limit,show,id,key,mod', 'level' => 3),<br>             'template' => array("attr" => "file", "close" => 0)<br>         );<br> <br>         function __construct() {<br>                 parent::__construct();<br>                 $this->tablePrefix = C("DB_PREFIX");<br>         }<br> <br>         public function _list($attr, $content) {<br> <br>                 static $_iterateParseCache = array();<br>                 $cacheIterateId = md5($attr . $content);<br>                 /**<br>                  * 标签变量解析<br>                  */<br>                 $tag = $this->parseTag($this->parseXmlAttr($attr,<br>                                 'list'));<br>                 /**<br>                  * 数据获取<br>                  */<br>                 $tag = array_merge($tag,<br>                         array("output" => FALSE));<br>                $list = service("Sql")->Select($tag);<br> <br>                 /**<br>                  * 生成模板PHP代码并返回<br>                  */<br>                 $key = isset($tag['key'])? $tag['key'] : "key";<br>                 $id=isset($tag['id'])? $tag['id'] : “列表”;<br>                 $mod=isset($tag['mod'])? intval($tag['mod']) : 2;<br>                 $this->tpl->set($cacheIterateId,<br>                         $列表);<br> <br>                 $parsestr = "";<br>                 $parsestr .= '<?php  $page = $this->get("page' . $cacheIterateId . '");$_resule = $this->get('' . $cacheIterateId . '');如果($_resule):$'。 $键。 '=0;';<br> <br>                 $parsestr .= 'foreach($_resule as $key=>$' . $id . '):';<br>                 $parsestr .='$' . $键。 ';$mod = ($' . $key . ' % ' . $mod . ' );?>';<br>                 $parsestr .= $content . '<?php endforeach; 结束;?>';<br>                 // 模拟内存缓存<br>                 $_iterateParseCache[$cacheIterateId] = $parsestr;<br> <br>                 返回 $parsestr;<br>         }<br> <br>         /**<br>          * 模板包含标签 <br>          * 格式<br>          * <template file="APP/模块/模板"/><br>          * @staticvar array $_admintemplateParseCache<br>          * @param type $attr 属性字符串<br>          * @param type $content 标签内容<br>          * @return array <br>          */<br>         公共函数_template($attr, $content) {<br>                 静态 $_admintemplateParseCache = array();<br>                 $cacheIterateId = md5($attr.$content);<br>                 if (isset($_admintemplateParseCache[$cacheIterateId])) {<br>                         返回 $_admintemplateParseCache[$cacheIterateId];<br>                }<br>                 // 分析管理模板标签的标签定义<br>                 $tag = $this->parseXmlAttr($attr,<br>                         '模板');<br>                 $文件=爆炸(“/”,<br>                         $tag['文件']);<br>                 $counts=count($file);<br>                 if ($counts                         返回 false;<br>                 } else if ($counts                          $file_path=DIRECTORY_SEPARATOR。 “行政人员” 。 DIRECTORY_SEPARATOR。 “Tpl”。 DIRECTORY_SEPARATOR。 $tag['文件'];<br>                         转储($file_path);<br>                 } 其他 {<br>                         $file_path=DIRECTORY_SEPARATOR。 $文件[0]。 DIRECTORY_SEPARATOR。 “Tpl”。 DIRECTORY_SEPARATOR。 $文件[1]。 DIRECTORY_SEPARATOR。 $文件[2];<br>                 }<br>                 //模板路径<br>                 $TemplatePath = APP_PATH。 C(“APP_GROUP_PATH”)。 $文件路径。 C("TMPL_TEMPLATE_SUFFIX");<br>                 //判断模板是否存在<br>                 if (!file_exists_case($TemplatePath)) {<br>                         返回 false;<br>                 }<br>                 //读取内容<br>                 $tmplContent = file_get_contents($TemplatePath);<br>                 //解析模板内容<br>                 $parseStr = $this->tpl->parse($tmplContent);<br>                 $_admintemplateParseCache[$cacheIterateId] = $parseStr;<br>                 返回 $_admintemplateParseCache[$cacheIterateId];<br>         }<br> <br>         私有函数 parseTag($tag = array()) {<br>                 if (!empty($tag)) {<br>                        foreach($tag 作为$key=>$value){<br>                                 $tempValue = str_replace(array("@#_"),<br>                                         数组($this->tablePrefix),<br>                                         $值);<br>                                 if (preg_match("/(array|\$)/i",<br>                                                 $tempValue))<br>                                         eval("$tempValue=" . $tempValue . ";");<br>                                 $newTag[$key] = $tempValue;<br>                         }<br>                         $tag = $newTag;<br>                 }<br>                 返回 $tag;<br>         }<br> <br> }<br> <br> ?>

AD:真正免费,域名虚机企业邮箱=0元

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn