从对象 MongoDB 和 PHP (fuelphp) 中提取数据

发布于 2024-12-22 20:34:19 字数 2345 浏览 5 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

莫言歌 2024-12-29 20:34:19

试试这个:

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

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

Try this:

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

It is also possible to pass complete objects to the template instead of converting it to an array first.

青芜 2024-12-29 20:34:19

我不熟悉 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)

I'm not familiar with FuelPHP but it appears as though your $data variable is not formatted correctly. You have:

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

Most likely you need it to be (You were overwriting the values each time you grabbed a new row):

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

Then on your view you should have something like (You weren't iterating through your 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>

However this is overly complicated in that you're iterating through the data twice. You're better approach would probably be the following:

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

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

Then in your view do the following:

<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>

EDIT:

Additional Information on which this answer is based: (Assuming FuelPHP is like CodeIgniter which it appears to be)

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

when passed to the view becomes:

$some_item (which equates to some_val) and $some_item2 (which equates to some_val2)

白衬杉格子梦 2024-12-29 20:34:19

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

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

为什么不直接说

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

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

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

再次,我有不知道这是否是您在 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:

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

Why not just say

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

Then get the things you want out of the data object in the view:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文