CodeIgniter 将带有查询的数组传递到视图中
您好,我在获取视图以显示以下代码的任何结果时遇到问题。
控制器:
$datedue = 2011-01-27;
$username = $this->tank_auth->get_username();
$sql = "SELECT taskid FROM relationships WHERE username = ? AND datedue = ?";
$tasks = $this->db->query($sql, array($username, $datedue));
$count = 0;
$taskdetails = array();
foreach($tasks->result() as $row)
{
$taskid = $row->taskid;
$subsql = "SELECT * FROM tasks WHERE id = ?";
$taskdetails[$count] = $this->db->query($subsql, array($taskid));
$count++;
}
$data['taskdetails'] = $taskdetails;
$data['total'] = $count;
$this->header();
$this->load->view('dashboard', $data);
视图:
<?php
foreach($taskdetails as $entry)
{
foreach($entry->result() as $row)
{
echo $row->name;
}
}
?>
任何帮助都会很好,谢谢。
Hi I am having an issue getting the view to show any results for the following code.
Controller:
$datedue = 2011-01-27;
$username = $this->tank_auth->get_username();
$sql = "SELECT taskid FROM relationships WHERE username = ? AND datedue = ?";
$tasks = $this->db->query($sql, array($username, $datedue));
$count = 0;
$taskdetails = array();
foreach($tasks->result() as $row)
{
$taskid = $row->taskid;
$subsql = "SELECT * FROM tasks WHERE id = ?";
$taskdetails[$count] = $this->db->query($subsql, array($taskid));
$count++;
}
$data['taskdetails'] = $taskdetails;
$data['total'] = $count;
$this->header();
$this->load->view('dashboard', $data);
View:
<?php
foreach($taskdetails as $entry)
{
foreach($entry->result() as $row)
{
echo $row->name;
}
}
?>
Any help would be nice thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的视图未显示某些内容的原因是您的查询不完全正确。
应该用引号引起来,因为日期是一个字符串。
另外,您没有正确遵循 MVC 的概念。所有数据库查询和结果都应该发生在模型内部。以及控制器内部的所有数据处理。
控制器和视图都不应处理任何数据库连接,这是模型的职责。因此,我建议创建一个模型并将所有数据库查询放入一个或两个函数中。然后控制器将调用这些函数并接收返回的数据。然后它应该操纵它并将其以干净的方式传递给视图(我的意思是,视图永远不应该调用
result()
以下是代码的结构:
控制器
型号
视图:
The reason why your view is not displaying something is because you're query is not completely correct.
should be enclosed in quotes, since the date is a string.
Also, you're not correctly following the concept of MVC. All the database querying and results should occur inside of the Model. And all of the data handling inside the Controller.
The Controller nor the View should handle any of the database connections, that is the duty of the Model. Therefore I would advise to create a Model and put all of the Database querying in one or two functions. The Controller would then call these functions and receive the data back. Then it should manipulate it and pass it to the View in a clean matter (by this I mean, that the View should NEVER call
result()
Here is how you're code should be structured:
CONTROLLER
MODEL
VIEW:
验证您的任务表包含名称字段
查看
为什么您要在此行中传递数组
而是写
verify your task table contain the name field
view
why you are passing array in this line
instead write