Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn to write your own operations related to data retrieval through TP. I would like to share it with you. I think it is good. Support
Class SqlService {
Protected $ templateModule = NULL;
Public $ _ PageShow = NULL;
Protected $ option = array ();
Public function Select ($ tag = array ()){
$ This-> _ 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;
}
/**
* Model Determination
* @ Param type $ name
* @ Return type
*/
Public function SetModule ($ name = ""){
$ ModuleName = $ name
? ($ Name = "false"
? ""
: $ Name)
: MODULE_NAME;
Return D (ucwords (parse_name ($ moduleName,
1 )));
}
/**
* Filter conditions
* Filter rules: all methods in this class are processed by this class. If this class does not have such a method, all the methods are processed by the tp model class.
* The join and table parameters must contain the values before the 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 ', 'stinct', '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 ();
// Page processing needs Optimization
$ This-> _ PageShow = $ Page-> show ("Admin ");
// After the query is complete, clear the condition to avoid affecting the next query.
$ 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 );
}
/**
* Check whether a primary key exists in the field. If no primary key exists, add it.
* @ 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;
}
}
The following code applies in the template. The class type is the same as that of other CMS call templates.
Class TagLibAdmin extends TagLib {
Protected $ tags = array (
'LIST' => 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 variable Parsing
*/
$ Tag = $ this-> parseTag ($ this-> parseXmlAttr ($ attr,
'LIST '));
/**
* Data Acquisition
*/
$ Tag = array_merge ($ tag,
Array ("output" => FALSE ));
$ List = service ("SQL")-> Select ($ tag );
/**
* Generate the PHP code of the template and return
*/
$ 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 .' ';
// Simulate Memory Cache
$ _ IterateParseCache [$ cacheIterateId] = $ parsestr;
Return $ parsestr;
}
/**
* The template contains tags.
* Format
*
* @ Staticvar array $ _ admintemplateParseCache
* @ Param type $ attr attribute string
* @ Param type $ content TAG content
* @ Return array
*/
Public function _ template ($ attr, $ content ){
Static $ _ admintemplateParseCache = array ();
$ CacheIterateId = md5 ($ attr. $ content );
If (isset ($ _ admintemplateParseCache [$ cacheIterateId]) {
Return $ _ admintemplateParseCache [$ cacheIterateId];
}
// Analyze the tag definition of the Admintemplate tag
$ Tag = $ this-> parseXmlAttr ($ attr,
'Template ');
$ File = explode ("/",
$ Tag ['file']);
$ Counts = count ($ file );
If ($ counts <2 ){
Return false;
} Else if ($ counts <3 ){
$ 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];
}
// Template path
$ TemplatePath = APP_PATH. C ("APP_GROUP_PATH"). $ file_path. C ("TMPL_TEMPLATE_SUFFIX ");
// Determine whether the template exists
If (! File_exists_case ($ TemplatePath )){
Return false;
}
// Read content
$ TmplContent = file_get_contents ($ TemplatePath );
// Parse the template content
$ 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: truly free, domain name + VM + enterprise mailbox = 0 RMB