Tuesday, 2 October 2018

Common Model -Reduce script code

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Common_Model extends CI_Model {

    #function to get global setttings from the database   
public function getGlobalSettings($lang_id = '') {
        $global = array();
        $this->db->select('mst_global.*,trans_global.*');
        $this->db->from('mst_global_settings as mst_global');
        $this->db->join('trans_global_settings as trans_global', 'mst_global.global_name_id = trans_global.global_name_id', 'left');
        if ($lang_id != '') {
            $this->db->where("trans_global.lang_id", $lang_id); /* for lnag id passed ie. english */
        } else {
            $this->db->where("trans_global.lang_id", 17); /* for default language ie. english */
        }
        $result = $this->db->get();
        foreach ($result->result_array() as $row) {
            $global[$row['name']] = $row['value'];
        }
        return $global;
    }
#common function to get records from the database table
public function getRecords($table, $fields = '', $condition = '', $order_by = '', $limit = '', $debug = 0) {
        $str_sql = '';
        if(is_array($fields)) { 
            $str_sql =implode(",", $fields);
        } elseif($fields!="") { 
            $str_sql = $fields;
        } else{
            $str_sql = '*';
        } $this->db->select($str_sql);

        if (is_array($condition)) {  #$condition passed as array
            if (count($condition) > 0) {
                foreach ($condition as $field_name => $field_value) {
                    if ($field_name != '' && $field_value != '') {
                        $this->db->where($field_name, $field_value);
                    }
                }
            }
        } else if ($condition != "") { #$condition passed as string
            $this->db->where($condition);
        }

        if ($limit != "")
            $this->db->limit($limit);#limit is not blank

        if (is_array($order_by)) {
            $this->db->order_by($order_by[0], $order_by[1]);  #$order_by is not blank
        } else if ($order_by != "") {
            $this->db->order_by($order_by);  #$order_by is not blank
        }


        $this->db->from($table);  #getting record from table name passed
        $query = $this->db->get();
//$total = count($query);
//echo $total;die;
        if ($debug) {
            die($this->db->last_query());
        }
/// echo $this->db->last_query();//die;
        return $query->result_array();
    }

public function getRecordsCount($table, $fields = '', $condition = '', $order_by = '', $limit = '', $debug = 0) {
        $str_sql = '';
        if(is_array($fields)) { 
            $str_sql =implode(",", $fields);
        } elseif($fields!="") { 
            $str_sql = $fields;
        } else{
            $str_sql = '*';
        } $this->db->select($str_sql);

        if (is_array($condition)) {  #$condition passed as array
            if (count($condition) > 0) {
                foreach ($condition as $field_name => $field_value) {
                    if ($field_name != '' && $field_value != '') {
                        $this->db->where($field_name, $field_value);
                    }
                }
            }
        } else if ($condition != "") { #$condition passed as string
            $this->db->where($condition);
        }

        if ($limit != "")
            $this->db->limit($limit);#limit is not blank

        if (is_array($order_by)) {
            $this->db->order_by($order_by[0], $order_by[1]);  #$order_by is not blank
        } else if ($order_by != "") {
            $this->db->order_by($order_by);  #$order_by is not blank
        }


        $this->db->from($table);  #getting record from table name passed
        $query = $this->db->get();
//$total = count($query);
//echo $total;die;
        if ($debug) {
            die($this->db->last_query());
        }
/// echo $this->db->last_query();//die;
        return $query->num_rows();
    }



public function numrows($table, $fields = '', $condition = '', $order_by = '', $limit = '', $debug = 0){

$this->db->select('*');
        $this->db->from($table);
        $this->db->where($condition);

        $result = $this->db->get();
     
       return $num = $result->num_rows();

}



    #function to insert record into the database

    public function insertRow($insert_data, $table_name) {
        $this->db->insert($table_name, $insert_data);
    return $this->db->insert_id();
    }

    #function to update record in the database

    public function updateRow($table_name, $update_data, $condition) {
        if (is_array($condition)) {
            if (count($condition) > 0) {
                foreach ($condition as $field_name => $field_value) {
                    if ($field_name != '' && $field_value != '') {
                        $this->db->where($field_name, $field_value);
                    }
                }
            }
        } else if ($condition != "") {
            $this->db->where($condition);
        }
//echo $this->db->last_query();die;
        return $this->db->update($table_name, $update_data);

    }

//start code if record already exist then update or delete
function update_or_insert($table , $data , $cmn_col){
$this->db->where($cmn_col);
$q = $this->db->get($table);

if ( $q->num_rows() > 0 ) {
$this->db->where($cmn_col);
return $this->db->update($table,$data);
} else {
$this->db->set($cmn_col);
return $this->db->insert($table,$data);
}
//echo $this->db->last_query();
}

//start code if record already exist then update or delete
function update_or_insert_new($table , $data , $cmn_col){
$this->db->where($cmn_col);
$q = $this->db->get($table);

if ( $q->num_rows() > 0 ) {

return 1;
} else {

return 2;
}
//echo $this->db->last_query();
}

//start code if record already exist then update or delete
function insert_or_update($table , $data){
$this->db->where('mast_id',$data['mast_id']);
$q = $this->db->get($table);

if ( $q->num_rows() > 0 ) {
$this->db->where('mast_id',$data['mast_id']);
$this->db->update($table,$data);
} else {
$this->db->set('mast_id', $data['mast_id']);
$this->db->insert($table,$data);
}
}


public function updateRecord($table_name, $update_data, $condition) {
       $this->db->select('*');
$this->db->from('mst_users');
$this->db->where($condition);

         $this->db->update($table_name, $update_data);
//echo $this->db->last_query();die;


    }

    #common function to delete rows from the table

    public function deleteRows($arr_delete_array, $table_name, $field_name) {
        if (count($arr_delete_array) > 0) {
            foreach ($arr_delete_array as $id) {
                $this->db->where($field_name, $id);
                $query = $this->db->delete($table_name);
            }
        }
//echo $this->db->last_query();die;
    }

//start code to delete single row
function row_delete_comman($table_name,$data)
{
$this->db->where($data);
return $this->db->delete($table_name);
}

//start code to delete record from student wishlist
public function deleteWishRows($m_id,  $u_id , $table_name) {

                $this->db->where('mast_id', $m_id);
$this->db->where('uni_id', $u_id);
                $query = $this->db->delete($table_name);
         
        //echo $this->db->last_query();die;
    }
}

No comments:

Post a Comment