這是我第一次進行網絡編程。我想使一個變量可以用於某些功能,我使用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 $username
和public $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;
}
}
所以我怎樣才能獲得can_log - 包含col1和col2 - 其他功能?
$查詢 - >行()是不符合要求的烏爾? – 2014-10-09 06:53:04
在模型中添加公共變量而不是在控制器中 – Kiran 2014-10-09 06:55:42