• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pagination config problem

#1
[eluser]CI_Newb[/eluser]
Trying to get pagination working properly on my search form but having issues getting the total_rows.

I need to get the total_rows from the query rather than from entire database.

Model
Code:
function admin_search($num, $offset)
    {
        $start_date = $this->input->post('startDate');    
        $end_date = $this->input->post('endDate');
        $username = $this->input->post('username');
        
        $dateRange = "submit_date BETWEEN '$start_date' and '$end_date'";
        $this->db->where($dateRange, NULL);
        $this->db->like('username', $username);

        $data = $this->db->get('data', $num, $offset);
        
        if($data->num_rows() > 0) {
            return $data;
            }
    }

Controller
Code:
function admin_results()
    {
        $this->load->library('pagination');
        
         $config['base_url'] = base_url().'/search/admin_results/';
        $config['total_rows'] = $this->db->count_all('data');
        $config['per_page'] = '100';
        $config['num_links'] = 3;
        $config['full_tag_open'] = '<p>';
    $config['full_tag_close'] = '</p>';
    
    $this->pagination->initialize($config);
        
        $data['users'] = $this->menus->agents_menu();
        $data['search'] = $this->search_model->admin_search($config['per_page'],$this->uri->segment(3));
        $this->load->view('content/header');
        $this->load->view('admin/admin_search_view', $data);
        $this->load->view('content/footer');
    }

Displaying in view
Code:
echo $this->pagination->create_links();

How do I get the total number of rows from
Code:
$data['search'] = $this->search_model->admin_search($config['per_page'],$this->uri->segment(3));

To show for
Code:
$config['total_rows'] = $this->db->count_all('data');

#2
[eluser]SPeed_FANat1c[/eluser]
with this line

Code:
$config['total_rows'] = $this->db->count_all('data');

you count all rows from table 'data'.

You need to run this

Code:
$data['search'] = $this->search_model->admin_search($config['per_page'],$this->uri->segment(3));

and then you can set $config['total_rows'] that way:

Code:
$config['total_rows'] = $data['search']->num_rows();

I didn't try but I think it should work.

#3
[eluser]LuckyFella73[/eluser]
SPeed_FANat1c is right - I think you just have to get rid of the
"$config['per_page']" part when getting the total_rows. Otherwise
you allways get only up to 'per_page' as the result of total rows.

The "$config['per_page']" part should only be needed when you get
the results for a result set (the query you run and send to the view
when your page with pagination-module is rendered)

At least I do it that way in my pagination part and it works Wink

#4
[eluser]CI_Newb[/eluser]
[quote author="SPeed_FANat1c" date="1289586724"]with this line

Code:
$config['total_rows'] = $this->db->count_all('data');

you count all rows from table 'data'.

You need to run this

Code:
$data['search'] = $this->search_model->admin_search($config['per_page'],$this->uri->segment(3));

and then you can set $config['total_rows'] that way:

Code:
$config['total_rows'] = $data['search']->num_rows();

I didn't try but I think it should work.[/quote]

So I run it once to get the total rows then im running it again to pass to the view?

#5
[eluser]SPeed_FANat1c[/eluser]
you don't have to run it twice, you already have information in the variable $data['search']. Getting number of rows didn't delete the information.

Have you tried this code?

#6
[eluser]CI_Newb[/eluser]
[quote author="SPeed_FANat1c" date="1289743129"]you don't have to run it twice, you already have information in the variable $data['search']. Getting number of rows didn't delete the information.

Have you tried this code?[/quote]

Ya i've been playing with what you suggested, but I just can't get it to go.

Right now I don't get errors and it is returning the right amount of rows, but I can't get the pagination links to show.

Here's how my model looks now.
Code:
function admin_results()
{
$this->load->library('pagination');

$config['base_url'] = base_url().'/search/admin_results/';
$config['per_page'] = '100';
$config['num_links'] = 3;
$config['full_tag_open'] = '<p>';
$config['full_tag_close'] = '</p>';

$data['users'] = $this->menus->agents_menu();
$data['search'] = $this->search_model->admin_search($config['per_page'],$this->uri->segment(3));

$config['total_rows'] = $data['search']->num_rows();  
$this->pagination->initialize($config);

$this->load->view('content/header');
$this->load->view('admin/admin_search_view', $data);
$this->load->view('content/footer');
}

Am I on the right track?

#7
[eluser]CI_Newb[/eluser]
[quote author="LuckyFella73" date="1289589658"]SPeed_FANat1c is right - I think you just have to get rid of the
"$config['per_page']" part when getting the total_rows. Otherwise
you allways get only up to 'per_page' as the result of total rows.

The "$config['per_page']" part should only be needed when you get
the results for a result set (the query you run and send to the view
when your page with pagination-module is rendered)

At least I do it that way in my pagination part and it works Wink[/quote]

Just to clarify, I can just remove the "$config['per_page']" part from "$data['search']"?

#8
[eluser]LuckyFella73[/eluser]
I didn't test this and you can shorten the code for sure - what
I meant was:
Code:
// controller
    function admin_results()
    {
        $this->load->library('pagination');
                
        $config['total_rows'] = $this->search_model->total_rows();
        
        $config['base_url'] = base_url().'/search/admin_results/';
        $config['per_page'] = '100';
        $config['num_links'] = 3;
        $config['full_tag_open'] = '<p>';
        $config['full_tag_close'] = '</p>';
        
        $this->pagination->initialize($config);

        $data['users'] = $this->menus->agents_menu();
        $data['search'] = $this->search_model->admin_search($config['per_page'],$this->uri->segment(3));
        
        $this->load->view('content/header');
        $this->load->view('admin/admin_search_view', $data);
        $this->load->view('content/footer');
    }
    
    
    // Model:
    function total_rows()
    {
        $start_date = $this->input->post('startDate');    
        $end_date = $this->input->post('endDate');
        $username = $this->input->post('username');
        
        $dateRange = "submit_date BETWEEN '$start_date' and '$end_date'";
        $this->db->where($dateRange, NULL);
        $this->db->like('username', $username);
        
        $this->db->get('data');
        return $this->db->count_all_results();
    }
    
    // Model:
    function admin_search($num, $offset)
    {
        $start_date = $this->input->post('startDate');    
        $end_date = $this->input->post('endDate');
        $username = $this->input->post('username');
        
        $dateRange = "submit_date BETWEEN '$start_date' and '$end_date'";
        $this->db->where($dateRange, NULL);
        $this->db->like('username', $username);

        $this->db->limit($offset, $num);
        
        $data = $this->db->get('data');
        
        if($data->num_rows() > 0)
        {
            return $data;
        }
    }

Hope that work for you..

#9
[eluser]CI_Newb[/eluser]
Just so I understand, essentially what i need to do is run the query once to get the total_rows, then I can use that value to run the query a second time to use the pagination.

Am I understanding this right?

#10
[eluser]LuckyFella73[/eluser]
The pagination library just need to know how many rows there are
available totally and how many rows you want to display per page
to calculate how many links (pages) to create (and how to set up the pagination-links).

The second query gets the data for the actual page based on the given
values (per page, start page). The query to generate the pagination
links is generally undependent from the query getting the data to display
in your view. You just have to make sure that the num-per-page value and
the "start-from" value in your "view-query" matches with the pagination
config value to get a result set and pagination links that make sense.

Sorry, for I'm no native english speaker it's not so easy to express
clearly what I'm trying to ...


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.