Question

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.

Was it helpful?

Solution

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)

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top