John Chu
John Chu

Reputation: 33

codeigniter pagination with selected records

I am new with codeigniter. Hope can help to solve problem on codeigniter pagination.

  1. i have selected some records from my view and pass to my controller using $_POST.
  2. the controller will use $_POST variable to select records from a model.
  3. then records will be display in the same view with pagination.

step 1-3 is okey, the view display correct info.

  1. when press on the pagination button from my view. This will call the same controller but $_POST info be come blank. So, my view is not displaying with the selected records as needed.

hope can help out.

i have simplied the code as follows:-

controllers:

    $config['total_rows']=$this->invdata_model->getFilterData_numRows();

    $config['base_url']=site_url('site/users_area') ;
    $config['uri_segment'] = '3'; 
    $config['per_page']=18;
    $config['num_links']=4;

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

    $data['records']=$this->invdata_model->getFilterData_Rows($config['per_page'],$this->uri->segment(3));
    $data['rec_country']=$this->invdata_model->country();

    $this->load->view('includes/header');
    $this->load->view('users_area_view',$data);
    $this->load->view('includes/footer');

models:

    $country = $this->input->post('country') ;


    $this->db->select('stockno, bdlno, country,volton');
    if (isset($country)) { $this->db->where_in('country',$country); }
    $q=$this->db->get('inventory')->num_rows();

    return $q ; 

View

  <?php echo form_open('site/users_area');   
        echo $this->table->generate($records);   
        echo $this->pagination->create_links() ;    ?>

  <div class="gadget">
<?php echo form_submit('submit','Apply','class="button_form"'); ?>      
  </div>

  $gadget['gadget_name']='country'; 
  $gadget['gadget_rec']=$rec_country; 
  $this->load->view('gadget',$gadget);
  </form>

View Gadget

  <div class="gadget">
  <?php

  $gadget_name2=$gadget_name.'[]';

  echo "<ul>";
  foreach ($gadget_rec as $item) {
    echo '<li >';  
    echo '<div id="sectionname">'.$item.'</div>';
    echo '<div id="sectioninput"><input type="checkbox" name="'.$gadget_name2.'" value="'.$item.'"></div>' ;
    echo '-';
    echo "</li>";  
  }

  echo "<ul>";
   ?>
  </div>

Thank you.

Upvotes: 0

Views: 4940

Answers (3)

GautamD31
GautamD31

Reputation: 28753

I suggest you to edit like:

$config['base_url']=site_url('site/users_area/arg1/arg2') ;

and then "$_GET" it form url or jst use

$val1 = $this->uri->segment(3);
$val1 = $this->uri->segment(4);

and also change your uri_segment

$config['uri_segment'] = '3';to
$config['uri_segment'] = '5'.

Depends on number of argumnts passing through url

Upvotes: 0

landons
landons

Reputation: 9547

It sounds like an approach issue. Why are you using POST data to filter your data? The pagination library builds a query string to determine the offset of your database query results. Why can't you use $_GET instead of $_POST?

I suppose it would be possible to set your pagination config 'base_url' to this:

$this->load->helper('url');
$this->load->library('pagination');

$config['base_url'] = current_url().'?'.http_build_query($_POST);
$config['total_rows'] = 200;
$config['per_page'] = 20;

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

echo $this->pagination->create_links();

And then in your controller, use $this->input->get_post(), rather than $this->input->post(). Be careful here--it's rarely safe to pass the full post data directly into a model for processing. It would be better to use CI's input class to prevent XSS attacks...

UPDATE:

To use CI's input class, rather than $_POST or $_GET directly, I usually keep my form data in a "namespace" of sorts, i.e.:

<input type="text" name="search[criteria1]" />
<input type="text" name="search[criteria2]" />
<input type="text" name="search[criteria3]" />

Then, in your controller:

...
public function some_resource()
{
    $this->load->model('Some_model');
    $search_criteria = $this->input->get_post('search');
    if ($search_criteria)
    {
        // make sure here to remove any unsupported criteria
        // (or in the model is usually a bit more modular, but
        // it depends on how strictly you follow MVC)
        $results = $this->Some_model->search($search_criteria);
    }
    else
    {
        // If no search criteria were given, return unfiltered results
        $results = $this->Some_model->get_all();
    }

    $this->load->view('some_view', array(
        'results'         => $results,
        'pagination_base' => current_url().'?'.http_build_query($search_criteria)
    ));
}
...

Because I specified $this->input->get_post(), it will first check for a query string parameter; if it doesn't exist, it falls back to post data. This will allow you to use POST in your form, but still pass in the same data through the query string with the pagination class. However, really the form should be using the GET method, as you're sending these parameters as a query, and not to send something to the server. Just my $.02 on that detail.

Upvotes: 1

minboost
minboost

Reputation: 2563

The pagination class uses GET requests and simply modifies the URL with the record offset of the page. For example, page 2 for a list that shows 50 records per page would be /some/url/50 and page 3 would be /some/url/100.

You can either:

  1. Change #3 to work with either a POST or GET and call a URL that can identify which records to select, or
  2. Extend the pagination class to output a form that resends the same POST data from #2 each time you change the page (and have each page link POST a from rather than just be a link).

I think #1 is probably the best option. It's SEF, can be bookmarked, and is overall a cleaner way to do it.

For example, you use the form, as you normally do, but you can also go directly to the result using /some/unique/url

Then each pagination link will call /some/unique/url/50, where 50 is the record offset (pagination class uses offset rather than page number).

Upvotes: 0

Related Questions