working on it ...

Filters

Explore Public Snippets

Sort by

Found 571 snippets matching: codeigniter

    public by Zenitram.PRO modified May 3, 2016  4161  6  6  1

    CODEIGNITER | DB Functions

    Commonly used database function using codeigniter
    php
    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
         
        class General_model extends CI_Model
        {
            public function __construct()
            {
                parent::__construct();
            }
         
            // Return all records in the table
            public function get_all($table)
            {
                $q = $this->db->get($table);
                if($q->num_rows() > 0)
                {
                    return $q->result();
                }
                return array();
            }
         
            // Return only one row
            public function get_row($table,$primaryfield,$id)
            {
                $this->db->where($primaryfield,$id);
                $q = $this->db->get($table);
                if($q->num_rows() > 0)
                {
                    return $q->row();
                }
                return false;
            }
         
            // Return one only field value
            public function get_data($table,$primaryfield,$fieldname,$id)
            {
                $this->db->select($fieldname);
                $this->db->where($primaryfield,$id);
                $q = $this->db->get($table);
                if($q->num_rows() > 0)
                {
                    return $q->result();
                }
                return array();
            }
         
            // Insert into table
            public function add($table,$data)
            {
                return $this->db->insert($table, $data);
            }
         
            // Update data to table
            public function update($table,$data,$primaryfield,$id)
            {
                $this->db->where($primaryfield, $id);
                $q = $this->db->update($table, $data);
                return $q;
            }
         
            // Delete record from table
            public function delete($table,$primaryfield,$id)
            {
            	$this->db->where($primaryfield,$id);
            	$this->db->delete($table);
            }
         
            // Check whether a value has duplicates in the database
            public function has_duplicate($value, $tabletocheck, $fieldtocheck)
            {
                $this->db->select($fieldtocheck);
                $this->db->where($fieldtocheck,$value);
                $result = $this->db->get($tabletocheck);
         
                if($result->num_rows() > 0) {
                    return true;
                }
                else {
                    return false;
                }
            }
         
            // Check whether the field has any reference from other table
            // Normally to check before delete a value that is a foreign key in another table
            public function has_child($value, $tabletocheck, $fieldtocheck)
            {
                $this->db->select($fieldtocheck);
                $this->db->where($fieldtocheck,$value);
                $result = $this->db->get($tabletocheck);
         
                if($result->num_rows() > 0) {
                    return true;
                }
                else {
                    return false;
                }
            }
         
            // Return an array to use as reference or dropdown selection
            public function get_ref($table,$key,$value,$dropdown=false)
            {
                $this->db->from($table);
                $this->db->order_by($value);
                $result = $this->db->get();
         
                $array = array();
                if ($dropdown)
                    $array = array("" => "Please Select");
         
                if($result->num_rows() > 0) {
                    foreach($result->result_array() as $row) {
                    $array[$row[$key]] = $row[$value];
                    }
                }
                return $array;
            }
        }                        

    public by azazqadir modified Jan 18, 2018  462  1  5  0

    Adding Captcha in CodeIgniter

    Source: https://www.cloudways.com/blog/captcha-in-codeigniter/
    <?php
    
    defined('BASEPATH') OR exit('your exit message');
    
    class Captcha extends CI_Controller
    
    {
    
       function __construct()
    
       {
    
           parent::__construct();
    
           $this->load->helper('captcha');
    
       }
    
       public function index()
    
       {
    
           if ($this->input->post('submit')) {
    
               $captcha_insert = $this->input->post('captcha');
    
               $contain_sess_captcha = $this->session->userdata('valuecaptchaCode');
    
               if ($captcha_insert === $contain_sess_captcha) {
    
                   echo 'Success';
    
               } else {
    
                   echo 'Failure';
    
               }
    
           }
    
           $config = array(
    
               'img_url' => base_url() . 'image_for_captcha/',
    
               'img_path' => 'image_for_captcha/',
    
               'img_height' => 45,
    
               'word_length' => 5,
    
               'img_width' => '45',
    
               'font_size' => 10
    
           );
    
           $captcha = create_captcha($config);
    
           $this->session->unset_userdata('valuecaptchaCode');
    
           $this->session->set_userdata('valuecaptchaCode', $captcha['word']);
    
           $data['captchaImg'] = $captcha['image'];
    
           $this->load->view('captcha/index', $data);
    
       }
    
       public function refresh()
    
       {
    
           $config = array(
    
               'img_url' => base_url() . 'image_for_captcha/',
    
               'img_path' => 'image_for_captcha/',
    
               'img_height' => 45,
    
               'word_length' => 5,
    
               'img_width' => '45',
    
               'font_size' => 10
    
           );
    
           $captcha = create_captcha($config);
    
           $this->session->unset_userdata('valuecaptchaCode');
    
           $this->session->set_userdata('valuecaptchaCode', $captcha['word']);
    
           echo $captcha['image'];
    
       }
    
    }

    public by NassimB modified Jul 22, 2016  1393  3  5  0

    codeigniter-pagination-url-with-get-parameters

    <?
    $this->load->library('pagination');
    		
    $item_per_page = 48;
    $total_rows = //query here for total num of items from model
    	
    // thanks to Aurel for the trick see http://stackoverflow.com/questions/5384644/codeigniter-pagination-url-with-get-parameters
    if (count($_GET) > 0) $config['suffix'] = '?' . http_build_query($_GET, '', "&");
    	
    $config['base_url']  = base_url().'/controller/method/';
    $config['first_url'] = $config['base_url'].'0?'.http_build_query($_GET);
    	
    $config['per_page']  = $item_per_page;
    $config['total_rows'] = $total_rows; 
    		
    $this->pagination->initialize($config);
    $data['pagination'] =  $this->pagination->create_links();
    	
    $offset = $this->uri->segment(3);
    if(!$offset) $offset = 0;
    $data['result'] = // query with offset and limit ($item_per_page)
    	
    //call view
    $this->load->view("view",$data)
    

    public by teslamint modified Aug 30, 2012  1936  7  3  0

    CodeIgniter CI_Model extended

    CodeIgniter CI_Model extended: MY_Model.php
    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    /**
     * extended CI_Model
     *
     * @access public
     * @extends CI_Model
     * @see http://thetechnofreak.com/technofreak/hook-system-php/ for hook system reference
     */
    class MY_Model extends CI_Model {
    	private $key_field = 'id';
    	private $fields = array('id');
    	private $table_name = '';
    	private $table_prefix = '';
    	private $required_fields = array();
    	private $optional_fields = array();
    	private $hooks = array();
    	protected $allow_operator = '/[<>]\=?\ |\!\=\ /i';
    
    	function __construct() {
    		parent::__construct();
    		$this->add_action('post_count_all_result_hook', 'self::_post_count_all_result_hook');
    		$this->add_action( 'before_get_where_loop', 'self::_before_get_where_loop' );
    		$this->add_action( 'after_get_where_loop', 'self::_after_get_where_loop' );
    		$this->_log('started.');
    	}
    
    	function __destruct() {
    		$this->_log('end.');
    	}
    
    	/**
    	 * get function.
    	 *
    	 * @access public
    	 * @param array $options (default: array())
    	 * @return void
    	 */
    	public function get($options = array()) {
    		$this->_log('started.');
    		if (!empty($this->table_prefix)) {
    			$table_prefix = "{$this->table_prefix}.";
    		} else {
    			$table_prefix = "";
    		}
    		// where hook
    		$this->execute_action('before_get_where_loop', $options);
    		foreach($options as $key => $value) {
    			if (in_array($key, $this->fields) && isset($value)) {
    				if (preg_match('/^(not )?null$/i', $value)) {
    					$this->db->where("{$table_prefix}{$key} IS ". strtoupper($value));
    				} elseif (strpos($value, '%') !== FALSE) {
    					$this->db->where("{$table_prefix}{$key} LIKE '{$value}'");
    				} elseif (preg_match($this->allow_operator, $value, $matches) > 0) {
    					$this->db->where("{$table_prefix}{$key} {$matches[0]}", trim(preg_replace($this->allow_operator, '', $value)));
    				} else {
    					$this->db->where("{$table_prefix}{$key}", $value);
    				}
    			}
    			$this->execute_action('inside_get_where_loop', array($key => $value));
    		}
    		$this->execute_action('after_get_where_loop', $options);
    		if (!empty($options['order_by']) && in_array($options['order_by'], $this->fields)) {
    			if (!empty($options['desc']))
    				$options['desc'] = $options['desc'] === 'desc' ? 'desc' : 'asc';
    			else
    				$options['desc'] = 'asc';
    			$this->db->order_by($options['order_by'], $options['desc']);
    		}
    		if (!empty($options['limit'])) {
    			$options['start'] = !empty($options['start']) ? $options['start'] : 0;
    			$this->db->limit($options['limit'], $options['start']);
    		}
    		// select hook
    		$this->execute_action('select_hook', $options);
    		if (!empty($this->table_prefix))
    			$query_table = sprintf("%s AS %s", $this->table_name, $this->table_prefix);
    		else $query_table = $this->table_name;
    		// pre_get hook
    		$this->execute_action('pre_get_hook', $options);
    		$query = $this->db->get($query_table);
    		// post_get hook
    		$this->execute_action('post_get_hook', array('options' => $options, 'query' => &$query));
    		$this->_log('get query: '. $this->db->last_query());
    		if (!$query) {
    			$this->_log('error', $this->db->_error_message());
    			return FALSE;
    		}
    		$this->_log('end.');
    		return $query->result();
    	}
    
    	/**
    	 * insert function.
    	 *
    	 * @access public
    	 * @param array $options (default: array())
    	 * @return void
    	 */
    	public function insert($options = array()) {
    		$this->_log('started.');
    		$this->execute_action('before_insert_where_loop', $options);
    		foreach($this->required_fields as $key) {
    			if (array_key_exists($key, $options) && isset($options[$key])) {
    				$this->db->set($key, $options[$key]);
    				unset($options[$key]);
    			}
    		}
    		foreach($this->optional_fields as $key) {
    			if (array_key_exists($key, $options) && !empty($options[$key])) {
    				$this->db->set($key, $options[$key]);
    				unset($options[$key]);
    			}
    		}
    		$this->execute_action('after_insert_where_loop', $options);
    		if ( count( $options ) ) {
    			// unknown column
    			$this->_log('error', 'unknown column: '. var_export($options, TRUE));
    		}
    
    		$this->execute_action('pre_insert_hook', $options);
    		$query = $this->db->insert($this->table_name);
    		$this->execute_action('post_insert_hook', $options);
    		$this->_log('insert query: '. $this->db->last_query());
    		if (!$query) {
    			$this->_log('error', $this->db->_error_message());
    			return FALSE;
    		}
    		$id = $this->db->insert_id();
    		$this->_log('insert_id: '.$id);
    		$this->_log('end.');
    		return intval($id);
    	}
    
    
    	/**
    	 * insert with ignore option
    	 *
    	 * @param unknown $options (optional)
    	 * @return unknown
    	 */
    	public function insert_ignore($options = array()) {
    		$this->_log('started.');
    		$this->execute_action('before_insert_where_loop', $options);
    		foreach ($this->required_fields as $key) {
    			if (array_key_exists($key, $options) && isset($options[$key])) {
    				$this->db->set($key, $options[$key]);
    				unset($options[$key]);
    			}
    		}
    		foreach ($this->optional_fields as $key) {
    			if (array_key_exists($key, $options) && !empty($options[$key])) {
    				$this->db->set($key, $options[$key]);
    				unset($options[$key]);
    			}
    		}
    		if ( count( $options ) ) {
    			// unknown column
    			$this->_log('error', 'unknown column: '. var_export($options, TRUE));
    		}
    
    		$this->execute_action('pre_insert_ignore_hook', $options);
    		$insert_query = $this->db->insert_string($this->table_name);
    		$insert_query = str_replace('INSERT INTO', 'INSERT IGNORE INTO', $insert_query);
    		$query = $this->db->query($insert_query);
    		$this->execute_action('post_insert_ignore_hook', $options);
    		$this->_log('insert query: '. $this->db->last_query());
    		if (!$query) {
    			$this->_log('error', $this->db->_error_message());
    			return FALSE;
    		}
    		// required to use mysqli as db driver
    		$id = $this->db->mysql_insert_id();
    		$this->_log('insert_id: '.$id);
    		$this->_log('end.');
    		return intval($id);
    	}
    
    
    	/**
    	 * insert_batch function.
    	 *
    	 * @access public
    	 * @param array $options (default: array())
    	 * @return void
    	 */
    	public function insert_batch($options = array()) {
    		$this->_log('started.');
    		$this->execute_action('before_insert_batch_query', $options);
    		$query = $this->db->insert_batch($this->table_name, $options, $this->key_field);
    		$this->execute_action('after_insert_batch_query', $options);
    		$this->_log('insert batch query: '. $this->db->last_query());
    		if (!$query) {
    			$this->_log('error', $this->db->_error_message());
    			return FALSE;
    		}
    		$this->_log('end.');
    		return $this->db->affected_rows();
    	}
    
    	/**
    	 * update function.
    	 *
    	 * @access public
    	 * @param array $options (default: array())
    	 * @return void
    	 */
    	public function update($options = array()) {
    		$this->_log('started.');
    		$this->execute_action('before_update_where_loop', $options);
    		foreach($options as $key => $value) {
    			if (strncmp($key, $this->key_field, strlen($this->key_field)) === 0)
    				continue;
    			if (in_array($key, $this->fields) && isset($value)) {
    				if ( $value === NULL || strcmp($value, 'NULL') === 0 || $value === '')
    					$this->db->set($key, NULL);
    				else
    					$this->db->set($key, $value);
    			} else {
    				$this->_log('error', "unknown column or no value: {$key} = {$value}");
    			}
    		}
    		$this->execute_action('after_update_where_loop', $options);
    		$this->db->where($this->key_field, $options[$this->key_field]);
    		$this->execute_action('pre_update_hook', $options);
    		$query = $this->db->update($this->table_name);
    		$this->execute_action('post_update_hook', $options);
    		if ( ! $query ) {
    			$this->_log('error', $this->db->_error_message());
    			return FALSE;
    		}
    		$this->_log('update query: '. $this->db->last_query());
    		$this->_log('end.');
    		return $this->db->affected_rows();
    	}
    
    	/**
    	 * update_batch function.
    	 *
    	 * @access public
    	 * @param array $options (default: array())
    	 * @return void
    	 */
    	public function update_batch($options = array()) {
    		$this->_log('started.');
    		if (count($options) < 1)
    			return FALSE;
    		$this->execute_action('pre_update_batch_hook', $options);
    		$query = $this->db->update_batch($this->table_name, $options, $this->key_field);
    		$this->execute_action('post_update_batch_hook', $options);
    		$this->_log('update batch query: '. $this->db->last_query());
    		$this->_log('end.');
    		if ($query)
    			return $this->db->affected_rows();
    		else {
    			$this->_log('error', $this->db->_error_message());
    			return FALSE;
    		}
    	}
    
    	/**
    	 * delete function.
    	 *
    	 * @access public
    	 * @param float $brand_id (default: 0)
    	 * @return void
    	 */
    	public function delete($id = 0) {
    		$this->_log('started.');
    		$result = FALSE;
    
    		if ( intval( $id ) ) {
    			$this->db->where($this->key_field, $id);
    			$this->execute_action('pre_delete_hook', array('id' => $id));
    			$query = $this->db->delete($this->table_name);
    			$this->execute_action('post_delete_hook', array('id' => $id));
    			$this->_log('delete query: '. $this->db->last_query());
    			if (!$query) {
    				$this->_log('error', $this->db->_error_message());
    			} else
    				$result = TRUE;
    		}
    
    		$this->_log('end.');
    		return $result;
    	}
    
    	/**
    	 * count_all function.
    	 *
    	 * @access public
    	 * @return integer - count of records
    	 */
    	public function count_all() {
    		$this->_log('started.');
    		$result = 0;
    		$this->execute_action('pre_count_all_hook', array());
    		if (!empty($this->table_prefix))
    			$query_table = sprintf("%s AS %s", $this->table_name, $this->table_prefix);
    		else $query_table = $this->table_name;
    		$result = $this->db->count_all($query_table);
    		$this->_log('count_all query: '. $this->db->last_query());
    		$this->execute_action('post_count_all_hook', array());
    		$this->_log('end.');
    		return $result;
    	}
    
    
    	/**
    	 * get count of query results
    	 * @param  array  $options columns
    	 * @return integer - count of records
    	 */
    	public function count_all_results($options = array()) {
    		$this->_log('started.');
    		$result = 0;
    		$this->execute_action('pre_count_all_result_hook', array());
    		if (!empty($this->table_prefix))
    			$query_table = sprintf("%s AS %s", $this->table_name, $this->table_prefix);
    		else $query_table = $this->table_name;
    		$result = $this->db->count_all_results($query_table);
    		$this->_log('count_all_results query: '. $this->db->last_query());
    		$this->execute_action('post_count_all_result_hook', array());
    		$this->_log('end.');
    		return $result;
    	}
    
    // hook functions -----------------------------
    
    	/**
    	 * add hook action
    	 * @param string  $where    name of hook to use
    	 * @param string  $callback name of function
    	 * @param integer $priority priority of function call (0~, default: 50)
    	 */
    	public function add_action($where, $callback, $priority = 50) {
    		$this->_log('started.');
    		if ( !isset($this->hooks[$where])) {
    			$this->hooks[$where] = array();
    		}
    		if (intval($priority) < 0) {
    			$priority = abs($priority);
    		}
    		if ( isset( $this->hooks[$where][$callback] ) && $this->hooks[$where][$callback] === $priority ) {
    			// skip
    			$this->_log('callback already registered. skipped.');
    		} else {
    			$this->hooks[$where][$callback] = $priority;
    			$this->_log('add callback: '. $callback .' to: '. $where);
    		}
    		$this->_log('end.');
    	}
    
    
    	/**
    	 * remove hook action
    	 *
    	 * @param  string $where    name of hook to use
    	 * @param  string $callback name of function
    	 */
    	public function remove_action($where, $callback) {
    		$this->_log('started.');
    		if ( isset($this->hooks[$where][$callback])) {
    			unset($this->hooks[$where][$callback]);
    			$this->_log( 'remove callback: '. $callback .' from: '. $where );
    		}
    		$this->_log('end.');
    	}
    
    
    	/**
    	 * execute hook action
    	 *
    	 * @param  string $where name of hook to use
    	 * @param  array  $args  array of arguments
    	 */
    	public function execute_action($where, $args = array()) {
    		$this->_log('started.');
    		$class = get_class($this);
    		if ( isset($this->hooks[$where]) && is_array($this->hooks[$where])) {
    			arsort($this->hooks[$where]);
    			foreach ($this->hooks[$where] as $callback => $priority) {
    				$this->_log( 'executing callback: '. $callback .' with priority: '. $priority );
    				call_user_func(array($class, $callback), $args);
    			}
    		}
    		$this->_log('end.');
    	}
    
    
    // get/set functions --------------------------
    
    	/**
    	 *
    	 *
    	 * @return unknown
    	 */
    	public function get_fields() {
    		return $this->fields;
    	}
    
    
    	/**
    	 *
    	 *
    	 * @param unknown $name (optional)
    	 */
    	public function set_table_name($name = '') {
    		$this->table_name = $name;
    	}
    
    
    	/**
    	 *
    	 *
    	 * @return unknown
    	 */
    	public function get_table_name() {
    		return $this->table_name;
    	}
    
    
    	/**
    	 *
    	 *
    	 * @param unknown $value (optional)
    	 */
    	public function set_table_prefix($value='')
    	{
    		$this->table_prefix = $value;
    	}
    
    
    	/**
    	 *
    	 *
    	 * @return unknown
    	 */
    	public function get_table_prefix()
    	{
    		return $this->table_prefix;
    	}
    
    
    	/**
    	 *
    	 *
    	 * @param unknown $fields (optional)
    	 */
    	public function set_required_fields($fields = array()) {
    		$this->required_fields = $fields;
    	}
    
    
    	/**
    	 *
    	 *
    	 * @return unknown
    	 */
    	public function get_required_fields() {
    		return $this->required_fields;
    	}
    
    
    	/**
    	 *
    	 *
    	 * @param unknown $fields (optional)
    	 */
    	public function set_optional_fields($fields = array()) {
    		$this->optional_fields = $fields;
    	}
    
    
    	/**
    	 *
    	 *
    	 * @param unknown $fields (optional)
    	 * @return unknown
    	 */
    	public function get_optional_fields($fields = array()) {
    		return $this->optional_fields;
    	}
    
    
    	/**
    	 *
    	 *
    	 * @param unknown $field (optional)
    	 */
    	public function set_key_field($field = 'id') {
    		$this->key_field = $field;
    	}
    
    
    	/**
    	 *
    	 *
    	 * @return unknown
    	 */
    	public function get_key_field() {
    		return $this->key_field;
    	}
    
    
    	/**
    	 *
    	 *
    	 * @param unknown $more_fields (optional)
    	 */
    	public function combine_fields($more_fields = array()) {
    		$this->fields = array_merge(array($this->key_field), $this->required_fields, $this->optional_fields, $more_fields);
    		$this->fields = array_flip(array_flip($this->fields)); // remove null or duplicate element just in case.
    		$this->_log('$this->fields: '. var_export($this->fields, TRUE));
    	}
    
    
    // protected function ----------------
    
    	/**
    	 * logging message via CI's log_message()
    	 */
    	protected function _log() {
    		$num_arg = func_num_args();
    		if ( ! intval( $num_arg ) )
    			return;
    		$trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT );
    		$func = $trace[1]['function'];
    		$line = $trace[0]['line'];
    		$object = $trace[1]['object'];
    		if ( is_object( $object ) )
    			$class = get_class( $object );
    		else
    			$class = get_called_class();
    
    		$level = 'debug';
    		$message = '';
    
    		switch ( $num_arg ) {
    		case 1:
    			$message = func_get_arg( 0 );
    			break;
    		case 2:
    			$message = func_get_arg( 1 );
    			$level = ( strcasecmp( $func, func_get_arg( 0 ) ) === 0 ) ? $level : func_get_arg( 0 );
    			$func = ( ! method_exists( $class, $func ) ) ? func_get_arg( 0 ) : $func;
    			break;
    		default:
    			list( $func, $level, $message ) = func_get_args();
    			if ( ! method_exists( $class, $func ) ) {
    				// maybe swapped
    				$temp = $func;
    				$func = $level;
    				$level = $func;
    			}
    		}
    
    		$message = str_replace( PHP_EOL, ' ', $message );
    		log_message( $level, "{$class}::{$func}({$line}): {$message}" );
    	}
    
    	/**
    	 * start db query cache
    	 */
    	protected function _before_get_where_loop() {
    		$this->db->flush_cache();
    		$this->db->start_cache();
    		$this->_log( 'db cache start' );
    	}
    
    	/**
    	 * stop db query cache
    	 */
    	protected function _after_get_where_loop() {
    		$this->db->stop_cache();
    		$this->_log( 'db cache stop' );
    	}
    
    	/**
    	 * flush db cache after get count of results
    	 */
    	protected function _post_count_all_result_hook() {
    		$this->_log('running!');
    		$this->db->flush_cache();
    	}
    
    }
    
    
    

    public by nsssim modified Sep 2, 2015  1862  7  5  0

    calling a controller from another controller in codeigniter - no hmvc- Nassim baci

    calling a controller from another controller in codeigniter - no hmvc- we ll need Request library http://requests.ryanmccue.info/
    $this->load->library('PHPRequests');
    $this->load->helper("url");
    $response_url = base_url()."controller/method";
    $response_str = Requests::get($response_url);
    echo($response_str->body);
    $json_data = json_decode($response_str->body);

    public by teslamint modified Sep 4, 2012  2113  0  3  0

    custom log function for CodeIgniter

    custom log function for CodeIgniter: log.php
    <?php
    class example extends CI_Controller {
    	/**
    	 * leave log message
    	 *
    	 * @access private
    	 * @param  string $func    (default: __FUNCTION__)
    	 * @param  string $level   (default: 'debug')
    	 * @param  string $message (default: '')
    	 * @return void
    	 */
    	private function __log() {
    		$num_arg = func_num_args();
    		$trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT );
    		$func = $trace[1]['function'];
    		$line = $trace[0]['line'];
    		$object = $trace[1]['object'];
    		if ( is_object( $object ) )
    			$class = get_class( $object );
    		else
    			$class = get_called_class();
    
    		$level = 'debug';
    		$message = '';
    
    		switch ( $num_arg ) {
    		case 1:
    			$message = func_get_arg( 0 );
    			break;
    		case 2:
    			$message = func_get_arg( 1 );
    			$func = method_exists( $this, func_get_arg( 0 ) ) ? func_get_arg( 0 ) : $func;
    			$level = ( strcasecmp( $func, func_get_arg( 0 ) ) === 0 ) ? $level : func_get_arg( 0 );
    			break;
    		default:
    			list( $func, $level, $message ) = func_get_args();
    			if ( ! method_exists( $class, $func ) ) {
    				// maybe swapped
    				$temp = $func;
    				$func = $level;
    				$level = $func;
    			}
    		}
    
    		$message = str_replace( PHP_EOL, '', $message );
    		log_message( $level, "{$class}::{$func}({$line}): {$message}" );
    	}
    }
    
    

    public by cyberrspiritt modified Oct 11, 2015  1662  0  6  0

    Codeigniter Sample Controller

    Simple codeigniter controller initialization
    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    class Controllername extends CI_Controller {
    
        function __construct(){
             parent::__constrcut();
        }
    
    	public function index()
    	{
    		
    	}
    
    }
    
    /* End of file controllername.php */
    /* Location: ./application/controllers/controllername.php */

    public by jaehoon modified Aug 30, 2012  1593  0  3  0

    CodeIgniter CI_Model extended

    CodeIgniter CI_Model extended: MY_Model.php
    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    /**
     * extended CI_Model
     *
     * @access public
     * @extends CI_Model
     * @see http://thetechnofreak.com/technofreak/hook-system-php/ for hook system reference
     */
    class MY_Model extends CI_Model {
    	private $key_field = 'id';
    	private $fields = array('id');
    	private $table_name = '';
    	private $table_prefix = '';
    	private $required_fields = array();
    	private $optional_fields = array();
    	private $hooks = array();
    	protected $allow_operator = '/[<>]\=?\ |\!\=\ /i';
    
    	function __construct() {
    		parent::__construct();
    		$this->add_action('post_count_all_result_hook', 'self::_post_count_all_result_hook');
    		$this->add_action( 'before_get_where_loop', 'self::_before_get_where_loop' );
    		$this->add_action( 'after_get_where_loop', 'self::_after_get_where_loop' );
    		$this->_log('started.');
    	}
    
    	function __destruct() {
    		$this->_log('end.');
    	}
    
    	/**
    	 * get function.
    	 *
    	 * @access public
    	 * @param array $options (default: array())
    	 * @return void
    	 */
    	public function get($options = array()) {
    		$this->_log('started.');
    		if (!empty($this->table_prefix)) {
    			$table_prefix = "{$this->table_prefix}.";
    		} else {
    			$table_prefix = "";
    		}
    		// where hook
    		$this->execute_action('before_get_where_loop', $options);
    		foreach($options as $key => $value) {
    			if (in_array($key, $this->fields) && isset($value)) {
    				if (preg_match('/^(not )?null$/i', $value)) {
    					$this->db->where("{$table_prefix}{$key} IS ". strtoupper($value));
    				} elseif (strpos($value, '%') !== FALSE) {
    					$this->db->where("{$table_prefix}{$key} LIKE '{$value}'");
    				} elseif (preg_match($this->allow_operator, $value, $matches) > 0) {
    					$this->db->where("{$table_prefix}{$key} {$matches[0]}", trim(preg_replace($this->allow_operator, '', $value)));
    				} else {
    					$this->db->where("{$table_prefix}{$key}", $value);
    				}
    			}
    			$this->execute_action('inside_get_where_loop', array($key => $value));
    		}
    		$this->execute_action('after_get_where_loop', $options);
    		if (!empty($options['order_by']) && in_array($options['order_by'], $this->fields)) {
    			if (!empty($options['desc']))
    				$options['desc'] = $options['desc'] === 'desc' ? 'desc' : 'asc';
    			else
    				$options['desc'] = 'asc';
    			$this->db->order_by($options['order_by'], $options['desc']);
    		}
    		if (!empty($options['limit'])) {
    			$options['start'] = !empty($options['start']) ? $options['start'] : 0;
    			$this->db->limit($options['limit'], $options['start']);
    		}
    		// select hook
    		$this->execute_action('select_hook', $options);
    		if (!empty($this->table_prefix))
    			$query_table = sprintf("%s AS %s", $this->table_name, $this->table_prefix);
    		else $query_table = $this->table_name;
    		// pre_get hook
    		$this->execute_action('pre_get_hook', $options);
    		$query = $this->db->get($query_table);
    		// post_get hook
    		$this->execute_action('post_get_hook', array('options' => $options, 'query' => &$query));
    		$this->_log('get query: '. $this->db->last_query());
    		if (!$query) {
    			$this->_log('error', $this->db->_error_message());
    			return FALSE;
    		}
    		$this->_log('end.');
    		return $query->result();
    	}
    
    	/**
    	 * insert function.
    	 *
    	 * @access public
    	 * @param array $options (default: array())
    	 * @return void
    	 */
    	public function insert($options = array()) {
    		$this->_log('started.');
    		$this->execute_action('before_insert_where_loop', $options);
    		foreach($this->required_fields as $key) {
    			if (array_key_exists($key, $options) && isset($options[$key])) {
    				$this->db->set($key, $options[$key]);
    				unset($options[$key]);
    			}
    		}
    		foreach($this->optional_fields as $key) {
    			if (array_key_exists($key, $options) && !empty($options[$key])) {
    				$this->db->set($key, $options[$key]);
    				unset($options[$key]);
    			}
    		}
    		$this->execute_action('after_insert_where_loop', $options);
    		if ( count( $options ) ) {
    			// unknown column
    			$this->_log('error', 'unknown column: '. var_export($options, TRUE));
    		}
    
    		$this->execute_action('pre_insert_hook', $options);
    		$query = $this->db->insert($this->table_name);
    		$this->execute_action('post_insert_hook', $options);
    		$this->_log('insert query: '. $this->db->last_query());
    		if (!$query) {
    			$this->_log('error', $this->db->_error_message());
    			return FALSE;
    		}
    		$id = $this->db->insert_id();
    		$this->_log('insert_id: '.$id);
    		$this->_log('end.');
    		return intval($id);
    	}
    
    
    	/**
    	 * insert with ignore option
    	 *
    	 * @param unknown $options (optional)
    	 * @return unknown
    	 */
    	public function insert_ignore($options = array()) {
    		$this->_log('started.');
    		$this->execute_action('before_insert_where_loop', $options);
    		foreach ($this->required_fields as $key) {
    			if (array_key_exists($key, $options) && isset($options[$key])) {
    				$this->db->set($key, $options[$key]);
    				unset($options[$key]);
    			}
    		}
    		foreach ($this->optional_fields as $key) {
    			if (array_key_exists($key, $options) && !empty($options[$key])) {
    				$this->db->set($key, $options[$key]);
    				unset($options[$key]);
    			}
    		}
    		if ( count( $options ) ) {
    			// unknown column
    			$this->_log('error', 'unknown column: '. var_export($options, TRUE));
    		}
    
    		$this->execute_action('pre_insert_ignore_hook', $options);
    		$insert_query = $this->db->insert_string($this->table_name);
    		$insert_query = str_replace('INSERT INTO', 'INSERT IGNORE INTO', $insert_query);
    		$query = $this->db->query($insert_query);
    		$this->execute_action('post_insert_ignore_hook', $options);
    		$this->_log('insert query: '. $this->db->last_query());
    		if (!$query) {
    			$this->_log('error', $this->db->_error_message());
    			return FALSE;
    		}
    		// required to use mysqli as db driver
    		$id = $this->db->mysql_insert_id();
    		$this->_log('insert_id: '.$id);
    		$this->_log('end.');
    		return intval($id);
    	}
    
    
    	/**
    	 * insert_batch function.
    	 *
    	 * @access public
    	 * @param array $options (default: array())
    	 * @return void
    	 */
    	public function insert_batch($options = array()) {
    		$this->_log('started.');
    		$this->execute_action('before_insert_batch_query', $options);
    		$query = $this->db->insert_batch($this->table_name, $options, $this->key_field);
    		$this->execute_action('after_insert_batch_query', $options);
    		$this->_log('insert batch query: '. $this->db->last_query());
    		if (!$query) {
    			$this->_log('error', $this->db->_error_message());
    			return FALSE;
    		}
    		$this->_log('end.');
    		return $this->db->affected_rows();
    	}
    
    	/**
    	 * update function.
    	 *
    	 * @access public
    	 * @param array $options (default: array())
    	 * @return void
    	 */
    	public function update($options = array()) {
    		$this->_log('started.');
    		$this->execute_action('before_update_where_loop', $options);
    		foreach($options as $key => $value) {
    			if (strncmp($key, $this->key_field, strlen($this->key_field)) === 0)
    				continue;
    			if (in_array($key, $this->fields) && isset($value)) {
    				if ( $value === NULL || strcmp($value, 'NULL') === 0 || $value === '')
    					$this->db->set($key, NULL);
    				else
    					$this->db->set($key, $value);
    			} else {
    				$this->_log('error', "unknown column or no value: {$key} = {$value}");
    			}
    		}
    		$this->execute_action('after_update_where_loop', $options);
    		$this->db->where($this->key_field, $options[$this->key_field]);
    		$this->execute_action('pre_update_hook', $options);
    		$query = $this->db->update($this->table_name);
    		$this->execute_action('post_update_hook', $options);
    		if ( ! $query ) {
    			$this->_log('error', $this->db->_error_message());
    			return FALSE;
    		}
    		$this->_log('update query: '. $this->db->last_query());
    		$this->_log('end.');
    		return $this->db->affected_rows();
    	}
    
    	/**
    	 * update_batch function.
    	 *
    	 * @access public
    	 * @param array $options (default: array())
    	 * @return void
    	 */
    	public function update_batch($options = array()) {
    		$this->_log('started.');
    		if (count($options) < 1)
    			return FALSE;
    		$this->execute_action('pre_update_batch_hook', $options);
    		$query = $this->db->update_batch($this->table_name, $options, $this->key_field);
    		$this->execute_action('post_update_batch_hook', $options);
    		$this->_log('update batch query: '. $this->db->last_query());
    		$this->_log('end.');
    		if ($query)
    			return $this->db->affected_rows();
    		else {
    			$this->_log('error', $this->db->_error_message());
    			return FALSE;
    		}
    	}
    
    	/**
    	 * delete function.
    	 *
    	 * @access public
    	 * @param float $brand_id (default: 0)
    	 * @return void
    	 */
    	public function delete($id = 0) {
    		$this->_log('started.');
    		$result = FALSE;
    
    		if ( intval( $id ) ) {
    			$this->db->where($this->key_field, $id);
    			$this->execute_action('pre_delete_hook', array('id' => $id));
    			$query = $this->db->delete($this->table_name);
    			$this->execute_action('post_delete_hook', array('id' => $id));
    			$this->_log('delete query: '. $this->db->last_query());
    			if (!$query) {
    				$this->_log('error', $this->db->_error_message());
    			} else
    				$result = TRUE;
    		}
    
    		$this->_log('end.');
    		return $result;
    	}
    
    	/**
    	 * count_all function.
    	 *
    	 * @access public
    	 * @return integer - count of records
    	 */
    	public function count_all() {
    		$this->_log('started.');
    		$result = 0;
    		$this->execute_action('pre_count_all_hook', array());
    		if (!empty($this->table_prefix))
    			$query_table = sprintf("%s AS %s", $this->table_name, $this->table_prefix);
    		else $query_table = $this->table_name;
    		$result = $this->db->count_all($query_table);
    		$this->_log('count_all query: '. $this->db->last_query());
    		$this->execute_action('post_count_all_hook', array());
    		$this->_log('end.');
    		return $result;
    	}
    
    
    	/**
    	 * get count of query results
    	 * @param  array  $options columns
    	 * @return integer - count of records
    	 */
    	public function count_all_results($options = array()) {
    		$this->_log('started.');
    		$result = 0;
    		$this->execute_action('pre_count_all_result_hook', array());
    		if (!empty($this->table_prefix))
    			$query_table = sprintf("%s AS %s", $this->table_name, $this->table_prefix);
    		else $query_table = $this->table_name;
    		$result = $this->db->count_all_results($query_table);
    		$this->_log('count_all_results query: '. $this->db->last_query());
    		$this->execute_action('post_count_all_result_hook', array());
    		$this->_log('end.');
    		return $result;
    	}
    
    // hook functions -----------------------------
    
    	/**
    	 * add hook action
    	 * @param string  $where    name of hook to use
    	 * @param string  $callback name of function
    	 * @param integer $priority priority of function call (0~, default: 50)
    	 */
    	public function add_action($where, $callback, $priority = 50) {
    		$this->_log('started.');
    		if ( !isset($this->hooks[$where])) {
    			$this->hooks[$where] = array();
    		}
    		if (intval($priority) < 0) {
    			$priority = abs($priority);
    		}
    		if ( isset( $this->hooks[$where][$callback] ) && $this->hooks[$where][$callback] === $priority ) {
    			// skip
    			$this->_log('callback already registered. skipped.');
    		} else {
    			$this->hooks[$where][$callback] = $priority;
    			$this->_log('add callback: '. $callback .' to: '. $where);
    		}
    		$this->_log('end.');
    	}
    
    
    	/**
    	 * remove hook action
    	 *
    	 * @param  string $where    name of hook to use
    	 * @param  string $callback name of function
    	 */
    	public function remove_action($where, $callback) {
    		$this->_log('started.');
    		if ( isset($this->hooks[$where][$callback])) {
    			unset($this->hooks[$where][$callback]);
    			$this->_log( 'remove callback: '. $callback .' from: '. $where );
    		}
    		$this->_log('end.');
    	}
    
    
    	/**
    	 * execute hook action
    	 *
    	 * @param  string $where name of hook to use
    	 * @param  array  $args  array of arguments
    	 */
    	public function execute_action($where, $args = array()) {
    		$this->_log('started.');
    		$class = get_class($this);
    		if ( isset($this->hooks[$where]) && is_array($this->hooks[$where])) {
    			arsort($this->hooks[$where]);
    			foreach ($this->hooks[$where] as $callback => $priority) {
    				$this->_log( 'executing callback: '. $callback .' with priority: '. $priority );
    				call_user_func(array($class, $callback), $args);
    			}
    		}
    		$this->_log('end.');
    	}
    
    
    // get/set functions --------------------------
    
    	/**
    	 *
    	 *
    	 * @return unknown
    	 */
    	public function get_fields() {
    		return $this->fields;
    	}
    
    
    	/**
    	 *
    	 *
    	 * @param unknown $name (optional)
    	 */
    	public function set_table_name($name = '') {
    		$this->table_name = $name;
    	}
    
    
    	/**
    	 *
    	 *
    	 * @return unknown
    	 */
    	public function get_table_name() {
    		return $this->table_name;
    	}
    
    
    	/**
    	 *
    	 *
    	 * @param unknown $value (optional)
    	 */
    	public function set_table_prefix($value='')
    	{
    		$this->table_prefix = $value;
    	}
    
    
    	/**
    	 *
    	 *
    	 * @return unknown
    	 */
    	public function get_table_prefix()
    	{
    		return $this->table_prefix;
    	}
    
    
    	/**
    	 *
    	 *
    	 * @param unknown $fields (optional)
    	 */
    	public function set_required_fields($fields = array()) {
    		$this->required_fields = $fields;
    	}
    
    
    	/**
    	 *
    	 *
    	 * @return unknown
    	 */
    	public function get_required_fields() {
    		return $this->required_fields;
    	}
    
    
    	/**
    	 *
    	 *
    	 * @param unknown $fields (optional)
    	 */
    	public function set_optional_fields($fields = array()) {
    		$this->optional_fields = $fields;
    	}
    
    
    	/**
    	 *
    	 *
    	 * @param unknown $fields (optional)
    	 * @return unknown
    	 */
    	public function get_optional_fields($fields = array()) {
    		return $this->optional_fields;
    	}
    
    
    	/**
    	 *
    	 *
    	 * @param unknown $field (optional)
    	 */
    	public function set_key_field($field = 'id') {
    		$this->key_field = $field;
    	}
    
    
    	/**
    	 *
    	 *
    	 * @return unknown
    	 */
    	public function get_key_field() {
    		return $this->key_field;
    	}
    
    
    	/**
    	 *
    	 *
    	 * @param unknown $more_fields (optional)
    	 */
    	public function combine_fields($more_fields = array()) {
    		$this->fields = array_merge(array($this->key_field), $this->required_fields, $this->optional_fields, $more_fields);
    		$this->fields = array_flip(array_flip($this->fields)); // remove null or duplicate element just in case.
    		$this->_log('$this->fields: '. var_export($this->fields, TRUE));
    	}
    
    
    // protected function ----------------
    
    	/**
    	 * logging message via CI's log_message()
    	 */
    	protected function _log() {
    		$num_arg = func_num_args();
    		if ( ! intval( $num_arg ) )
    			return;
    		$trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT );
    		$func = $trace[1]['function'];
    		$line = $trace[0]['line'];
    		$object = $trace[1]['object'];
    		if ( is_object( $object ) )
    			$class = get_class( $object );
    		else
    			$class = get_called_class();
    
    		$level = 'debug';
    		$message = '';
    
    		switch ( $num_arg ) {
    		case 1:
    			$message = func_get_arg( 0 );
    			break;
    		case 2:
    			$message = func_get_arg( 1 );
    			$level = ( strcasecmp( $func, func_get_arg( 0 ) ) === 0 ) ? $level : func_get_arg( 0 );
    			$func = ( ! method_exists( $class, $func ) ) ? func_get_arg( 0 ) : $func;
    			break;
    		default:
    			list( $func, $level, $message ) = func_get_args();
    			if ( ! method_exists( $class, $func ) ) {
    				// maybe swapped
    				$temp = $func;
    				$func = $level;
    				$level = $func;
    			}
    		}
    
    		$message = str_replace( PHP_EOL, ' ', $message );
    		log_message( $level, "{$class}::{$func}({$line}): {$message}" );
    	}
    
    	/**
    	 * start db query cache
    	 */
    	protected function _before_get_where_loop() {
    		$this->db->flush_cache();
    		$this->db->start_cache();
    		$this->_log( 'db cache start' );
    	}
    
    	/**
    	 * stop db query cache
    	 */
    	protected function _after_get_where_loop() {
    		$this->db->stop_cache();
    		$this->_log( 'db cache stop' );
    	}
    
    	/**
    	 * flush db cache after get count of results
    	 */
    	protected function _post_count_all_result_hook() {
    		$this->_log('running!');
    		$this->db->flush_cache();
    	}
    
    }
    
    
    

    public by Aleksandar Popovic modified Feb 3, 2015  1556  0  3  0

    Codeigniter .htaccess

    Codeigniter .htaccess: .htaccess
    <IfModule mod_rewrite.c>
      RewriteEngine On
      # !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
      #  slashes.
      # If your page resides at
      #  http://www.example.com/mypage/test1
      # then use
      # RewriteBase /mypage/test1/
      RewriteBase /
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ index.php?/$1 [L]
    </IfModule>
     
    <IfModule !mod_rewrite.c>
      # If we don't have mod_rewrite installed, all 404's
      # can be sent to index.php, and everything works as normal.
      # Submitted by: ElliotHaughin
     
      ErrorDocument 404 /index.php
    </IfModule>
    
    

    public by jaehoon modified Sep 4, 2012  1442  0  3  0

    custom log function for CodeIgniter

    custom log function for CodeIgniter: log.php
    <?php
    class example extends CI_Controller {
    	/**
    	 * leave log message
    	 *
    	 * @access private
    	 * @param  string $func    (default: __FUNCTION__)
    	 * @param  string $level   (default: 'debug')
    	 * @param  string $message (default: '')
    	 * @return void
    	 */
    	private function __log() {
    		$num_arg = func_num_args();
    		$trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT );
    		$func = $trace[1]['function'];
    		$line = $trace[0]['line'];
    		$object = $trace[1]['object'];
    		if ( is_object( $object ) )
    			$class = get_class( $object );
    		else
    			$class = get_called_class();
    
    		$level = 'debug';
    		$message = '';
    
    		switch ( $num_arg ) {
    		case 1:
    			$message = func_get_arg( 0 );
    			break;
    		case 2:
    			$message = func_get_arg( 1 );
    			$func = method_exists( $this, func_get_arg( 0 ) ) ? func_get_arg( 0 ) : $func;
    			$level = ( strcasecmp( $func, func_get_arg( 0 ) ) === 0 ) ? $level : func_get_arg( 0 );
    			break;
    		default:
    			list( $func, $level, $message ) = func_get_args();
    			if ( ! method_exists( $class, $func ) ) {
    				// maybe swapped
    				$temp = $func;
    				$func = $level;
    				$level = $func;
    			}
    		}
    
    		$message = str_replace( PHP_EOL, '', $message );
    		log_message( $level, "{$class}::{$func}({$line}): {$message}" );
    	}
    }
    
    
    • Public Snippets
    • Channels Snippets