Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7001029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:45:01+00:00 2026-05-27T20:45:01+00:00

I am using MongoDB and FuelPHP. I successfully connected to MongoDB and can extract

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:45:01+00:00Added an answer on May 27, 2026 at 8:45 pm

    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using C# .NET 3.5 and WCF, I'm trying to write out some of the
Using mongodb in grails i am trying to get a hold of the database
Using mongodb I'm trying to remove a property (_id) from a child collection (ListingFeatures)
I am using mongodb and I have changed the dbpath. Can I just copy
I have inserted and fetched data using MongoDB, in PHP. Is there an actual
I'm using MongoDB with Mongoid and trying to put in place a rudimentary search
I am using MongoDB with C# driver. I managed to add/delete/update data from collections,
Using MongoDB I'm trying to copy a database from one server to another. My
I'm using MongoDB and MongoID in a rails app, how can some models be
I am using MongoDB at work these days. So far, I find it a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.