2015-05-28 15 views
2

我正在創建一個表單,我想製作一個自定義錯誤日誌,例如當用戶離開電子郵件爲空時,我想給我的error_helper可變的電子郵件,發送給用戶,他沒有填寫這個變量,並記錄他留下這個空的。我是codeIgniter的入門者,我已閱讀手冊,但沒有找到有用的東西。這裏是我的代碼:你可以給幫手一個變量,並改變它在CodeIgniter中使用的函數

public function submitCheck() { 
    $this->load->model("User"); 
    if(empty($post['email'] || 'email'=="")) 
    { 
     echo "Email is empty."; //This will be changed to a view for user  
     $this->load->helper('error'); //Loading the helper 
     errorLog('Firstname'); //Variable I want to give to the errorLog function. 
     exit(); 
    }  

所以在這裏我給我的錯誤日誌的變量,至極我想在我error_helper使用

function errorLog(){   
    if(errorLog() == 'email') //Here I want to change the error function based on what variable I give to errorLog 
    { 
     log_message('error', 'Email was empty'); 
     exit(); 
    } 
} 

這是可能做或一些其他的方式怎麼可以根據我給我的errorLog變量來改變函數。 在此先感謝

回答

2

你需要傳遞parameter到您的輔助函數

控制器文件

public function submitCheck() { 
    $this->load->model("User"); 
    if(empty($post['email'])) 
    { 
     // echo "Email is empty."; //This will be changed to a view for user  
     $this->load->helper('error'); //Loading the helper 
    echo $msg= errorLog('Firstname'); //Variable I want to give to the errorLog function. 
    } 

助手文件

function errorLog($var){ // get the variable thet you have pass from your helper   
     if($var == 'email') //Here I want to change the error function based on what variable I give to errorLog 
     { 
      return log_message('error', 'Email was empty');// here you use return type 

     } 
     if($var == 'test') 
     { 
      return log_message('error', 'Test was empty');// here you use return type 

     } 
} 

而且加載幫手controller constructor文件或文件autoload.php

+0

這個工作對我來說,非常感謝!你的意思是我需要創建一個控制器構造函數或在我自己的控制器中創建一個? (對不起noob問題) – RDAxRoadkill

1

讀取參數的函數:

function errorLog($var){   
    if($var== 'email') //Here I want to change the error function based on what variable I give to errorLog 
    { 
     log_message('error', 'Email was empty'); 
     exit(); 
    } 
} 
相關問題