CodeIgniterコード宝物庫
CodeIgniterでよく使うコードだけを集めました。
CodeIgniterコード
<?php
/*ファイルの読み込み*/
$this->load->model('');//一枚
$this->load->library('');
$this->load->helper('');
//初期構成時によく使う読み込み
$this->load->library(array('session', 'parser', 'form_validation', 'pagination', 'user_agent'));//複数
$this->load->helper(array('form', 'url', 'array', 'file','cookie'));
//サイトのURL
base_url();
//ファイルのアップ先
FCPATH
//現在のページを取得
$this->uri->segment('1');
/*configデータ取得*/
$this->ci->config->load('file_name');
$row = $this->ci->config->item('key');
/*POST,GET処理時*/
$this->input->post();
$this->input->get();
//変数代入を一行で分岐
$x = ($this->uri->segment('1'))? $this->uri->segment('1') : 'y';
$x = ($this->input->get('get'))? $this->input->get('get') : 'y';
//テンプレート利用(viewsディレクトリから引用)
$body = $this->parser->parse('mail/e_mail', $data, TRUE);
/*Model*/
$this->db->select('key1,key2,key3');
$this->db->select('tb.*, "hoge" as type', FALSE);//selectに配列を追加する場合
$this->db->from($table);
$this->db->join($table, 'tb1.key1 = tb2.key1');
$this->db->where('key1', $data);
$this->db->where_not_in('key1', $data);
$this->db->where_in('key1', $data);
$this->db->or_where('key1', $data);
$this->db->where('date_format(date, "%Y-%m-%d") =', "'$date'", false);//フォーマットして検索
$this->db->where('col IS NOT NULL', null, false)//nullでないものを探す場合
$this->db->group_by($group);
$this->db->limit($limit);
$this->db->order_by($order);
$query = $this->db->get();
return $query->result('array');
return $query->row_array();
return $query->num_rows();
return $this->db->count_all_results();//$this->db->get();を省略の場合
$this->db->flush_cache();//取得条件をリセット
//開発環境でのみ表示
<?php if (ENVIRONMENT == 'development'):?>
<?php endif; ?>
注意
・良く使うものだけを載せています。
・今後も増やしていく予定です。
