WordPress database insert custom query.


function perform_database_action(){
    global $wpdb;
    $data= array('col1'=>$value1,'col2'=>$value2,'col3'=>$value3);
    $format = array('%s','%s','%s');
    $wpdb->insert('table_name', $data, $format);
}

Codeigniter Pagination Example (Query String )

Before Going through the complete article I assume that you have a basic idea of CodeIgniter,

Controller Code:

public function user_location($start_from = 0) {

        $offset = ($this->uri->segment(3) != '' ? $this->uri->segment(3) : 0);

        $config = array();
        $config["total_rows"] = $this->Reports_model->user_record_count();
        $config['per_page'] = 4;
        $config['full_tag_open'] = '
        $config['full_tag_close'] = '
';        $config['first_link'] = 'First';
        $config['last_link'] = 'Last';
        $config['uri_segment'] = 3;
        $config['use_page_numbers'] = TRUE;
        $config["base_url"] = base_url() . "reports/user_location";
        $config['suffix'] = '?' . http_build_query($_GET, '', "&");
        $config['first_url'] = base_url() . 'reports/user_location/?' . http_build_query($_GET, '', "&");

        $this->pagination->initialize($config);

        $data['links'] = $this->pagination->create_links();

        if (!empty($start_from)) {
            $start = $config['per_page'] * ($start_from - 1);
        } else {
            $start = 0;
        }

        $param = array();
        $param = $_GET;

        $data['results'] = $this->Reports_model->fetch_user($config["per_page"], $start, $param);


     
        $data['header'] = $this->load->view('main_header', '', true);
        $data['menu'] = $this->load->view('main_menu', '', true);
        $data['footer'] = $this->load->view('main_footer', '', true);

        $this->load->view('user_list', $data);
    }

Model Code :


public function fetch_user($limit, $start, $param) {
        $return_result = array();
        $this->db->select('user.mobile,user_type_master.name,user.city_name');
        $this->db->from('user');
        $this->db->join('user_type_master', 'user_type_master.id = user.user_type_id', 'LEFT');

        if (isset($param['user_type']) && !empty($param['user_type'])) {
            $this->db->where('user.user_type_id', $param['user_type']);
        }

        $this->db->limit($limit, $start);

        $result = $this->db->get();

        if ($result && $result->num_rows() > 0) {
            $return_result = $result->result_array();
            foreach ($return_result as $key => $value) {
                if (isset($value ['profile_image']) && !empty($value ['profile_image'])) {
                    $return_result [$key] ['profile_image'] = DESIGNS_MEDIA_URL . $value ['profile_image'];
                }
            }
        }

        return $return_result;

    }


View Code :

I don't think its required here.

Codeigniter Model ( Select, Insert , Update , Delete ) Snippets


class xyz_model extends CI_Model {

    const TABLE_NAME = 'xyz';

    public function __construct() {
        $this->load->database();
    }

    public function saveData($data) {
        $return_id = 0;
        if (!empty($data)) {
            $insert_status = $this->db->insert(self::TABLE_NAME, $data);
            if ($insert_status) {
                $return_id = $this->db->insert_id();
            }
        }
        return $return_id;
    }

    public function updateData($update_params, $condition_params) {
        $return_status = 0;
        if (!empty($update_params) && !empty($condition_params)) {
            $this->db->where($condition_params);
            $this->db->update(self::TABLE_NAME, $update_params);
            $return_status = $this->db->affected_rows();
        }
        return $return_status;
    }

    public function getData($id, $fields = '') {
        $return_result = array();
        if ($id > 0) {
            if (!empty($fields)) {
                $this->db->select($fields);
            }
            $result = $this->db->get_where(self::TABLE_NAME, array(
                'id' => $id
                    ), $limit = 1, $offset = 0);

            if ($result && $result->num_rows() > 0) {
                $return_result = $result->result_array();
            }
        }
        return $return_result;
    }

    public function delete($id) {
        return $this->db->delete(self::TABLE_NAME, array(
                    'id' => $id
        ));
    }

}