【发布时间】:2011-12-27 20:35:41
【问题描述】:
我正在使用 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 循环和一般框架的理解。谢谢。
【问题讨论】:
-
我们需要更多信息。请通过 db.collection.findOne() 向我们展示集合中的完整文档以及通过 github 提供的完整代码,以便我们尝试重现。