首頁 >php教程 >php手册 >发布一个比比较全面的取数据的类(结合TP)

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

WBOY
WBOY原創
2016-06-07 11:43:46931瀏覽

通过TP取数据的相关操作类 自己写的.和大家分享一个,觉得好的.支持一下
<?php <br /> <br> class SqlService {<br> <br>         protected $templateModule = NULL;<br>         public $_PageShow = NULL;<br>         protected $option = array();<br> <br>         public function Select($tag = array()) {<br> <br>                 $this->_PageShow = NULL;<br>                 static $_iterateParseCache = array();<br>                 $cacheIterateId = md5(serialize($tag));<br>                 if ($_iterateParseCache[$cacheIterateId])<br>                         return $_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>                         unset($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['limit'],<br>                                 $tag['function']);<br>                 } else {<br>                         $list = call_user_func(array($this, $tag['_type']),<br>                                 $tag['field'],<br>                                 $tag['limit'],<br>                                 $tag['function']);<br>                 }<br> <br> <br>                 if (FALSE !== $tag['output']) {<br>                         return service("ShowMe")->ShowTable($list,<br>                                         $this->templateModule,<br>                                         ($tag['_listFields']<br>                                                 ? $tag['_listFields']<br>                                                 : "_listFields"));<br>                 }<br>                 return $list;<br>         }<br> <br>         /**<br>          * 确定模型<br>          * @param type $name<br>          * @return type<br>          */<br>         public function SetModule($name = "") {<br>                 $moduleName = $name<br>                         ? ($name == "false"<br>                                 ? ""<br>                                 : $name)<br>                         : MODULE_NAME;<br>                 return D(ucwords(parse_name($moduleName,<br>                                         1)));<br>         }<br> <br>         /**<br>          * 对条件过滤 <br>          * 过滤规则 : 如本类有的方法,全部交由本类处理,如本类没有的该当 ,全部交由TP的MODEL 类处理 <br>          * join 和 table 参数,需要带表前辍 <br>          */<br>         private function 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', 'relation');<br>                         foreach ($tag as $key => $value) {<br>                                 if (in_array($key,<br>                                                 $insertPears)) {<br>                                         call_user_func(array($this->templateModule, $key),<br>                                                 $value);<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><div>%s</div></div>";<br>                 if (!is_null($this->_PageShow)) {<br>                         return sprintf($tpl,<br>                                 $this->_PageShow);<br>                 }<br>                 return "";<br>         }<br> <br>         private function _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->option = NULL;<br>                         }<br>                 } else {<br>                         $list = $this->templateModule->limit($Pagesize)->select();<br>                         $this->option = 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>                                         $list);<br> <br> <br>                         if (function_exists($func))<br>                                 return call_user_func($func,<br>                                         $list);<br>                 }<br> <br>                 return $list;<br>         }<br> <br>         public function view($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>                                 ? array($join)<br>                                 : $join);<br>                 $this->templateModule->setProperty("viewFields",<br>                         $thisViewFields);<br>         }<br> <br>         private function SetviewField($Field = array()) {<br> <br>                 if (is_string($Field))<br>                         $Field = explode(",",<br>                                 $Field);<br>                 return array($this->templateModule->getModelName() => $Field);<br>         }<br> <br>         /**<br>          * 检测字段中是否存在主键 如没有就补上<br>          * @param type $field<br>          * @return type<br>          */<br>         private function CheckPK($field = array()) {<br>                 $pk = $this->templateModule->getPK();<br> <br> <br>                 if (is_array($field) && FALSE === array_search($pk,<br>                                 $field)) {<br>                         $field = array_merge(array($pk),<br>                                 $field);<br>                 }<br>                 if (is_string($field)) {<br>                         $te = explode(",",<br>                                 $field);<br>                         if (FALSE === array_search($pk,<br>                                         $te)) {<br>                                 $field = array_merge(array($pk),<br>                                         $te);<br>                                 $field = implode(",",<br>                                         $field);<br>                         }<br>                 }<br> <br>                 return $field;<br>         }<br> <br>         public function _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>                                         $function))<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'] : "list";<br>                 $mod = isset($tag['mod']) ? intval($tag['mod']) : 2;<br>                 $this->tpl->set($cacheIterateId,<br>                         $list);<br> <br>                 $parsestr = "";<br>                 $parsestr .= '<?php $page = $this->get("page' . $cacheIterateId . '");$_resule = $this->get(\'' . $cacheIterateId . '\');if ($_resule): $' . $key . '=0;';<br> <br>                 $parsestr .= 'foreach($_resule as $key=>$' . $id . '):';<br>                 $parsestr .= '++$' . $key . ';$mod = ($' . $key . ' % ' . $mod . ' );?>';<br>                 $parsestr .= $content . '<?php endforeach; endif;?>';<br>                 //模拟内存缓存<br>                 $_iterateParseCache[$cacheIterateId] = $parsestr;<br> <br>                 return $parsestr;<br>         }<br> <br>         /**<br>          * 模板包含标签 <br>          * 格式<br>          * <template></template><br>          * @staticvar array $_admintemplateParseCache<br>          * @param type $attr 属性字符串<br>          * @param type $content 标签内容<br>          * @return array <br>          */<br>         public function _template($attr, $content) {<br>                 static $_admintemplateParseCache = array();<br>                 $cacheIterateId = md5($attr . $content);<br>                 if (isset($_admintemplateParseCache[$cacheIterateId])) {<br>                         return $_admintemplateParseCache[$cacheIterateId];<br>                 }<br>                 //分析Admintemplate标签的标签定义<br>                 $tag = $this->parseXmlAttr($attr,<br>                         'template');<br>                 $file = explode("/",<br>                         $tag['file']);<br>                 $counts = count($file);<br>                 if ($counts                          return false;<br>                 } else if ($counts                          $file_path = DIRECTORY_SEPARATOR . "Administrator" . DIRECTORY_SEPARATOR . "Tpl" . DIRECTORY_SEPARATOR . $tag['file'];<br>                         dump($file_path);<br>                 } else {<br>                         $file_path = DIRECTORY_SEPARATOR . $file[0] . DIRECTORY_SEPARATOR . "Tpl" . DIRECTORY_SEPARATOR . $file[1] . DIRECTORY_SEPARATOR . $file[2];<br>                 }<br>                 //模板路径<br>                 $TemplatePath = APP_PATH . C("APP_GROUP_PATH") . $file_path . C("TMPL_TEMPLATE_SUFFIX");<br>                 //判断模板是否存在<br>                 if (!file_exists_case($TemplatePath)) {<br>                         return false;<br>                 }<br>                 //读取内容<br>                 $tmplContent = file_get_contents($TemplatePath);<br>                 //解析模板内容<br>                 $parseStr = $this->tpl->parse($tmplContent);<br>                 $_admintemplateParseCache[$cacheIterateId] = $parseStr;<br>                 return $_admintemplateParseCache[$cacheIterateId];<br>         }<br> <br>         private function parseTag($tag = array()) {<br>                 if (!empty($tag)) {<br>                         foreach ($tag as $key => $value) {<br>                                 $tempValue = str_replace(array("@#_"),<br>                                         array($this->tablePrefix),<br>                                         $value);<br>                                 if (preg_match("/(array|\\$)/i",<br>                                                 $tempValue))<br>                                         eval("\$tempValue=" . $tempValue . ";");<br>                                 $newTag[$key] = $tempValue;<br>                         }<br>                         $tag = $newTag;<br>                 }<br>                 return $tag;<br>         }<br> <br> }<br> <br> ?>

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

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn