网络编程 
首页 > 网络编程 > 浏览文章

CI框架常用函数封装实例

(编辑:jimmy 日期: 2024/5/9 浏览:3 次 )

本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《php优秀开发框架总结》、《ThinkPHP入门教程》、《ThinkPHP常用方法总结》、《Zend FrameWork框架入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

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

上一篇:CI框架中类的自动加载问题分析
下一篇:CI框架数据库查询缓存优化的方法
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网