views:

255

answers:

1

I need to get the number of rows of a query (so I can paginate results).

As I'm learning codeigniter (and OO php) I wanted to try and chain a ->num_rows() to the query, but it doesn't work:

//this works:
$data['count'] = count($this->events->findEvents($data['date'], $data['keyword']));

//the following doesn't work and generates
// Fatal Error: Call to a member function num_rows() on a non-object
$data['count2'] = $this->events->findEvents($data['date'], $data['keyword'])->num_rows();

the model returns an array of objects, and I think this is the reason why I can't use a method on it.

function findEvents($date, $keyword, $limit = NULL, $offset = NULL)
{
    $data = array();

    $this->db->select('events.*, venues.*, events.venue AS venue_id');
    $this->db->join('venues', 'events.venue = venues.id');

    if ($date)
    {
        $this->db->where('date', $date);
    }

    if ($keyword)
    {
        $this->db->like('events.description', $keyword);
        $this->db->or_like('venues.description', $keyword);
        $this->db->or_like('band', $keyword);
        $this->db->or_like('venues.venue', $keyword);
        $this->db->or_like('genre', $keyword);
    }

    $this->db->order_by('date', 'DESC');
    $this->db->order_by('events.priority', 'DESC'); 
    $this->db->limit($limit, $offset); //for pagination purposes
    $Q = $this->db->get('events');
    if ($Q->num_rows() > 0)
    {
        foreach ($Q->result() as $row)
        {
            $data[] = $row;
        }
    }

    $Q->free_result();
    return $data;
}   

Is there anything that i can do to be able to use it? EG, instead of $data[] = $row; I should use another (OO) syntax?

A: 

You function findEvents is returning $data which you declared to be an array at the start. This is not an object and does not allow you to access functions using the member access syntax.

To count the number of values in an array see count

Also, from what I understand you not only want the query results returned as an array, but you want to be able to access methods on the result $Q = $this->db->get('events'); However this is not possible as this is a local variable and it is not being returned out. The function here has a result type of array and is not an object and thus has no access to anything, but the array. One solution to this is to return an associative array:

return array("results" => $data, "count" => $Q->num_rows());

Then use array syntax to access the count or the results. Another option is return a new object that has result and count fields and use accessor methods to get to those.

Joshua Rodgers
Thanks ByteMR. As you have understood I'm a newbie with both codeigniter and OOP, so sorry for my stupid question(s)! However, the thing that I'd like to understand is: how should I modify the model in order to return an object, instead of an array? And, how would I access its properties, eg in a foreach loop?
Patrick
I'd refer you to the PHP manual on classes at: http://www.php.net/manual/en/oop5.intro.phpEssentially you'd create a class with two properties. One for data and one for the row count. I'd name it like "EventResult" and then create a constructor that sets the values of these two properties. Then you can do like: `return new EventResult($data, $Q->num_rows());`
Joshua Rodgers