Examples of common CI framework function encapsulation and ci framework Encapsulation
This example describes common function encapsulation in the CI framework. We will share this with you for your reference. The details are as follows:
/*** Encapsulate the query function */public function get_what ($ table = '', $ where = array (), $ fields = '*') {if (''= $ table) {return false;} // query and return related results $ query = $ this-> db-> select ($ fields) -> where ($ where)-> get ($ table); $ res = $ query-> result_array (); return $ res ;} /*** encapsulate a single query function */public function get_row ($ table = '', $ where = array (), $ fields = '*') {if (''= $ table) {return false;} // query and return related results $ query = $ this-> db-> select ($ field S)-> where ($ where)-> get ($ table); $ res = $ query-> row_array (); return $ res ;} /*** encapsulate the UPDATE function */public function update_what ($ table = '', $ where = array (), $ data = array ()) {if (''= $ table | true = empty ($ where) | true = empty ($ data) {return false ;} // update the corresponding field $ query = $ this-> db-> update ($ table, $ data, $ where); return $ query ;} /*** auto-increment and auto-increment of extended database functions * using: * $ table = 'codeuser'; $ where = array ('id' => 1); $ data = ar Ray ('usestate' => 'usestate + 1', 'imgtype' => 'imgtype-1'); */public function update_count ($ table = '', $ where = array (), $ data = array ()) {// if the table name is null or the data is null, false is returned directly. if (''= $ table | empty ($ data) {return false ;} foreach ($ data as $ key => $ val) {if (false! = Stripos ($ val, '+') | false! = Stripos ($ val, '-') {$ this-> db-> set ($ key, $ val, FALSE );} else {$ this-> db-> set ($ key, $ val) ;}$ res = $ this-> db-> where ($ where) -> update ($ table); return $ res;}/*** encapsulate the insert function */public function insert_what ($ table = '', $ data = array ()) {if (''= $ table | true = empty ($ data) {return false ;} // insert related records $ query = $ this-> db-> insert ($ table, $ data); return $ query ;} /*** Delete record encapsulation function */public function delete_what ($ table = '', $ where = array () {if (true = empty ($ where) | ''= $ table) {return false;} // delete related table records $ query = $ this-> db-> delete ($ table, $ where ); return $ query;}/*** debug related functions */public function debug_what ($ org_error = '') {$ con = $ this-> router-> fetch_class (); $ func = $ this-> router-> fetch_method (); if ($ org_error) {$ error. = date ("Y-m-d H: I: s", time ()). "\ r \ n"; $ error. = _ FILE __. "\ r \ n"; $ error. = $ con. "\ r \ n" under the Controller; $ error. = $ func. "method debugging information: \ r \ n"; $ error. = $ org_error; file_put_contents (". /error_log.txt ", $ error. "\ r \ n", FILE_APPEND );}}