本文實例講述了CodeIgniter框架常見用法。分享給大家供大家參考,具體如下:
1、codeigniter控制器超級對象和屬性
1
2
3
4
|
$this ->load; $this ->load->database(); $this ->load->view(); $this ->load->helper(); |
1
2
|
$this ->uri; $this ->uri->segment(3); |
1
|
$this ->input; |
2、數(shù)據(jù)庫配置
1
2
|
$this ->load->database(); $this ->db->query( 'SELECT * FROM blog_user' ); |
配置交換表前綴
1
2
|
$db [ 'default' ][ 'dbprefix' ] = 'blog_' ; $db [ 'default' ][ 'swap_pre' ] = 'my_' ; |
那么我們在寫sql語句時就用my_這個表前綴,ci會自動把my_換位blog_,所以,dbprefix可以隨便修改,方便我們修改數(shù)據(jù)庫名。
如:
1
|
$sql = "SELECT * FROM my_archive" ; |
3、表單提交路徑
1
|
$this ->load->helper( 'url' ); |
用
1
|
site_url( '控制器/方法名' ) |
4、表單驗證(可參考前面的文章 《CodeIgniter表單驗證方法實例詳解》及《CI框架表單驗證實例詳解》)
5、SQL語句相關(guān)
① 插入
1
2
3
|
$this ->db->insert( 'archive' , $archive ); 返回bool值 $insert_id = $this ->db->insert_id(); $this ->db->insert_batch( 'archive' , $data ); //插入多條 |
② 查詢
1
2
3
4
5
6
7
8
|
$query = $this ->db->query( $sql ); //返回Object $query ->num_rows() 或者 $query ->num_rows 返回查詢出多少條 if ( $query ->num_rows() > 0){ return $query ->result(); //$query->row() $query->result_array() $query->row_array() } else { return false; } $query ->last_query(); |
③ 更新
1
2
|
$bool = $this ->db->where( 'id >' , '74835' )->update( 'archive' , $data ); $this ->db->affected_rows(); //影響行數(shù) |
④ 刪除
1
2
3
|
$bool = $this ->db-> delete ( 'tablename' , array ( 'id' => '500' )); $bool = $this ->db->where( array ( 'id' => 500))-> delete ( 'tablename' ); $this ->db->affected_rows(); //影響行 |
希望本文所述對大家基于CodeIgniter框架的PHP程序設(shè)計有所幫助。