This article mainly introduced the CI framework commonly used function encapsulation, combined with the example form analysis of the CI framework commonly used database query, insert, delete, update and debug debugging and other operational skills, the need for friends can refer to the next
The examples in this paper describe the common function encapsulation of CI framework. Share to everyone for your reference, as follows:
/*** Package Query functions */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;} /*** Package single query function */public function get_row ($table = ", $where =array (), $fields = ' * ') {if (' = = $table) {return false ; }//Query and return related results $query = $this->db->select ($fields)->where ($where)->get ($table); $res = $query->row_array (); return $res;} /*** Package Update functions */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;} /*** Extended database function self-increment * using:* $table = ' Codeuser '; $where = array (' ID ' =>1); $data = Array (' usestate ' = ' usestate+1 ', ' Imgtype ' = ' imgtype-1 '); */public function Update_count ($table = ", $where =array (), $data =array ()) {//If the table name is empty or the data is empty, return false if (' = = $table | | empty ($DATA)) {Retu RN false; } foreach ($data as $key = + $val) {if (False!== Stripos ($val, ' + ') | | false!== stripos ($val, '-') {$th Is->db->set ($key, $val, FALSE); }else{$this->db->set ($key, $val); }} $res = $this->db->where ($where)->update ($table); return $res;} /*** encapsulated Insert Function */public functions insert_what ($table = ', $data = Array ()) {if (' = = $table | | true = = = Empty ($data)) { return false; }//Insert related record $query = $this->db->insert ($table, $data); return $query;} /*** Delete Record encapsulation function */public functions 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 Correlation function */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. "Under controller: \ r \ n"; $error. = $func. "Method debug information is as follows: \ r \ n"; $error. = $org _error;file_put_contents ("./error_log.txt", $error. " \ r \ n ", file_append); }}
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!