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