Model Query

<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Test_model extends CI_Model
{
    public $todays_date;
   
    public function __construct()
    {
        parent::__construct();
       
        date_default_timezone_set('Asia/Dhaka');
        $this->todays_date = date("Y-m-d")." 00:00:00";
    }

/* Insert Query */

    public function create_data($data)
    {
        return $this->db->insert('tbl_form', $data);
    }

    public function create_data($data)
    {
        return $this->db->insert_batch('tbl_form', $data);
    }

    public function create_data($data)
    {
        $query = $this->db->insert('tbl_form', $data);
        if ($query == true) {
            return $this->db->insert_id();
        } else {
            return false;
        }
    }

    public function create_data($data)
    {
        $query_inser = $this->db->insert('tbl_form', $data);
        if ($query_inser == true) {
            $this->db->select('*');
            $this->db->from('tbl_form');
            $query = $this->db->get();
            foreach ($query->result_array() as $row) {
                $json_data[] = array('label'=>$row['text'],'value'=>$row['id']);
            }
            $cache_file ='./assets/json/data.json';
            $dataList = json_encode($json_data);
            file_put_contents($cache_file,$dataList);
            return true;
        } else {
            return false;
        }
    }

/* Select Query */

    public function get_data()
    {
        $this->db->select('*');
        $this->db->from('tbl_form');
        $this->db->where('status', 0);
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return false;
        }
    }

    public function get_data($id)
    {
        $sql = "SELECT * FROM `tbl_form`  WHERE id = '$id' ORDER BY `id` DESC";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return false;
        }
    }

    public function get_data()
    {
        $this->db->select('*');
        $this->db->where('status', 0);
        $this->db->order_by('id', 'DESC');
        $query = $this->db->get('tbl_form');

        if ($query->num_rows() > 0) {
            $resultData = $query->result_array();
            $listData = array();
            foreach ($resultData as $key => $result) {
                $nestedData['id']           = $result['id'];
                $nestedData['text']         = $result['text'];
                $nestedData['dropdown']     = $result['dropdown'];
                $nestedData['radio']        = $result['radio'];
                $nestedData['checkbox']     = $result['checkbox'];
                $nestedData['cdate']        = $result['cdate'];
                $nestedData['udate']        = $result['udate'];
                $nestedData['status']       = $result['status'];
                $nestedData['price']        = $this->get_data($result['id']);
                $listData[]                 = $nestedData;
            }
            return $listData;
        } else {
            return false;
        }
    }

    public function get_data()
    {
        $this->db->select('tbl_form.*,tbl_events.*');
        $this->db->from('tbl_form');
        $this->db->where('tbl_form.id', $this->session->userdata('uuid'));
        $this->db->join('tbl_events', 'tbl_form.id=tbl_events.id', 'INNER');
        $this->db->order_by('tbl_form.id', 'DESC');
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return false;
        }
    }

/* Update Query */

    public function update_data($id)
    {
        $this->db->where('id', $id);
        return $this->db->update('tbl_form', ['status' => 2]);
    }

    public function update_data($id, $data)
    {
        $this->db->where('id', $id);
        return $this->db->update('tbl_form', $data);
    }

    public function update_data($data)
    {
        $this->db->where('tbl_form.id', $data['id']);
        $this->db->set('tbl_form.text', $data['text']);
        $this->db->set('tbl_form.udate', $data['udate']);
        $query = $this->db->update('tbl_form');

        if ($query) {
            $this->db->select('*');
            $this->db->from('tbl_form');
            $this->db->where('tbl_form.id', $data['id']);
            return $query = $this->db->get()->row_array();
        } else {
            return false;
        }

    }

/* Delete Query */

    public function delete_data($id)
    {
        $this->db->where('id', $id);
        return $this->db->delete('tbl_form');
    }

    public function delete_data($id)
    {
        $this->db->delete('tbl_form', array('id' => $id));
    }

}

Comments

Popular posts from this blog

API

Encryption and Decryption By PHP

Seeder