dongzhang1987 2012-12-03 23:29
浏览 30
已采纳

如何从模型中提取变量以在CodeIgniter中的控制器上使用?

I am trying to use a variable in my controller that has been created in my model.

Here is the model:

public function validate_retrieve($data) {

            $query = $this->db->where($data)->get('users', '1');

            foreach ($query->result() as $user)
            {
                $user->email;
                $user->salt;
                $user->id;

            }

            $token = sha1($user->email.$user->salt).dechex($user->id);
            $reset_token = array(
                'token' => $token,
                'email' => $user->email
            );

            $insert = $this->db->insert('reset', $reset_token, '1');
            return $reset_token;
        }

and on my controller I'm trying to use $token that was created in the model:

public function retrieve()
        // REQUEST PASSWORD RESET
        // LOADED WHEN THE FORM IS SUBMITTED OFF THE PASSWORD PAGE AND SENDS THE EMAIL WITH TOKEN AND INSTRUCTIONS
        {
            $this->load->library('form_validation');
            $this->load->library('session');
            $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
            $this->load->model('user_model', 'um');
            $this->load->library('encrypt');
            $this->load->helper('url');
            $submit = $this->input->post('submit');
            $salt = $this->_salt();

            if($submit)
            // IF THE SUBMIT BUTTON IS SET
            {

                // START PROCESS TO CREATE $USER VARIABLE THAT HOLDS WHAT THE USER ENTERED IN THE FORM AND THAT CAN GET CHECKED AGAINST THE DB IN THE MODEL
                $user = $this->um->validate_retrieve(array('email' => $this->input->post('email')));


                // IF THE USER IS CREATED AND CHECKS OUT AND ALL OF THE ERRORS ARE CLEARED ON THE FORM
                if( $user && $this->form_validation->run() == TRUE ) {

                    $domain = "clci.dev/index.php";

                    // CREATE A TOKEN LINK TO SEND TO THE USERS EMAIL THAT EXIST IN THE DB AND WAS ENTERED

                    $token = $this->um->token;
                    $link = "http://www.".$domain."/auth/reset/?token=$token";


                        $this->load->library('email');

                        $this->email->from('noreply@cysticlife.org', 'CysticLife');
                        $this->email->to($this->input->post('email')); 

                        $this->email->subject('Reset Password');
                        $this->email->message("Please go to the following web address to reset your password:

$link

-Your friends at CysticLife

Please remember to add the cysticlife.org domain to your address book to ensure that you receive your CysticLife e-Notifications as requested.");  

                        $this->email->send();
                        redirect('auth/success');
                        exit;

                    }
                    $this->form_validation->run() == FALSE;
                    $data['main_content'] = 'auth/password';
                    $this->load->view('includes/templates/main_page_template', $data);
                    $data['email_error'] = 'This email is invalid';
                }
            }

thanks in advance

  • 写回答

1条回答 默认 最新

  • dongshuzhuo5659 2012-12-03 23:33
    关注

    You're trying to access an array as an object.

    $reset_token = $this->um->validate_retrieve($whatever_id);
    $token = $reset_token['token'];
    

    That's how you access the token portion of the returned results from this function.

    And according to your latest update, you would actually access it like this:

    $token = $user['token'];
    

    Because in the following line you assign the results of the validate_retrieve method to the variable $user:

    $user = $this->um->validate_retrieve(array('email' => $this->input->post('email')));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了