SoFunction
Updated on 2025-04-07

Commonly used function encapsulation examples of CI frameworks

This article describes the commonly used function encapsulation of CI frameworks. Share it for your reference, as follows:

/**
 * Encapsulate query function
 */
public function get_what($table='',$where=array(),$fields = ' * '){
    if( '' == $table ){
      return false;
    }
    //Query and return relevant 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 relevant results    $query = $this->db->select($fields)->where($where)->get($table);
    $res = $query->row_array();
    return $res;
}
/**
 * Encapsulation update function
 */
public function update_what($table='', $where=array(), $data = array()){
    if('' == $table || true === empty($where) || true === empty($data)){
      return false;
    }
    //Update the corresponding fields    $query = $this->db->update($table,$data,$where);
    return $query;
}
/**
 * Self-increase and self-decrease of extended database functions
 * 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, then return false 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;
}
/**
 * Encapsulation insertion 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."Under the controller:\r\n";
      $error .= $func."The method debugging information is as follows:\r\n";
      $error .= $org_error;file_put_contents("./error_log.txt",$error."\r\n",FILE_APPEND);
    }
}

For more information about CodeIgniter, readers who are interested in view the topic of this site:Codeigniter Introductory Tutorial》、《Advanced tutorial on CI (CodeIgniter) framework》、《Summary of excellent development framework for php》、《ThinkPHP Introduction Tutorial》、《Summary of common methods of ThinkPHP》、《Zend FrameWork framework tutorial》、《PHP object-oriented programming tutorial》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php

I hope that this article will be helpful to everyone's PHP programming based on the CodeIgniter framework.