Shantanu
Shantanu

Reputation: 148

Fetching value from a query result in the controller

I have a model, where I am running a query and selecting table field from my database, the result of this query is received in the controller. I am trying to extract the type_id, which is an id from this result and pass it to another function in the same model, this function than selects image paths from a database table against this type_id. But I am getting an error "Trying to get property of non-object".

Model

class Interior_listing_model extends CI_model
{
    public function interior_list()
  {
    //$query = $this->db->get('interior_testing');
    $query['resulty'] = $this->db->get_where('interior_testing', array('category_id' => 1),0,0);
    return $query['resulty']->result_array();

  }
public function image_list($type_id)
  {
$query1 = $this->db->get_where('interior_image_testing', array('type_id' => $type_id),3,0);
   return $query1->get()->row();
}

}

Controller

public function index()
    {   
        $this->load->model("Interior_listing_model","interior");
        $data['articles']  = $this->interior->interior_list();
        $data['particles'] = $this->interior->image_list($data['articles']->type_id);
        // Load Interior Listing View
        $this->load->view("interior/interior",$data);
    }

In this controller at $data['articles']->type_id I am getting the error, mentioned above.

View

     <?php foreach($articles as $article): ?>
      span class="text-center"><?= $article->type ?></span><br/>
     <?php foreach ($particles as $particle): 
                    ?>
                  <div class="col-lg-4 col-md-4 col-sm-4 col-6 workimg">
                    <img src="assets/img/<?= $particle->image_path ?> " width="100%"> <!-- needs to be changed -->
                  </div> 
 <?php endforeach; ?>
 <?php endforeach; ?> 

Upvotes: 0

Views: 609

Answers (2)

jhilgeman
jhilgeman

Reputation: 1583

The "return" statement on interior_list() is returning an array:

return $query['resulty']->result_array(); 

result_array() is a CI function that returns an array of rows, where each row is one record from the result.

So You're trying to access an object property called type_id but you're dealing with an array, not an object.

Since $data['articles'] could have two or more rows in it, you need to figure out what the expected result will be here.

For example, maybe you're looking for something like this:

$data['articles'] = $this->interior->interior_list();
foreach($data['articles'] as $idx => $article)
{
  $data['particles'] = array_merge($data['particles'], $this->interior->image_list($article["type_id"]));
}

Upvotes: 0

DFriend
DFriend

Reputation: 8964

At least two changes are needed.

In your model change

return $query['resulty']->result_array();

to

return $query['resulty']->result();

result() returns an array of objects where each object is a row from the query.

Because $data['articles'] is an array of objects you need to reference which item in the array to use.

Change

$data['particles'] = $this->interior->image_list($data['articles']->type_id);

to

$data['particles'] = $this->interior->image_list($data['articles'][0]->type_id);

This will only use the first row of the model results. I suspect this won't be achieving everything you want. But it should stop the error.

Upvotes: 1

Related Questions