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
        ));
    }

}

How to load external font in css




How how to use in your code

.your-div-name {
  1. font-familymyFirstFont;

How to set time zone of mysql?

There are 3 places where the timezone might be set in MySQL:

in the file "my.cnf" in the [mysqld] section

default-time-zone='+00:00'

@@global.time_zone variable

To see what value they are set to
SELECT @@global.time_zone;
To set a value for it use either one:
SET GLOBAL time_zone = '+8:00';
SET GLOBAL time_zone = 'Europe/Helsinki';
SET @@global.time_zone='+00:00';
(using named timezones like 'Europe/Helsinki' means that you have to have a timezone table properly populated)

@@session.time_zone variable

SELECT @@session.time_zone;
To set it use either one:
SET time_zone = 'Europe/Helsinki';
SET time_zone = "+00:00";
SET @@session.time_zone = "+00:00";
both might return SYSTEM which means that they use the timezone set in my.cnf. For timezone names to work you must setup your timezone information tables need to be populated:http://dev.mysql.com/doc/refman/5.1/en/time-zone-support.html I also mention how to populate those tables in this answer

To get the current timezone

SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP);
It will return 02:00:00 if your timezone is +2:00.

To get the current UNIX timestamp:

SELECT UNIX_TIMESTAMP();
SELECT UNIX_TIMESTAMP(NOW());

To get the timestamp column as a UNIX timestamp

SELECT UNIX_TIMESTAMP(`timestamp`) FROM `table_name`

To get a UTC datetime column as a UNIX timestamp

SELECT UNIX_TIMESTAMP(CONVERT_TZ(`utc_datetime`, '+00:00', @@session.time_zone)) FROM `table_name`
Note: Changing the timezone will not change the stored datetime or timestamp, but it will show select a different datetime from timestamp columns

lazyload Image

JQuery

$(window).bind("load", function(){
 var timeout = setTimeout(function() {
  $('.lazyload1second').each(function(){      
   $(this).attr('src', $(this).attr('original_link')).fadeIn(700);
   $(this).removeAttr('original_link');   
  });
 }, 500);
});

HTML

width="100%"  src="//www.example.com/images/right_banner_loader.jpg" original_link="https://example.blob.core.windows.net/images/offers/website.jpg?v3=3" alt="True Rupees" class="lazyload1second">


Replace Or hide Broken Image

// Replace source
$('img').error(function(){
        $(this).attr('src', 'missing.png');
});

// Or, hide them
$("img").error(function(){
        $(this).hide();
});

Display Image Preview Be four Uploading Image.

See Live Demo.

http://stackoverflow.com/a/12369027


HTML



   type='file' onchange="readURL(this);" />
     id="blah" src="#" alt="your image" />

JQuery
function readURL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('#blah')
                    .attr('src', e.target.result)
                    .width(150)
                    .height(200);
            };

            reader.readAsDataURL(input.files[0]);
        }
}

Save CI Session to DB Config

$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_encrypt_cookie'] = TRUE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'cms_ci_sessions';
$config['sess_match_ip'] = TRUE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 7200;
$config['sess_expire_on_close'] = TRUE;