1

我正在使用 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 循环和框架的理解。谢谢你。

4

3 回答 3

2

试试这个:

foreach($results as $row => $val)
{
    $data[$row] = $val;
}

也可以将完整的对象传递给模板,而不是先将其转换为数组。

于 2011-12-28T08:18:23.620 回答
1

我不知道 php,但在我看来,您正在遍历结果对象中的所有键、值对:

foreach($results as $row => $val)
{
    $data = $val;
}

为什么不直接说

foreach($results as $obj)
{
    $data = $obj
}

然后从视图中的数据对象中取出你想要的东西:

<?php if (isset($data['_id'])){echo $data['_id'];} ?>

同样,我不知道你是否在 php 中这样做,但想法是你想要循环结果,然后访问每个对象的属性,而不是遍历属性。

更新:修复了一些代码,我假设燃料以某种方式将数据等局部变量传递给视图,但也许它没有。

-泰勒

于 2011-12-27T21:24:32.250 回答
1

我对 FuelPHP 不熟悉,但看起来您的 $data 变量格式不正确。你有:

foreach($results as $row => $val){
    $data = $val;
}

很可能您需要它(每次抓取新行时都会覆盖这些值):

foreach($results as $row => $val){
    $data['results'][$row] = $val;
}

然后在你看来,你应该有类似的东西(你没有遍历你的结果):

<body>
<div class="middle">
<?php foreach($results as $result): ?>
    <p><?php if (isset($result['_id'])){echo $result['_id'];} ?></p>
    <p><?php if (isset($result['first_name'])){echo $result['first_name'];} ?></p>
    <p><?php if (isset($result['last_name'])){echo $result['last_name'];} ?></p>
<?php endforeach; ?>
</div>
</body>

但是,这过于复杂,因为您要对数据进行两次迭代。您更好的方法可能如下:

//Replace this:
foreach($results as $row => $val){
    $data['results'][$row] = $val;
}

//with this:
$data['results'] = $results;

然后在您看来,请执行以下操作:

<body>
<div class="middle">
<?php foreach($results as $result): ?>
    <p><?php if (isset($result['_id'])){echo $result['_id'];} ?></p>
    <p><?php if (isset($result['first_name'])){echo $result['first_name'];} ?></p>
    <p><?php if (isset($result['last_name'])){echo $result['last_name'];} ?></p>
<?php endforeach; ?>
</div>
</body>

编辑:

此答案所基于的附加信息:(假设 FuelPHP 看起来像 CodeIgniter)

$data['some_item'] = some_val;
$data['some_item2'] = some_val2;

当传递给视图时变为:

$some_item(相当于 some_val)和 $some_item2(相当于 some_val2)

于 2012-01-03T21:48:06.560 回答