Welcome Guest, Not a member yet? Register   Sign In
Why does paginate call the database twice?
#1

[eluser]gh0st[/eluser]
I'm trying to build a search form, which will query the database and then eventually spit the results out in a paginated form.

I've gone through the tutorial, and the forum examples (where possible) and every time the Paginate class appears to call the database twice.

1. To run your query (with no MySQL limit) and find out how many records there are.
2. To do your query (with MySQL limit) and then display them.

What if your database is really big, such as 5-10MB+ databases?

Firstly, should I be concerned that the paginate calls the database twice, and secondly; is the paginate class that comes with CI the best paginate class to use?

Example;
Code:
// this is an example and may not run correctly
// controller
class Paging extends Controller
{
    function Paging()
    {
        parent::Controller();
        $this->output->enable_profiler(TRUE);
        $this->load->database();
        $this->load->library('pagination');
    }

    function index()
    {
        // build query
        $sql = 'SELECT id, name FROM `books` ORDER BY name DESC';

        // run first query
        $query        = $this->db->query($sql);
        $num_rows    = $query->num_rows();

        // get offset
        $offset        = $this->uri->segment(3);

        // pagination config
        $config['base_url']    = '/paging/index/';
        $config['total_rows']    = $num_rows;
        $config['cur_page']    = $offset;
        $config['per_page']    = 10;

        // run second query
        $sql_second        = $sql ." LIMIT ".$config['cur_page'].", ".$config['per_page'];
        $data['results']    = $this->db->query($sql_second);

        // debug output
        print '<p>'.$sql_second.'</p>';

        // initalize pagination
        $this->pagination->initialize($config);

        // load the HTML Table Class
        $this->load->library('table');
        $this->table->set_heading('id', 'name');

        // load view
        $this->load->view('paging', $data);
    }

} // end class


Messages In This Thread
Why does paginate call the database twice? - by El Forum - 02-08-2009, 11:44 AM
Why does paginate call the database twice? - by El Forum - 02-08-2009, 12:16 PM
Why does paginate call the database twice? - by El Forum - 02-08-2009, 01:48 PM
Why does paginate call the database twice? - by El Forum - 02-08-2009, 02:03 PM
Why does paginate call the database twice? - by El Forum - 02-08-2009, 02:20 PM
Why does paginate call the database twice? - by El Forum - 06-03-2009, 09:00 AM
Why does paginate call the database twice? - by El Forum - 06-03-2009, 10:01 AM
Why does paginate call the database twice? - by El Forum - 06-03-2009, 10:08 AM
Why does paginate call the database twice? - by El Forum - 06-03-2009, 10:16 AM



Theme © iAndrew 2016 - Forum software by © MyBB