php CodeIgniter get_where
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1577365/
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
CodeIgniter get_where
提问by doubleplusgood
I'm attempting to use get_where to grab a list of all database records where the owner is equal to the logged in user.
我正在尝试使用 get_where 来获取所有者等于登录用户的所有数据库记录的列表。
This is my function in my controller;
这是我在控制器中的功能;
function files()
{
$owner = $this->auth->get_user();
$this->db->get_where('files', array('owner =' => '$owner'))->result();
}
And in my view I have the following;
在我看来,我有以下几点;
<?php foreach($query->result() as $row): ?>
<span><?=$row->name?></span>
<?php endforeach; ?>
When I try accessing the view, I get the error :
当我尝试访问视图时,出现错误:
Fatal error: Call to a member function result() on a non-object in /views/account/files.php on line 1.
致命错误:在第 1 行的 /views/account/files.php 中的非对象上调用成员函数 result()。
Wondered if anyone had any ideas of what might be up with this?
想知道是否有人对此可能有任何想法?
Thanks
谢谢
回答by KushalP
CodeIgniter is a framework based on MVCprinciples. As a result, you would usually separate application logic, data abstraction and "output" into their respective areas for CodeIgniter use. In this case: controllers, models and views.
CodeIgniter 是一个基于MVC原则的框架。因此,您通常会将应用程序逻辑、数据抽象和“输出”分离到各自的区域以供 CodeIgniter 使用。在这种情况下:控制器、模型和视图。
Just for reference, you should usually have you "data" code as a model function, in this case the get_where functionality. I highly suggest you read through the provided User Guideto get to grips with CodeIgniter, it should hold your hand through most steps. See: Table of Contents (top right).
仅供参考,您通常应该将“数据”代码作为模型函数,在本例中为 get_where 功能。我强烈建议您通读提供的用户指南以掌握 CodeIgniter,它应该可以帮助您完成大多数步骤。请参阅:目录(右上角)。
TL;DR
TL; 博士
To solve your problem you need to make sure that you pass controller variables through to your view:
要解决您的问题,您需要确保将控制器变量传递给您的视图:
function files()
{
$owner = $this->auth->get_user();
$data['files'] = $this->db->get_where('files', array('owner =' => '$owner'))->result();
$this->load->view('name_of_my_view', $data);
}
And then make sure to use the correct variable in your view:
然后确保在您的视图中使用正确的变量:
<?php foreach($files as $row): ?>
<span><?=$row['name']; ?></span>
<?php endforeach; ?>
回答by Teej
<?php foreach($query->result() as $row): ?>
<span><?=$row->name?></span>
<?php endforeach; ?>
Remove the result function like so.
像这样删除结果函数。
<?php foreach($query as $row): ?>
<span><?=$row->name?></span>
<?php endforeach; ?>
Btw. It's a much better idea to test the query for a result before you return it.
顺便提一句。在返回结果之前测试查询是一个更好的主意。
function files()
{
$owner = $this->auth->get_user();
$query = $this->db->get_where('files', array('owner =' => $owner))->result();
if ($query->num_rows() > 0)
{
return $query->result();
}
return FALSE;
}
回答by Saddam Khan
public function get_records(){
return $this->db->get_where('table_name', array('column_name' => value))->result();
}
This is how you can return data from database using get_where() method.
这是使用 get_where() 方法从数据库返回数据的方法。
回答by mickmackusa
- All querying should be performed in the Model.
- Processing logic in the View should be kept to an absolute minimum. If you need to use some basic looping or conditionals, okay, but nearly all data preparation should be done before the View.
- By single quoting your
$ownervariable, you convert it to a literal string -- in other words, it is rendered as a dollar sign followed by five letters which is certainly not what you want. - The default comparison of codeigniter's
wheremethods is=, so you don't need to declare the equals sign. - I don't know which Auth library you are using, so I'll go out on a limb and assume that
get_user()returns an object -- of which you wish to access the id of the current user. This will require->idchained to the end of the method call to access the id property.
- 所有查询都应在模型中执行。
- 视图中的处理逻辑应保持在绝对最小值。如果您需要使用一些基本的循环或条件,可以,但是几乎所有的数据准备都应该在 View 之前完成。
- 通过单引号您的
$owner变量,您将其转换为文字字符串——换句话说,它呈现为一个美元符号,后跟五个字母,这当然不是您想要的。 - codeigniter 的
where方法的默认比较是=,所以你不需要声明等号。 - 我不知道您使用的是哪个 Auth 库,所以我会假设
get_user()返回一个对象 - 您希望访问当前用户的 ID。这将需要->id链接到方法调用的末尾以访问 id 属性。
Now, let's re-script your MVC architecture.
现在,让我们重新编写 MVC 架构的脚本。
The story starts in the controller. You aren't passing any data in, so its duties are:
故事从控制器开始。您没有传入任何数据,因此其职责是:
- Load the model (if it isn't already loaded)
- Call the model method and pass the owner id as a parameter.
- Load the view and pass the model's returned result set as a parameter.
- 加载模型(如果尚未加载)
- 调用模型方法并将所有者 id 作为参数传递。
- 加载视图并将模型返回的结果集作为参数传递。
*Notice that there is no querying and no displaying of content.
*请注意,没有查询和显示内容。
Controller:(no single-use variables)
控制器:(无一次性变量)
public function files() {
$this->load->model('Files_model');
$this->load->view(
'user_files',
['files' => $this->Files_model->Files($this->auth->get_user()->id)]
);
}
Alternatively, you can write your controller withsingle-use variables if you prefer the declarative benefits / readability.
或者,如果您更喜欢声明性的好处/可读性,您可以使用一次性变量编写控制器。
public function files() {
$this->load->model('Files_model');
$userId = $this->auth->get_user()->id;
$data['files'] = $this->Files_model->Files($userId);
$this->load->view('user_files', $data);
}
Model:(parameters are passed-in, result sets are returned)
模型:(传入参数,返回结果集)
public function Files($userId) {
return $this->db->get_where('files', ['owner' => $userId])->result();
}
In the above snippet, the generated query will be:
在上面的代码段中,生成的查询将是:
SELECT * FROM files WHERE owner = $userId
The result set (assuming the query suits the db table schema) will be an empty array if no qualifying results or an indexed array of objects. Either way, the return value will be an array.
如果没有符合条件的结果或对象的索引数组,则结果集(假设查询适合 db 表架构)将是一个空数组。无论哪种方式,返回值都将是一个数组。
In the final step, the view will receive the populated result set as $files(the variable is named by the associative first-level key that was declared in the view loading method).
在最后一步,视图将接收填充的结果集$files(该变量由视图加载方法中声明的关联第一级键命名)。
View:
看法:
<?php
foreach ($files as $file) {
echo "<span>{$file->name}</span>";
}
The {and }are not essential, I just prefer it for readability in my IDE.
该{和}是不是必需的,我只是喜欢它的可读性,在我的IDE。
To sum it all up, the data flows like this:
总而言之,数据流如下:
Controller -> Model -> Controller -> View
控制器 -> 模型 -> 控制器 -> 视图
Only the model does database interactions. Only the view prints to screen.
只有模型进行数据库交互。只有视图打印到屏幕上。

