CI框架常用函数封装实例

把每个睡醒后的早晨当成一件礼物,把每个开心后的微笑当成一个习惯。朋友,短信祝早上好,愿你微笑今天,快乐永远!有勇气并不表示恐惧不存在,而是敢面对恐惧克服恐惧。

本文实例讲述了CI框架常用函数封装。分享给大家供大家参考,具体如下:

/**
* 封装查询函数
*/
public function get_what($table='',$where=array(),$fields = ' * '){
    if( '' == $table ){
      return false;
    }
    //查询并返回相关结果
    $query = $this->db->select($fields)->where($where)->get($table);
    $res = $query->result_array();
    return $res;
}
/**
* 封装单条查询函数
*/
public function get_row($table='',$where=array(),$fields = ' * '){
    if( '' == $table ){
      return false;
    }
    //查询并返回相关结果
    $query = $this->db->select($fields)->where($where)->get($table);
    $res = $query->row_array();
    return $res;
}
/**
* 封装更新函数
*/
public function update_what($table='', $where=array(), $data = array()){
    if('' == $table || true === empty($where) || true === empty($data)){
      return false;
    }
    //更新相应的字段
    $query = $this->db->update($table,$data,$where);
    return $query;
}
/**
* 扩展数据库函数之自增 自减
* using:
* $table = 'codeuser';
$where = array('id'=>1);
$data = array('usestate'=>'usestate+1','imgtype' => 'imgtype-1');
*/
public function update_count($table = '', $where=array(), $data=array()){
     //如果表名为空 或者数据为空则直接 返回false
     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;
}
/**
* 封装插入函数
*/
public function insert_what($table = '', $data = array()){
    if('' == $table || true === empty($data)){
      return false;
    }
    //插入 相关记录
    $query = $this->db->insert($table, $data);
    return $query;
}
/**
* 删除记录封装函数
*/
public function delete_what($table = '', $where=array()){
    if(true === empty($where) || '' == $table){
      return false;
    }
    //删除相关表记录
    $query = $this->db->delete($table,$where);
    return $query;
}
/**
* debug 相关函数
*/
 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";
      $error .= $func." 方法调试信息如下:\r\n";
      $error .= $org_error;file_put_contents("./error_log.txt",$error."\r\n",FILE_APPEND);
    }
}

希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。

到此这篇关于CI框架常用函数封装实例就介绍到这了。每一个人,活在世上,必定要经历很多,承受很多。经历,不是沧桑,是沉淀;承受,不是苦难,是历练。在沧桑中领悟,在历练中成熟,迎一袖风,绽一树香,静品淡藏。趟过岁月冷暖,感知生命厚重,苦过,甜过,爱过,恨过,哭过,笑过,才是人生。更多相关CI框架常用函数封装实例内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

您可能有感兴趣的文章
Laravel5.4框架使用socialite实现github登录的方法

PHP htmlspecialchars()函数用法与实例讲解

CI框架网页缓存简单用法分析

PHP htmlspecialchars() 函数实例代码及用法大全

CI框架安全过滤函数示例