1

I need to get all the rows which are filters by the query. But only the first row is returned from the model to the controller.

Given below is my model function. How to get all the data needed?

public function getOfferTags($param) {

    $this->db->select('tags.*');
    $this->db->from('tags');
    $this->db->join('offer_tag', 'offer_tag.tag_id = tags.id');
    $this->db->join('offers', 'offers.id = offer_tag.offer_id');
    $this->db->where('offers.id', $param);
    $query = $this->db->get();
    return $query->row();    

}
2
  • Could you return the query rather than a single row, then loop over it, or return $query->result_array() Commented Sep 17, 2018 at 8:33
  • Sorry I am new to CI. When I check the returned values on the controller only the first row is returning. Commented Sep 17, 2018 at 8:36

2 Answers 2

2

Simply not return row and on that place fetch as an array form like

return $query->result_array();

After that checks in controller and get the result in array.

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

Comments

1

I just wanted to add, because you are new to CI, that you should always check if their are rows before using the array to prevent notices or issues. with mehta's method you would do if(count($rows) > 0) { //rows exist } else { // no rows, display error .etc. } or you can do:

$query = $this->db->get();
if ($query->num_rows() > 0) {
    return $query->result_array(); // or result() for obj
}
return false;

Usage:

if ($this->somemodel->getOfferTags($stmt)) {
    // has data
} else {
    // no data
}

It is important to start with good practices and this will help ;)

1 Comment

Thank you very much. I will definitely follow the above practise

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.