从对象 MongoDB 和 PHP (fuelphp) 中提取数据
我正在使用 MongoDB 和 FuelPHP。我成功连接到MongoDB并可以提取数据。我已经花了三天时间试图弄清楚如何将数据注入视图中。 我的观点是这样的:
<body>
<div class="middle">
<p><?php if (isset($_id)){echo $_id;} ?></p>
<p><?php if (isset($first_name)){echo $first_name;} ?></p>
<p><?php if (isset($last_name)){echo $last_name;} ?></p>
</div>
</body>
我的控制器是:
class Controller_Home extends Controller {
public function action_index()
{
$data['css'] = Asset::css(array('reset.css','main.css'));
$results = Model_Home::get_results();
//I thought all I have to do is this foreach loop and it would work
foreach($results as $row => $val){
$data = $val;
}
$this->response->body = View::factory('home/index', $data);
}
}
我的 var_dump() 是:
object(stdClass)#10 (2) {
[0]=>
array(5) {
["_id"]=>
object(MongoId)#13 (1) {
["$id"]=>
string(24) "4ef82a27b238f02ed9000000"
}
["cms"]=>
array(1) {
[0]=>
string(8) "Druapl_1"
}
["first_name"]=>
string(6) "Name_1"
["last_name"]=>
string(10) "Lst_Name_1"
["skills"]=>
array(3) {
[0]=>
string(6) "html_1"
[1]=>
string(5) "css_1"
[2]=>
string(8) "jQuery_1"
}
}
[1]=>
array(5) {
["_id"]=>
object(MongoId)#14 (1) {
["$id"]=>
string(24) "4ef81a0dcf163c7da3e5c964"
}
["cms"]=>
array(1) {
[0]=>
string(8) "Druapl_2"
}
["first_name"]=>
string(6) "Name_2"
["last_name"]=>
string(10) "Lst_Name_2"
["skills"]=>
array(3) {
[0]=>
string(6) "html_2"
[1]=>
string(5) "css_2"
[2]=>
string(8) "jQuery_2"
}
}
}
现在,它确实有效,但由于某种原因我只看到我视图中的第一项:
4ef81a0dcf163c7da3e5c964
Name_2
Lst_Name_2
html_2
css_2
jQuery_2
Druapl_2
我认为我的老对手 foreach 循环中出现了严重错误...... 请帮忙,这肯定会帮助我提高对对象、foreach 循环和框架的理解。谢谢。
I am using MongoDB and FuelPHP. I successfully connected to MongoDB and can extract data. I already spent three days trying to figure out how to inject data into view.
My view is such:
<body>
<div class="middle">
<p><?php if (isset($_id)){echo $_id;} ?></p>
<p><?php if (isset($first_name)){echo $first_name;} ?></p>
<p><?php if (isset($last_name)){echo $last_name;} ?></p>
</div>
</body>
My controller is:
class Controller_Home extends Controller {
public function action_index()
{
$data['css'] = Asset::css(array('reset.css','main.css'));
$results = Model_Home::get_results();
//I thought all I have to do is this foreach loop and it would work
foreach($results as $row => $val){
$data = $val;
}
$this->response->body = View::factory('home/index', $data);
}
}
My var_dump() is:
object(stdClass)#10 (2) {
[0]=>
array(5) {
["_id"]=>
object(MongoId)#13 (1) {
["$id"]=>
string(24) "4ef82a27b238f02ed9000000"
}
["cms"]=>
array(1) {
[0]=>
string(8) "Druapl_1"
}
["first_name"]=>
string(6) "Name_1"
["last_name"]=>
string(10) "Lst_Name_1"
["skills"]=>
array(3) {
[0]=>
string(6) "html_1"
[1]=>
string(5) "css_1"
[2]=>
string(8) "jQuery_1"
}
}
[1]=>
array(5) {
["_id"]=>
object(MongoId)#14 (1) {
["$id"]=>
string(24) "4ef81a0dcf163c7da3e5c964"
}
["cms"]=>
array(1) {
[0]=>
string(8) "Druapl_2"
}
["first_name"]=>
string(6) "Name_2"
["last_name"]=>
string(10) "Lst_Name_2"
["skills"]=>
array(3) {
[0]=>
string(6) "html_2"
[1]=>
string(5) "css_2"
[2]=>
string(8) "jQuery_2"
}
}
}
Now, it does work, but I only see the first item in my view for some reason:
4ef81a0dcf163c7da3e5c964
Name_2
Lst_Name_2
html_2
css_2
jQuery_2
Druapl_2
I think something goes terribly wrong in my old nemesis foreach loop...
Please help, this will certainly will help me improve my understanding of objects, foreach loops and frameworks in general. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
也可以将完整的对象传递给模板,而不是先将其转换为数组。
Try this:
It is also possible to pass complete objects to the template instead of converting it to an array first.
我不熟悉 FuelPHP,但似乎您的 $data 变量格式不正确。你有:
很可能你需要它(每次抓取新行时你都会覆盖这些值):
然后在你的视图中你应该有类似的东西(你没有迭代你的结果):
但是这太复杂了因为您要迭代数据两次。您更好的方法可能如下:
然后在您看来执行以下操作:
编辑:
附加信息:(假设FuelPHP类似于CodeIgniter,它看起来是)
此答案所基于的 传递到视图变为:
$some_item (相当于 some_val)和 $some_item2 (相当于 some_val2)
I'm not familiar with FuelPHP but it appears as though your $data variable is not formatted correctly. You have:
Most likely you need it to be (You were overwriting the values each time you grabbed a new row):
Then on your view you should have something like (You weren't iterating through your results):
However this is overly complicated in that you're iterating through the data twice. You're better approach would probably be the following:
Then in your view do the following:
EDIT:
Additional Information on which this answer is based: (Assuming FuelPHP is like CodeIgniter which it appears to be)
when passed to the view becomes:
$some_item (which equates to some_val) and $some_item2 (which equates to some_val2)
我不知道 php,但在我看来,您正在迭代结果对象中的所有键、值对:
为什么不直接说
然后从视图中的数据对象中获取您想要的东西:
再次,我有不知道这是否是您在 php 中执行此操作的方式,但想法是您想要循环结果,然后访问每个对象的属性,而不是迭代属性。
更新:修复了一些代码,我假设燃料以某种方式将数据等局部变量传递到视图,但也许没有。
-泰勒
I don't know php but it seems to me that you are iterating over all the key, value pairs in the result objects:
Why not just say
Then get the things you want out of the data object in the view:
Again, I have no idea if thats how you do it in php but the idea is that you want to loop over results and then access attributes of each object, not iterate over the attributes.
Updated: fixed some of the code, I'm assuming fuel somehow passes local variables like data to the view but maybe it doesn't.
-Tyler