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

时间:2014-10-09 06:42:06

标签: php codeigniter

这是我第一次做网络编程。我想创建一个我可以在某些函数上使用的变量,我使用public $username;public $password;并使用$this->username$this->password;但它没有用。这是我在控制器上的代码;

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

我也试过public $usernamepublic $password,但仍然无法获得

在我的模特上;

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

那么如何才能将包含col1和col2的can_log转换为其他函数?

2 个答案:

答案 0 :(得分:0)

也许是这样的?

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

然后调用函数

with_parameter($can_log);

答案 1 :(得分:0)

我并不了解确切的要求,但如果适用于您,则尝试代码如下。
遵循了 CI指南,您需要学习

<强>控制器:

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


型号:

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