切換
舊版
前往
大廳
主題

CodeIgniter 基礎練習

路人甲 | 2015-02-21 23:06:30 | 巴幣 0 | 人氣 265

Codelgniter

$_POST 資料
$user = $this -> input ->post('user',TURE);

$_GET 資料
$user = $this -> input -> get('user',TURE);

一起判斷

$user = $this -> input -> get_post('user',TURE); //先找POST後找GET

資料庫

查詢

$this -> db -> select('title.content,data');
$this -> db -> from ('table');
$this -> db -> where ('title',$title);
$query = $this -> db -> get();

//如同 SELECT title,content,date FROM table where title = $title

也可使用傳統查詢方式
$this -> db -> query('查詢語法');

新增
$data = array(
'title' => 'My title' ,
'name'  => 'My name'  ,
'data   => 'My data'  
);
$this -> db -> insert('table',$data);
//如同 INSERT INTO table (title,name,data) value('My title','My name' ,'My data')

回傳ID值
$this -> db -> insert_id();

更新
$data = array(
'title' => 'My title' ,
'name'  => 'My name'  ,
'data   => 'My data'  
);
$this -> db ->where('id',$id);
$this -> db ->upfate('table',$data);

//如同 UPDATE table SET title ='{$title}',name

刪除
$this -> db -> where('id',$id);
$this -> db ->delete('table');

// 如同 DELETE FROM table where id = $id;
刪除多項
$tables = array ('table1','table2','table3');
$this -> db -> where('id','5');
$this -> db ->delete('tables');

顯示單筆資料(物件版)
result()

$query = $this -> db -> query("查詢語法")

if ($query -> num_rows() >0)
{
foreach ($query -> result() as $row)
{
echo $row -> title;
echo $row -> name;
echo $row -> body;
}
}

顯示單筆資料(陣列版)
result_arry()

$query = $this -> db -> query("查詢語法")

if ($query -> num_rows() >0)
{
foreach ($query -> result_array() as $row)
{
echo $row['title'];
echo $row['name'];
echo $row['body'];
}
}

載入

載入View
$this->load->view('view名稱');

載入model
$this->load->model('model名稱');

載入library
$this->load->library('library名稱');

載入helper
$this->load->helper('helper名稱');

設定自動載入

在application/config/autoload.php中加入
//自動載入package
autoload['packages'] = array();
//自動載入library
autoload['librarys'] = array();
//自動載入helper
autoload['helper'] = array();
//自動載入model
autoload['model'] = array();

創作回應

更多創作