如何使用模型检索数据并将其传递到使用 CodeIgniter 的控制器中使用?
从用户登录表单中,我需要使用他们的用户名 ($this->input->post('username'))
来查询我的membership
表数据库,以便我可以检索用户 firstname
和 lastname
,我认为这应该通过从我的控制器调用的模型来完成。然后,我需要将该数据传递回我的控制器,以使用 firstname
和 lastname
添加到我的会话 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的模型中,创建一个函数来检索用户的名字和姓氏:
在您的控制器中,就像您所做的那样,抓取控制器中的 POST 内容:
然后,从您的控制器中调用您的模型函数,但保存输出该模型函数在变量中:
之后,您可以做您想做的事情:
希望有帮助。这是我凭空写下的,但它应该有效。我还建议您仔细阅读 CI 文档,因为它确实是我见过的关于框架的最佳文档之一。祝你好运。
In your model, make a function to retrieve the first and last name of a user:
In your controller, like you were doijng, grab the POST contents in your controller:
Then, from your controller, call your model function but save the output of that model function in a variable:
After that, you can do what you were trying to do:
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.
请查看 型号的 CodeIgniter 用户指南。
Blog
控制器和Blog
模型的示例将为您解答。Please check CodeIgniter user guide for model. The example of
Blog
controller andBlog
model will answer you.您首先需要创建一个模型。
然后在您的控制器中,使用成员资格模型来检索数据。调用此函数:
$this->membership_model->getUser($this->input->post('username'));
You first need to create a model.
then in your controller, use the membership model to retrieve data. Calling this function:
$this->membership_model->getUser($this->input->post('username'));