2014-10-09 52 views
0

這是我第一次進行網絡編程。我想使一個變量可以用於某些功能,我使用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; 
    } 
} 

所以我怎樣才能獲得can_log - 包含col1和col2 - 其他功能?

+0

$查詢 - >行()是不符合要求的烏爾? – 2014-10-09 06:53:04

+0

在模型中添加公共變量而不是在控制器中 – Kiran 2014-10-09 06:55:42

回答

0

也許是這樣的?

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

,然後調用函數

with_parameter($can_log); 
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; 
    } 
} 
相關問題