如何使用模型检索数据并将其传递到使用 CodeIgniter 的控制器中使用?

发布于 2024-11-13 09:28:57 字数 631 浏览 2 评论 0原文

从用户登录表单中,我需要使用他们的用户名 ($this->input->post('username')) 来查询我的membership 表数据库,以便我可以检索用户 firstnamelastname,我认为这应该通过从我的控制器调用的模型来完成。然后,我需要将该数据传递回我的控制器,以使用 firstnamelastname 添加到我的会话 userdata 中,该会话在我的会话中设置控制器,如下图所示。

$data = array(
   'username' => $this->input->post('username'),
   'firstname' => //need to retrieve value from database,
   'lastname' => //need to retrieve value from database,
   'is_logged_in' => true
);

$this->session->set_userdata($data);

谢谢, 克里斯坦.

From a user login form i need to use their username ($this->input->post('username')) to query against the membership table in my database so that I can retrieve the users firstname and lastname, which I believe should be done via a model called from my controller. I then need to pass that data back to my controller to use the firstname and lastname to add in to my session userdata which is set in my controller, as shown below.

$data = array(
   'username' => $this->input->post('username'),
   'firstname' => //need to retrieve value from database,
   'lastname' => //need to retrieve value from database,
   'is_logged_in' => true
);

$this->session->set_userdata($data);

Thanks,
Kristan.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱格式化 2024-11-20 09:28:57

在您的模型中,创建一个函数来检索用户的名字和姓氏:

 public function get_user_details($username){
     // retrieve contents from db:
     // the first username here is the name of the column in your table

     $query = $this->db->select('firstname, lastname')
                       ->where('username', $username)
                       ->get('membership');
     return $query->result_array();
 }

在您的控制器中,就像您所做的那样,抓取控制器中的 POST 内容:

 $username = $this->input->post('username');

然后,从您的控制器中调用您的模型函数,但保存输出该模型函数在变量中:

 $data = $this->user_model->get_user_details($username);

之后,您可以做您想做的事情:

 $this->session->set_userdata($data);

希望有帮助。这是我凭空写下的,但它应该有效。我还建议您仔细阅读 CI 文档,因为它确实是我见过的关于框架的最佳文档之一。祝你好运。

In your model, make a function to retrieve the first and last name of a user:

 public function get_user_details($username){
     // retrieve contents from db:
     // the first username here is the name of the column in your table

     $query = $this->db->select('firstname, lastname')
                       ->where('username', $username)
                       ->get('membership');
     return $query->result_array();
 }

In your controller, like you were doijng, grab the POST contents in your controller:

 $username = $this->input->post('username');

Then, from your controller, call your model function but save the output of that model function in a variable:

 $data = $this->user_model->get_user_details($username);

After that, you can do what you were trying to do:

 $this->session->set_userdata($data);

Hope that helps. I wrote this from the top of my head but it should work. I also suggest you read the CI documentation through because it really is some of the best documentation I have ever seen for a framework. Good luck.

ˇ宁静的妩媚 2024-11-20 09:28:57

请查看 型号的 CodeIgniter 用户指南Blog 控制器和 Blog 模型的示例将为您解答。

class Blogmodel extends CI_Model {

    var $title   = '';
    var $content = '';
    var $date    = '';

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    function get_last_ten_entries()
    {
        $query = $this->db->get('entries', 10);
        return $query->result();
    }

    function insert_entry()
    {
        $this->title   = $_POST['title']; // please read the below note
        $this->content = $_POST['content'];
        $this->date    = time();

        $this->db->insert('entries', $this);
    }

    function update_entry()
    {
        $this->title   = $_POST['title'];
        $this->content = $_POST['content'];
        $this->date    = time();

        $this->db->update('entries', $this, array('id' => $_POST['id']));
    }

}

class Blog_controller extends CI_Controller {

    function blog()
    {
        $this->load->model('Blog');

        $data['query'] = $this->Blog->get_last_ten_entries();

        $this->load->view('blog', $data);
    }
}

Please check CodeIgniter user guide for model. The example of Blog controller and Blog model will answer you.

class Blogmodel extends CI_Model {

    var $title   = '';
    var $content = '';
    var $date    = '';

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    function get_last_ten_entries()
    {
        $query = $this->db->get('entries', 10);
        return $query->result();
    }

    function insert_entry()
    {
        $this->title   = $_POST['title']; // please read the below note
        $this->content = $_POST['content'];
        $this->date    = time();

        $this->db->insert('entries', $this);
    }

    function update_entry()
    {
        $this->title   = $_POST['title'];
        $this->content = $_POST['content'];
        $this->date    = time();

        $this->db->update('entries', $this, array('id' => $_POST['id']));
    }

}

class Blog_controller extends CI_Controller {

    function blog()
    {
        $this->load->model('Blog');

        $data['query'] = $this->Blog->get_last_ten_entries();

        $this->load->view('blog', $data);
    }
}
横笛休吹塞上声 2024-11-20 09:28:57

您首先需要创建一个模型。

class Membership_model extends CI_Model {
    function __construct() {
        parent::__construct();
    }

    function getUser($username) {
        $query = $this->db->get_where('membership', array('username' => $username));
        return $query->result_array();
    }
}

然后在您的控制器中,使用成员资格模型来检索数据。调用此函数: $this->membership_model->getUser($this->input->post('username'));

You first need to create a model.

class Membership_model extends CI_Model {
    function __construct() {
        parent::__construct();
    }

    function getUser($username) {
        $query = $this->db->get_where('membership', array('username' => $username));
        return $query->result_array();
    }
}

then in your controller, use the membership model to retrieve data. Calling this function: $this->membership_model->getUser($this->input->post('username'));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文