Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从函数中调用函数_Php_Codeigniter - Fatal编程技术网

Php 从函数中调用函数

Php 从函数中调用函数,php,codeigniter,Php,Codeigniter,一般来说,我对MVC模式和OOP都是新手 我在CodeIgniter中有一个应用程序,基本部分工作正常。有一个表单要求输入用户名和密码。如果它们与数据库匹配,则用户将登录,创建会话,并将它们重定向到站点的另一部分。这一切都很好 现在我想再添加两个函数,一个是在用户登录时更新数据库,将位字段设置为1并设置时间戳 另一个是getusersname。我想从数据库中获取登录用户的名字、姓氏和ID,并返回它们,以便将它们存储在会话变量中 我收到一个致命错误:调用未定义的函数getusersname() 控

一般来说,我对MVC模式和OOP都是新手

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

现在我想再添加两个函数,一个是在用户登录时更新数据库,将位字段设置为1并设置时间戳

另一个是
getusersname
。我想从数据库中获取登录用户的名字、姓氏和ID,并返回它们,以便将它们存储在会话变量中

我收到一个
致命错误:调用未定义的函数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;
        }
    }

}
我想你可以看到我想做什么,但显然我做得不对。

像这样试试

$this->my_function_name();
他们会是这样的

if($query->num_rows == 1) 
{
    $this->getusersname($UserN);
    $this->updateloggedonuser($UserN);
    return true;    
}
$this实际上表示singleton Codeigniter实例,它实际上是控制器对象。

像这样试试

$this->my_function_name();
他们会是这样的

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

$this实际上代表了singleton Codeigniter实例,它实际上是控制器对象。

在第二个
modal
示例中,您需要将
getusername()
更改为
$this->getusername()


在第二个
modal
示例中,您需要将
getusername()
更改为
$this->getusername()


您想调用该方法,因此需要将
$this->
添加到方法调用中:

$this->getusersname($UserN);

在许多编程语言中,类方法中的函数调用被解释为方法调用。在PHP中,您必须始终指定要使用
$this->
调用方法,否则他将查看全局函数名称空间。

您要调用该方法,因此需要将
$this->
添加到方法调用中:

$this->getusersname($UserN);

在许多编程语言中,类方法中的函数调用被解释为方法调用。在PHP中,您必须始终指定要使用
$this->
调用方法,否则他将查看全局函数名称空间。

感谢您所做的一切。感谢您所做的一切。