php 使用代码点火器将 sql 查询结果从控制器传递到视图中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4915581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
passing sql query results from controller into view with code igniter
提问by Bill
So I'm having this problem, it should be pretty simple, but I don't know why I can't figure it out. I"m new to the whole idea of MVC, and I'm trying to pass a database query from my controller into a view and display the results in the view. The way I'm doing it now says "undefined variable, sql" when I load the view. This is what I have:
所以我遇到了这个问题,应该很简单,但我不知道为什么我想不通。我对 MVC 的整个想法很陌生,我正在尝试将数据库查询从我的控制器传递到视图中并在视图中显示结果。我现在的做法是“未定义的变量,sql”当我加载视图时。这就是我所拥有的:
CONTROLLER
控制器
function make_login()
{
//Select list of departments for dropdown
$this->load->database();
$sql = $this->db->query('SELECT departmentName FROM department ORDER BY departmentName ASC');
$this->load->view('siteheader.php');
$this->load->view('makelogin.php', $sql->result_array());
$this->load->view('sitefooter.php');
}
VIEW
看法
<?php
foreach($sql->result_array() as $row)
{
echo $row['departmentName'];
}
?>
(If I just echo it out in the controller, it displays the results)
(如果我只是在控制器中回显它,它会显示结果)
Any help would be awesome... THANKS!
任何帮助都会很棒......谢谢!
回答by Ross
few tips ~
小贴士~
your make_login should be in a model. your controller will look somethinglike this:
您的 make_login 应该在模型中。控制器将寻找的东西是这样的:
function make_login
{
$this->load->model('login_model'); // whatever you call it
$data['departments'] = $this->login_model->get_departments();
/* note - you don't need to have the extension when it's a php file */
$this->load->view('siteheader');
$this->load->view('makelogin', $data);
$this->load->view('sitefooter');
}
now in your model, have something like:
现在在你的模型中,有类似的东西:
function get_departments()
{
$sql = $this->db->query('SELECT departmentName FROM department ORDER BY departmentName ASC');
return $sql->result();
/* you simply return the results as an object
* also note you can use the ActiveRecord class for this...might make it easier
*/
}
and finally, your view:
最后,你的观点:
<?php
foreach($departments as $store)
{
echo $store->name . '<br />'; // your fields/whatever you want to output.
}
?>
回答by Peter
The SQL query should be done in the model.
SQL 查询应该在模型中完成。
Cheap mnemonic device: the D in model = database.
便宜的助记符:模型中的 D = 数据库。
In the Controller, you assign part of the $data array to the results of the query:
在 Controller 中,您将 $data 数组的一部分分配给查询结果:
$this->load->model('blog_model');
$data['posts'] = $this->blog_model->getPosts();
// Load the view with the data
$this->load->view('blog', $data);
In the Model, you do the actual query:
在模型中,您执行实际查询:
public function getPosts()
{
// Method chaining supported in PHP 5
$this->db->select('*')->from('posts');
// Assign the query object to a variable
$query = $this->db->get();
// We don't want to return an empty result, so let's handle that error somehow
if (!$query->num_rows() > 0) {
die("There are no posts in the database.");
}
// Make a new array for the posts
$posts = array();
// For the purposes of this example, we'll only return the title
foreach ($query->result() as $row) {
$posts[$row->id] = $row->title;
}
// Pass the result back to the controller
return $posts;
}
Now, in the view, each element of $data will be its own variable:
现在,在视图中, $data 的每个元素都将是它自己的变量:
<div class="post">
<?php foreach ($posts as $id => $title) : ?>
<h1 class="post-title"><?php echo $title; ?> (ID: <?php echo $id; ?>)</h1>
<p class="post-content">
.......
</p>
<?php endforeach; ?>
</div>
That's it!
就是这样!
回答by Michael Ozeryansky
It seems that either CI is confusing you or you are also new to PHP. They are just functions so you can't pass variables like that.
似乎 CI 使您感到困惑,或者您也是 PHP 的新手。它们只是函数,所以你不能像那样传递变量。
When passing an associative array it will take the key and make that into a variable with the value in the array, using native PHP functions. What Ross said is exactly what you are supposed to do.
传递关联数组时,它将使用本机 PHP 函数获取键并将其转换为具有数组中值的变量。罗斯说的正是你应该做的。
Model: all database stuff Controller: uses models to pass variables to views View: outputs the variables (a view should never have any sql in it)
模型:所有数据库的东西控制器:使用模型将变量传递给视图视图:输出变量(视图不应该在其中包含任何 sql)
Also note that result and result_array have the same data but result returns objects and result_array returns associative arrays.
另请注意, result 和 result_array 具有相同的数据,但 result 返回对象, result_array 返回关联数组。
回答by iBiryukov
foreach($array as $row)
{
echo $row['departmentName'];
}
?>
You need to pass the $array in. I am not sure what ->load->view() does and how it treats the incoming parameters.
您需要传入 $array 。我不确定 ->load->view() 的作用以及它如何处理传入的参数。