[英]CodeIgniter load two controller functions in the view
I'm building a simple ToDo list in CodeIgniter as I just learn it. 我刚刚学习时,就在CodeIgniter中构建了一个简单的ToDo列表。
I have a profile page which should show the user details + all the items he checked to do. 我有一个个人资料页面,该页面应显示用户详细信息以及他检查过的所有项目。
This is my view so far 到目前为止,这是我的看法
<div class="profile_header">
<?php foreach ($users as $user) { ?>
<h4><?php echo $user['name']; ?></h4>
</div>
This is my controller 这是我的控制器
public function index(){
$this->load->model('model_users');
$query = $this->model_users->getUserInfo();
$data['users'] = null;
if($query){
$data['users'] = $query;
}
$this->load->view('profile/profile', $data);
}
And my model 还有我的模特
public function getUserInfo(){
$user_email = $this->session->userdata('email');
$query = $this->db->get_where('users', array('email' => $user_email));
return $query->result_array();
}
Now you can see that I select the user information from the user table and send it to the view using the index controller function. 现在您可以看到我从用户表中选择了用户信息,并使用索引控制器功能将其发送到视图。 Now if I want to get all the
tasks
from list_task
table and show it on page, I assume I have to write a new model function and call it in my controller in a new function like getUserTasks
but how do I send them over to the view? 现在,如果我想从
list_task
表中获取所有tasks
并将其显示在页面上,我假设我必须编写一个新的模型函数,并在控制器中使用诸如getUserTasks
类的新函数调用它,但是如何将它们发送到视图中?
Firstly, I strongly recommend you create 2 model which names are users and tasks. 首先,我强烈建议您创建2个模型,名称分别是用户和任务。
In user model, just write codes about the users and your users table. 在用户模型中,只需编写有关用户和用户表的代码。
<?php
class User_model extends CI_Model {
function __construct() {
parent::__construct();
}
function getUserInfo(){ /*Fetch your users data*/ }
}
?>
In your task model, just write codes about the tasks and your tasks table. 在任务模型中,只需编写有关任务和任务表的代码。
<?php
class Tasks_model extends CI_Model {
function __construct() {
parent::__construct();
}
function getTasks($user){ /*Fetch tasks of user which specified by $user */ }
}
?>
Finally, using these functions of 2 models, fill your all data in controller and send it to view. 最后,使用2个模型的这些功能,将所有数据填充到控制器中并将其发送给视图。
public function index(){
$this->load->model('User_model');
$this->load->model('Tasks_model');
$queryOfUsers = $this->user_model->getUserInfo();
$queryOfTasks = $this->tasks_model->getTasks();
//manage/edit your data comes from database as you did above.
//put it in data and send to view.
$this->load->view('profile/profile', $data);
}
Codeigniter and other frameworks exist for easy coding and debugging. 存在Codeigniter和其他框架以简化编码和调试。 If you combine all functions in a model, you can not trace them easily and end of the project, you will be very far from the Object Oriented.
如果将所有功能组合到模型中,则无法轻松跟踪它们并在项目结束时,您将与面向对象相距甚远。
You'd just add another element to your $data
array. 您只需在
$data
数组中添加另一个元素。
So your controller would look something like this: 因此,您的控制器将如下所示:
public function index(){
$this->load->model('model_users');
$query = $this->model_users->getUserInfo();
$data['users'] = null;
if($query){
$data['users'] = $query;
}
// Load tasks (create a new method in your model)
$data['tasks'] = $this->model_users->getUserTasks();
$this->load->view('profile/profile', $data);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.