1
votes

Simple question but I don't know why it is not printing correctly or working as expected. I have this in model (works because when I print_r($result) it shows data:

function get_latest_entries()
{
    $query = $this->db->get('property');
    return $query->result_array();
}

And for controller:

public function index()
{
    $resultSet['properties'] = $this->propertymodel->get_latest_entries();
    $this->load->view('home', $resultSet);
}

In the view I want to iterate over the array (the table has description, city, address columns):

<?php
foreach($resultSet as $item)
{
    echo $item->city;
    echo $item->description;
}
?>   

I am getting two records on home page where am displaying the results as above:

Severity: Notice Message: Undefined variable: resultSet Filename: views/home.php Line Number: 16

And

Severity: Warning Message: Invalid argument supplied for foreach() Filename: views/home.php Line Number: 16

3
use $properties instead of $resultSet - Code Prank
put it as an answer @ShayanHusaini so I can mark it as one :) - sys_debug

3 Answers

3
votes

use $properties instead of $resultSet

2
votes

use this... you are passing $properties to your view and not $resultSet..

<?php
       foreach($properties as $item) // use properties
       {
        echo $item->city;
        echo $item->description;
       }
    ?>   
0
votes

Here is the problem in your code. -you have returned data as array of arrays and you are trying to access as an object, -and the second one is,you have passed properties variable to view and you are accessing resultSet. -so here is your code in view which contains errors

<?php
foreach($resultSet as $item)
{
    echo $item->city;
    echo $item->description;
}
?> 

-And the Correct version of your code is here...

<?php
foreach($properties as $item)
{
    echo $item['city'];
    echo $item['description'];
}
?>