简体   繁体   English

如何在控制器功能之间传递变量

[英]How can I pass variable between controller functions

This is my first time doing web programming. 这是我第一次进行网络编程。 I want to make one variable that I can use on some functions, I use public $username; 我想做一个可以在某些功能上使用的变量,我使用public $username; and public $password; public $password; and use $this->username and $this->password; 并使用$this->username$this->password; but it didn't work. 但这没用。 This is my code on controller; 这是我在控制器上的代码;

public $can_log ;

public function home(){

    $this->load->model("model_get");

    $data["results"] = $can_log;

    $this->load->view("content_home",$data);
}

public function login(){
    $this->load->view("site_header");
    $this->load->view("content_login");
    $this->load->view("site_footer");
}

public function login_validation(){
    $this->load->library('form_validation');
    $this->load->view("site_header");
    $this->load->view("site_nav");

    $this->form_validation->set_rules('username','Username','required|trim|callback_validate_credentials');
    $this->form_validation->set_rules('password','Password','required|trim');// use md5 if want to encrpyt this

    if($this->form_validation->run()){
        redirect('site/home');
    } else {
        $this->load->view('content_login'); 
    }
}

public function validate_credentials(){
    $this->load->model('model_get');

    $username = $this->input->post('username');//"user";
    $password = $this->input->post('password');//"password";
    //I tried both but none of those work

    $this->can_log = $this->model_get->can_log_in($username, $password);

    if($this->can_log){
        return true;
    } else {
        $this->form_validation->set_message('validate_credentials','Incorrect username/password.');
        return false;
    }
}

I also tried with public $username and public $password but still can't get it 我也尝试使用public $usernamepublic $password但仍然无法获取它

on my model; 在我的模特上

public function can_log_in($username, $password){
    $query = $this->db->query("SELECT col1, col2 FROM table1 where id_login = '$username' and id_password = '$password'");

    if($query->num_rows() > 0) {
        $data = $query->result(); // fetches single row: $query->row();
        return $data; //fetches single column: $data->col1;
    }
}

so how can I get can_log - that contains col1 and col2 - to other function? 所以我怎样才能把can_log-包含col1和col2-到其他功能?

Maybe something like this? 也许是这样的吗?

public function with_parameter($parameter)
{
   do something with $parameter
}

And then call the function 然后调用函数

with_parameter($can_log);

I didn't understood the exact requirements, but try below code if it works for you. 我不了解确切的要求,但是请尝试以下代码(如果它适合您)。
Have followed some CI guidelines which you need to learn . 遵循了一些您需要学习的 CI指南

Controller: 控制器:

class Controller_name extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model("model_get");  // load models in constructor

        $this->can_log = "some value";   // is the way to define a variable
    }

    public function home()
    {
        $data["results"] = $this->can_log;  // is the way to retrieve value
        $this->load->view("content_home",$data);
    }

    public function validate_credentials()
    {
        $username = $this->input->post('username');
        $password = $this->input->post('password');    

        $is_valid = $this->model_get->can_log_in($username, $password);

        if($is_valid)
        {
            return true;
        } 
        else 
        {
            $this->form_validation->set_message('validate_credentials','Incorrect username/password.');
            return false;
        }
    }
}


Model: 模型:

class Model_get extends CI_Model
{
    public function can_log_in($username, $password)
    {
        $where_aray = array("id_login" => $username, "id_password" => $password);
        $query = $this->db->get_where("table", $where_array);

        if($query->num_rows() > 0)
            return $query->row();
        return false;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在同一个 controller (laravel) 中的函数之间传递变量? - how can i pass a variable between functions in the same controller (laravel)? 为什么我无法在Codeigniter的控制器功能之间传递变量 - Why I can not pass a variable between controller functions in codeigniter 在同一控制器中的函数之间传递变量 - pass variable between functions in same controller 如何将Ajax URL中的两个变量传递给codeigniter控制器? - How Can I pass two variable in Ajax URL to codeigniter controller? 如何将变量从控制器传递到路由文件? - How can I pass a variable from a controller to the routes file? Laravel:如何将变量从控制器传递给另一个? - Laravel:how can I pass a variable from a controller to another? 如何在 controller 中的方法之间传递变量 - How to pass variable between methods in a controller 我可以在Codeigniter中的函数之间传递数据吗? - Can i pass data between functions in codeigniter? 如何将变量传递给 Laravel 中的每个视图,而不必从每个控制器传递它? - How can I pass a variable to every view in Laravel without having to pass it from every controller? 如何将 controller 索引 function 中的参数传递给模型的函数? LARAVEL - How can I pass a parameter from the controller index function to the model's functions? LARAVEL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM