<?php
/**
 * @Cscms 3.5 open source management system
 * @copyright 2009-2013 chshcms.com. All rights reserved.
 * @Author:Cheng Jie
 * @Dtime:2013-04-27
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

 class CsdjDB extends CI_Model
 {
    function __construct ()
	{
	   parent:: __construct ();
	   $this->load->database();
	}

    function get_all ($sql)  //多条件查询
	 {
	   $query=$this->db->query($sql);
	   return $query->result();
	 }

    function get_allnums ($sql='')  //多条件查询总数量
	 {
            if(!empty($sql)){
	           $query=$this->db->query($sql);
                   $nums=$query->num_rows();
            }else{
                   $nums=0;
            }
	   return $nums;
	 }

    function get_nums ($table,$czd='',$arr='')  //查询总数量
	 {
          if(!empty($czd) && !empty($arr)){
	      $this->db->where($czd,$arr);
          }
	   $query=$this->db->get($table);
           $nums=$query->num_rows();
	   return $nums;
	 }

    function get_djs ($table,$fzd='*')  //查询全部
	 {
	   $this->db->select($fzd);
	   $query=$this->db->get($table);
	   return $query->result();
	 }

    function get_djs_num ($table,$fzd='*',$num=12,$order='cs_id',$desc='desc',$czd='',$arr='')  //查询数量
	 {
          if($czd){
	     $this->db->where($czd,$arr);
          }
	   $this->db->select($fzd);
           $this->db->order_by($order,$desc);
	   $query=$this->db->get($table,$num);
	   return $query->result();
	 }

    function get_select ($table,$czd='',$fzd='*',$arr='')  //按条件查询
	 {
          if($czd && $arr){
	      $this->db->where($czd,$arr);
          }
	   $this->db->select($fzd);
	   $query=$this->db->get($table);
	   return $query->result();
	 }

    function get_soall ($table,$czd='',$fzd='*',$arr='')  //按条件LIKE搜索查询
	 {
          if($czd && $arr){
	      $this->db->like($fzd,$arr);
          }
	   $this->db->select($fzd);
	   $query=$this->db->get($table);
	   return $query->result();
	 }

    function get_pages($table, $czd='', $arr='', $num = 10, $offset = 0)
	{
           if($czd && $arr){
	        $this->db->where($czd,$arr);
           }
		$this->db->from($table);
		$this->db->limit($num, $offset);
		$this->db->order_by('id desc');
		
	 	$query = $this->db->get();
	 	
	 	return $query->result();
	}

    function get_insert ($table,$arr)  //增加
	 {
           if($arr){
	        $this->db->insert($table,$arr);
		return $this->db->insert_id();
           }else{
		return false;
           }
	 }


    function get_update ($table,$id,$arr,$zd='cs_id')  //修改
    {
          if(!empty($id)){
	      $this->db->where($zd,$id);
	      if($this->db->update($table,$arr)){
	        return true;
              }else{
		return false;
              }
          }else{
		return false;
          }
    }


    function get_del ($table,$ids,$zd='cs_id')  //删除
	 {
	   if(is_array($ids)){
		$this->db->where_in($zd,$ids);
	   }else{
		$this->db->where($zd,$ids);
	   }
	   if($this->db->delete($table)){
	        return true;
           }else{
		return false;
           }
	 }

    function get_table ($sql)  //创建表
	 {
             //创建数据库
             $lnk=@mysql_connect(CS_Sqlserver,CS_Sqluid,CS_Sqlpwd);
             if(!$lnk){
                  $this->CsdjAdmin->Admin_Url('<font color=red>抱歉,数据库连接失败！</font>','javascript:history.back();');
                  exit();
             }
	     @mysql_select_db(CS_Sqlname,$lnk);
	     @mysql_query("SET NAMES ".CS_Sqlcharset."", $lnk);
             @mysql_query($sql, $lnk);
	 }
 }

