顯示具有 CodeIgniter 標籤的文章。 顯示所有文章
顯示具有 CodeIgniter 標籤的文章。 顯示所有文章

2011年3月9日 星期三

[CodeIgniter] 驗證表單 form_validation

CodeIgniter 內建就有驗證的功能
先載入form_validation的library
function __construct()
 {
  parent::__construct();
  $this->load->library('form_validation');
        }

以下試驗證的函數
function checkadduser()//表單驗證
 { 
   $config=array(  //設定驗證規則
         array(  
         'field'=>'account',  //input的name
         'label'=>'帳號',     //設定名稱,在輸出錯誤訊息時出現的名稱
         'rules'=>'trim|required|xss_clean'  //驗證使用的規則
           ),  
         array(  
          'field'=>'password',  
          'label'=>'密碼',  
          'rules'=>'required|min_length[5]'  
           )
          );  
  $this->form_validation->set_rules($config);  
  $this->form_validation->set_message('required','%s不能為空值');   //%s是帶入上面所設定的label
  $this->form_validation->set_message('min_length','%s長度不能小於%s');
  $this->form_validation->set_error_delimiters('

','

'); //改變錯誤訊息的類別為error if($this->form_validation->run() ==FALSE) //判斷是否符合格式 { $this->adduserview(); //註冊失敗回到原頁面 } else { $this->adduser(); } }
接下來在view的顯示畫面加上錯誤訊息的顯示位置
//錯誤訊息的程式碼 

如果要錯誤訊息出現紅字就要到CSS裡面設定
把剛剛設定為error 的類別文字顏色變成紅色
p.errorp {
 color:  #f00;
}

在CodeIgniter還有內建許多驗證規則
可以到手冊裡面去看
參考網址:
CodeIgniter 使用手冊 Version 2.0.0

2011年3月7日 星期一

[CodeIgniter] 製作分頁 pagination函式

用CodeIgniter 製作出像下面的分頁


先在Controllers中寫下下列函式
function index()
 {
  $this->page(0);//呼叫page函式並帶入0的數值
   
 }
function page($set)
 {
  $this->load->library('pagination');   
  $data['title'] = "留言板"; //設定title
  $data['heading'] = "留言板";//設定標題 
  $data['all'] = $this->db->count_all_results('book'); 
  $config['base_url'] = base_url().'/book/page/';//設定頁面輸出網址
  $config['total_rows'] = $this->db->count_all_results('book'); //計算所有筆數
  $config['per_page'] = '10'; //一個分頁的數量
  //以下是設定樣式
  $config['full_tag_open']   = '';
  $config['first_link']      = '首頁';
  $config['last_link']       = '末頁';
  $config['next_link']       = '下一頁>';
  $config['prev_link']       = '<上一頁';
  
  $this->pagination->initialize($config);//初始化 
  $this->db->order_by('id','desc');
  $this->db->limit($config['per_page'],$set);
   $data['query']=$this->db->get('book');
        $data['pagelist']=$this->pagination->create_links();//顯示分頁,如果沒有分頁不會印出
        $this->load->view('book_viewend',$data);
   
 }

2011年3月1日 星期二

[CodeIgniter] 移除網址的index.php

1.打開Apache  的httpd.conf
   搜尋 LoadModule找到下面這行
   #LoadModule rewrite_module modules/mod_rewrite.so
   將前面的#字號去除後存檔
2.restart Apache

3.將CodeIgniter資料夾下的.htaccess檔用筆記本打開修改內容
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
4. 修改 \application\config\cofig.php
   搜尋 $config['index_page'] = 'index';
   去除預設值得index變成下面這樣
   $config['index_page'] = '';

完成了!!!
就不需要在加上index.php

[CodeIgniter]Unable to locate the model you have specifie: book

今天出現了這個問題 Unable to locate the model you have specified: book

原來是config下的autoload內的$autoload[model] 設錯了
我以為是要輸入資料庫名稱,原來是要輸入model的php檔名

2010年12月22日 星期三

CodeIgniter 學習筆記:安裝

將下載的CI資料夾放到WWW資料夾裡
剛開始裝就出現這個問題
Fatal error: Call to undefined function: ob_get_level() in c:\apache\htdocs\codeigniter\system\libraries\Loader.php on line 56
的URL
google一下原來是PHP版本的問題,重新下載了appserv-2.4.7 並修改了 application/config/config.php  
裡面的URL 改成127.0.0.1/(CI資料夾)


開啟瀏覽器輸入網址就出現了下圖

安裝成功
繼續研究中