Reputation: 9987
I have created this following model (see below). I have managed to fetch data from my database and send generated result to the view by creating a variable called $data['get_company']
. However, I can't figure out how I can use these results from the model in my Controller without using a foreach loop.
Model
class Company_model extends CI_Model
{
public function fetch_company($id = NULL) {
$this->db->select('*');
$this->db->where('customer_company_profiles_id', $id);
$this->db->from('customer_company_profiles');
$this->db->order_by('customer_company_profiles_id', 'desc');
$this->db->limit(1);
$query = $this->db->get();
if($query->num_rows() == NULL)
{
return false;
}
else
{
return $query->result();
}
}
}
Controller
public function index($id = NULL)
{
$id = $this->input->get('id');
$data['get_company'] = $this->Company_model->fetch_company($id);
$this->load->view('includes/header');
$this->load->view('company/company_index', $data);
$this->load->view('includes/footer');
}
Is there a function like $something_something->get_company->get_this_row('ID')
so that I can avoid using foreach loops inside a controller and how do I proceed?
Upvotes: 0
Views: 121
Reputation: 7715
Instead of return $query->result()
, use return $query->row()
in your model. This will return a single row which you can access normally, instead of having to first reference an array.
E.g. in your view you could then do:
echo $get_company->title;
Relevant section in CI manual.
Upvotes: 2