Skip to content

Instantly share code, notes, and snippets.

@eddysapata
Created July 12, 2018 11:44
Show Gist options
  • Save eddysapata/dae0ad9e43a64bbcd40df02077a8d817 to your computer and use it in GitHub Desktop.
Save eddysapata/dae0ad9e43a64bbcd40df02077a8d817 to your computer and use it in GitHub Desktop.
<?php
(defined('BASEPATH')) OR exit('No direct script access allowed');
/**
* CMS Canvas
*
* @author Hemant Chouhan
* @copyright Copyright (c) 2012
* @license MIT License
* @link http://BonFire.com
*/
class Common_model extends CI_Model
{
function get_data($table = '', $field = '', $where = '', $num = FALSE, $return = 'result')
{
if (isset($table) && $table != '') {
if ($where != '')
$this->db->where($where);
if ($field != '')
$this->db->select($field);
$query = $this->db->get($table);
if ($query) {
if ($num == FALSE)
return $query->$return();
else
return $query->num_rows();
} else {
return False;
}
}
}
function delete($table = '', $where = '')
{
if (!empty($table)) {
$this->db->where($where);
$this->db->delete($table);
if ($this->db->affected_rows() > 0)
return true;
else
return false;
}
}
function update_label($data, $id)
{
$this->db->where('attribute_id', $id);
$this->db->delete('attribute_label');
$query = $this->db->insert('attribute_label', $data);
}
function save_data($table = '', $data = '')
{
if (isset($table) && $table != '' && isset($data) && $data != '') {
$this->db->insert($table, $data);
if ($this->db->affected_rows() == '1')
return $this->db->insert_id();
else
return false;
}
}
function edit_data($table = '', $data = '', $where = '')
{
if (isset($table) && $table != '' && isset($data) && $data != '' && isset($where) && $where != '') {
$this->db->where($where);
$this->db->update($table, $data);
if ($this->db->affected_rows() == '1')
return true;
else
return false;
}
}
function delete_data($table = '', $where = '')
{
if (isset($table) && $table != '' && isset($where) && $where != '') {
$this->db->where($where);
$this->db->delete($table);
if ($this->db->affected_rows() > 0)
return true;
else
return false;
}
}
function delete_multiple($table1, $table2, $column1, $column2, $id)
{
$this->db->trans_begin();
$this->db->where_in($column1, $id);
$this->db->delete($table1);
$this->db->where_in($column2, $id);
$this->db->delete($table2);
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE) {
return false;
} else {
return true;
}
}
function delete_multiple_single_table($table1, $column1, $id)
{
if (!empty($id)) {
$this->db->trans_begin();
$this->db->where_in($column1, $id);
$this->db->delete($table1);
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE) {
return false;
} else {
return true;
}
} else {
return false;
}
}
function batch_insert_data($insert_batch = array())
{
if (isset($insert_batch['table']) && !empty($insert_batch['table']) && isset($insert_batch['data']) && !empty($insert_batch['data'])) {
$this->db->insert_batch($insert_batch['table'], $insert_batch['data']);
if ($this->db->affected_rows() > 0) {
return true;
} else {
return false;
}
} else {
return false;
}
}
function batch_update_data($update_batch = array())
{
if (isset($update_batch['table']) && !empty($update_batch['table']) && isset($update_batch['data']) && !empty($update_batch['data']) && isset($update_batch['where']) && !empty($update_batch['where'])) {
$this->db->update_batch($update_batch['table'], $update_batch['data'], $update_batch['where']);
if ($this->db->affected_rows() > 0) {
return true;
} else {
return false;
}
} else {
return false;
}
}
function where($where)
{
if ($where != '' && $where != NULL) {
$this->db->where($where);
}
return $this;
}
function returnWhereIn($column, $id)
{
if (!empty($column) && !empty($id)) {
$this->db->where_in($column, $id);
}
return $this;
}
function returnJoin($table1, $table2, $id1, $id2, $type = FALSE)
{
$this->db->join($table2, $table1 . '.' . $id1 . '=' . $table2 . '.' . $id2, $type);
return $this;
}
function returnJoinLeft($table1, $table2, $id1, $id2, $type = FALSE)
{
$this->db->join($table2, $table1 . '.' . $id1 . '=' . $table2 . '.' . $id2, 'left');
return $this;
}
function returnLike($column_name)
{
if (!empty($column_name)) {
$this->db->like($column_name);
}
return $this;
}
function returnOrLike_old($column_name)
{
if (!empty($column_name)) {
$this->db->or_like($column_name);
}
return $this;
}
// Custom or like by hemant
function returnOrLike($column_name)
{
if (!empty($column_name)) {
// loop through all left array elements
$loop = 0;
foreach ($column_name as $key => $search_string_multiple) {
$col_arr = explode('.', $key);
//Adding table prefix
if (count($col_arr) == 2) {
$table = $this->db->dbprefix($col_arr[0]);
$key = $table . '.' . $col_arr[1];
}
$key = str_replace('.', "`.`", $key);
if ($loop == 0) {
$sub = '(`' . $key . '` LIKE \'%' . $search_string_multiple . '%\' ';
} else {
$sub .= ' ESCAPE "!" OR `' . $key . '` LIKE \'%' . $search_string_multiple . '%\' ';
}
$loop++;
}
$sub .= ')';
$this->db->where($sub, NULL, FALSE);
}
return $this;
}
function returnAndLike($column_name)
{
if (!empty($column_name)) {
// loop through all left array elements
$loop = 0;
foreach ($column_name as $key => $search_string_multiple) {
$col_arr = explode('.', $key);
//Adding table prefix
if (count($col_arr) == 2) {
$table = $this->db->dbprefix($col_arr[0]);
$key = $table . '.' . $col_arr[1];
}
$key = str_replace('.', "`.`", $key);
if ($loop == 0) {
$sub = '(`' . $key . '` LIKE \'%' . $search_string_multiple . '%\' ';
} else {
$sub .= ' ESCAPE "!" Or `' . $key . '` LIKE \'%' . $search_string_multiple . '%\' ';
}
$loop++;
}
$sub .= ')';
//echo br(3);
//echo $sub;
$this->db->where($sub, NULL, FALSE);
}
return $this;
}
function returnOrLike_Diff($column_name)
{
if (!empty($column_name)) {
// loop through all left array elements
$loop = 0;
$string_arr = explode(',', current($column_name));
foreach ($column_name as $key => $search_string_multiple) {
$col_arr = explode('.', $key);
if (!isset($string_arr[$loop]))
continue;
$search_string_multiple = trim($string_arr[$loop]);
//Adding table prefix
if (count($col_arr) == 2) {
$table = $this->db->dbprefix($col_arr[0]);
$key = $table . '.' . $col_arr[1];
}
$key = str_replace('.', "`.`", $key);
if ($loop == 0) {
$sub = '(`' . $key . '` LIKE \'%' . $search_string_multiple . '%\' ';
} else {
$sub .= ' ESCAPE "!" OR `' . $key . '` LIKE \'%' . $search_string_multiple . '%\' ';
}
$loop++;
}
$sub .= ')';
$this->db->where($sub, NULL, FALSE);
}
return $this;
}
function updateIncrementRowVal($table, $column, $where)
{
$this->db->set($column, "{$column}+1", FALSE);
$this->db->where($where);
$this->db->update($table);
return $this;
}
function returnGroupby($column)
{
$this->db->group_by($column);
return $this;
}
function returnOrderby($column, $order = 'asc')
{
if (is_array($column)) {
foreach ($column as $value) {
$this->db->order_by($value, $order);
}
} else {
$this->db->order_by($column, $order);
}
return $this;
}
function returnLimitOffset($offset, $limit)
{
$this->db->limit($limit, $offset);
return $this;
}
public function returnOrWhere($column_name)
{
if (!empty($column_name)) {
// loop through all left array elements
$loop = 0;
foreach ($column_name as $key => $search_string_multiple) {
$col_arr = explode('.', $key);
//Adding table prefix
if (count($col_arr) == 2) {
$table = $this->db->dbprefix($col_arr[0]);
$key = $table . '.' . $col_arr[1];
}
$key = str_replace('.', "`.`", $key);
if ($loop == 0) {
$sub = '(' . $key . ' ' . $search_string_multiple;
} else {
$sub .= ' OR ' . $key . ' ' . $search_string_multiple;
}
$loop++;
}
$sub .= ')';
$this->db->where($sub, NULL, FALSE);
}
return $this;
}
public function returnWhereBtn($where = '')
{
if (!empty($where)) {
$this->db->where($where);
}
return $this;
}
function selectmin($column = '')
{
if (isset($column) && $column != '') {
$this->db->select_min($column);
return $this;
} else {
return False;
}
}
function selectmax($column = '')
{
if (isset($column) && $column != '') {
$this->db->select_max($column);
return $this;
} else {
return False;
}
}
function like($column, $query)
{
if (!empty($column) && !empty($query)) {
$this->db->like($column, $query);
}
return $this;
}
function or_like($column, $query)
{
if (!empty($column) && !empty($query)) {
$this->db->or_like($column, $query);
}
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment