0

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?

1

1 Answer 1

2

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.

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

3 Comments

Thank you, just what I'm looking for.
Do I need to create a second model if i also want to pass this data to my view, since I can't use $data['get_company'] when returning $query->row()?
Not sure I understand your question. You can use $data['get_company'] when returning $query->row().

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.