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.
I’m not familiar with FuelPHP but it appears as though your $data variable is not formatted correctly. You have:
Most likely you need it to be (You were overwriting the values each time you grabbed a new row):
Then on your view you should have something like (You weren’t iterating through your results):
However this is overly complicated in that you’re iterating through the data twice. You’re better approach would probably be the following:
Then in your view do the following:
EDIT:
Additional Information on which this answer is based: (Assuming FuelPHP is like CodeIgniter which it appears to be)
when passed to the view becomes:
$some_item (which equates to some_val) and $some_item2 (which equates to some_val2)