1

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

2
  • use $properties instead of $resultSet Commented Nov 8, 2012 at 6:33
  • put it as an answer @ShayanHusaini so I can mark it as one :) Commented Nov 8, 2012 at 6:36

3 Answers 3

3

use $properties instead of $resultSet

Sign up to request clarification or add additional context in comments.

Comments

2

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;
       }
    ?>   

Comments

0

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'];
}
?> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.