ds1379551 2015-05-28 08:02
浏览 6
已采纳

你能给助手一个变量,并改变它在CodeIgniter中使用的功能

I am creating a form and I want to make a custom error log, for example when the user leaves e-mail empty, I want to give my error_helper the variable email, sending to the user that he did not fill in this variable and log that he left this empty. I am a starter with codeIgniter and i've read the manual but I did not find anything helpful. Here is my code:

    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();
    }    

So here I've given my errorLog a variable, wich I want to use in my 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();
     }
}  

Is this possible to do or some other way how I can change the function based on what variable I give my errorLog. Thanks in advance

  • 写回答

2条回答 默认 最新

  • dongzhong5573 2015-05-28 08:07
    关注

    You need to pass parameter into your helper function

    Controller file

    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.
        }  
    

    Helper file

        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
    
             }
    }
    

    Also load your helper in controller constructor file or autoload.php file

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作