2013-06-01 64 views
0

我是一般的MVC模式和OOP的新手。从一个函数内调用函数

我在CodeIgniter中有一个应用程序,基本部分工作。有一个表单要求输入用户名和密码。如果它们与数据库匹配,则用户登录,创建会话并将其重定向到站点的另一部分。这一切工作正常。

我现在想添加两个更多的功能,其中一个updateloggedonuser用于在用户登录时更新数据库,将位字段设置为1并设置时间戳。另一种是getusersname。这一个我想从数据库中获取登录用户的名字,姓氏和ID,并返回它们,以便我可以将它们存储在会话变量中。

我得到一个Fatal error: Call to undefined function getusersname()

控制器:

class Login_c extends CI_Controller { 

    function index(){ 
     $view['main_content'] = 'loginform_view'; 
     $this->load->view('includes/template', $view); 
    } 

    function validate_credentials() { 
     $this->load->model('login_m'); 
     $query = $this->login_m->validate(); 
     if($query) // if the users credentials valid 
     { 
      $data = array(
       'username' => $this->input->post('username'), 
       'is_logged_in' => true 
       ); 

      $this->session->set_userdata($data); 
      redirect('site_c/main_area'); 
     } 
     else 
     { 
      $this->index(); 
     } 
    } 

    function logout(){ 
     $this->index(); 
     $this->session->sess_destroy(); 
    } 

} 

型号:

class Login_m extends CI_Model { 

    function validate(){ 
     $this->db->where('UserName', $this->input->post('username')); 
     $this->db->where('UserPassword', md5($this->input->post('password'))); 
     $query = $this->db->get('User'); 
     $userN = $this->input->post('username'); 
     if($query->num_rows == 1) 
     { 
      getusersname($UserN); 
      updateloggedonuser($UserN); 
      return true;  
     } 
    } 


    function updateloggedonuser($UserN){ 
     // with username set isActive to 1 and Updatedate to update 'datetime' 
     $date = new DateTime(); 
     $data = array('isActive' =>1, 
        'UpdateDate' => $date); 
     $this->db->where('UserName', $UserN); 
     $this->db->update('User',$data);  
    } 

    function getusersname($UserN){ 
     // with username get FirstName , LastName and UserID from Database 
     $sql = "SELECT UserID, FirstName, LastName FROM User WHERE UserName =?"; 
     $q= $this->db->query($sql,$UserN); 
     if($q->num_rows() ==1){ 
      foreach ($q->result() as $row) { 
       $userdata[] = $row; 
      } 
      return $userdata; 
     } 
    } 

} 

我想你可以看到我想要做的,但很明显,我不这样做的正确方法。

回答

1

尝试这样

$this->my_function_name(); 

所以他们会

if($query->num_rows == 1) 
{ 
    $this->getusersname($UserN); 
    $this->updateloggedonuser($UserN); 
    return true;  
} 

$此实际上代表了单笨实例,它实际上是一个控制器对象。

+0

谢谢你所有的工作。 – Greyhounddad

1

在第二modal例子,你需要改变getusername()$this->getusername();

function validate(){ 

    $this->db->where('UserName', $this->input->post('username')); 
    $this->db->where('UserPassword', md5($this->input->post('password'))); 
    $query = $this->db->get('User'); 

    $userN = $this->input->post('username'); 
    if($query->num_rows == 1) 
    { 
    $this->getusersname($UserN); 
    $this->updateloggedonuser($UserN); 
    return true;  

    } 

} 
1

你要调用的方法,因此,你需要添加$this->的方法调用:

$this->getusersname($UserN); 

在相当多的编程语言将类方法中的函数调用解释为方法调用。在PHP中,你总是要指定你想调用$this->的方法,否则他会查看全局函数名称空间。

相关问题