Welcome Guest, Not a member yet? Register   Sign In
return num_rows and query results
#8

[eluser]TheFuzzy0ne[/eluser]
This is the beauty of SQL_CALC_FOUND_ROWS. It basically ignores the LIMIT clause, so you'll get the total number of rows matched by your query.
Code:
function get($where=array(), $limit=25, $offset=0)
{
    $this->db->select('SQL_CALC_FOUND_ROWS *');
    $this->db->where($where);
    $this->db->limit($limit, $offset);
    $res = $this->db->get('my_table');
        
    $total_rows = $this->db->query('SELECT FOUND_ROWS()');
    $total_rows = $total_rows->result_array();
        
    return array(
            $res->result_array(),
            $total_rows[0]['FOUND_ROWS()']
        );
}

Try it, you might be pleasantly surprised, just remember that I think this is only supported by MySQL. If you want it to be platform independent, you'll need to do a separate call to count_all_results() and hit the database twice. This isn't a limit imposed by the library, but rather by the database platform.


Messages In This Thread
return num_rows and query results - by El Forum - 05-29-2009, 11:10 AM
return num_rows and query results - by El Forum - 05-29-2009, 11:18 AM
return num_rows and query results - by El Forum - 05-29-2009, 11:30 AM
return num_rows and query results - by El Forum - 05-29-2009, 11:40 AM
return num_rows and query results - by El Forum - 05-29-2009, 11:56 AM
return num_rows and query results - by El Forum - 05-29-2009, 12:06 PM
return num_rows and query results - by El Forum - 05-29-2009, 12:09 PM
return num_rows and query results - by El Forum - 05-29-2009, 12:34 PM



Theme © iAndrew 2016 - Forum software by © MyBB