“Displaying X to Y of Z results” using Codeigniter pagination library?

前端 未结 3 1881
名媛妹妹
名媛妹妹 2021-01-15 05:58

I am, using the Codeigniter pagination library,

I am wondering how I can grab the number of the first and last items displayed using the pagination class?

So

相关标签:
3条回答
  • 2021-01-15 06:26

    itachi's solution works great except you need to handle the case where the last page has less than $per_page elements.

    if ($result_end > $total) {
        $result_end = $total;
    }
    
    0 讨论(0)
  • 2021-01-15 06:32

    In CodeIgniter Controller file

    $params['limit'] = 15; $params['offset'] = ($this->input->get('per_page')) ? $this->input->get('per_page') : 0;

        $config = $this->config->item('pagination');
        $config['per_page'] = $params['limit'];
        $config['base_url'] = site_url('cy_controller/action?');
    
        $config['total_rows'] = $this->abcd_model->get_count($params);
        $this->pagination->initialize($config);
        $data['merchant'] = $this->abcd_model->get_all($params);
        $countMerchant = count($data['merchant']);
        if ($params['offset'] == 0) {
            $find_total_record = $countMerchant;
        } else {
            $valuec = $params['offset'] + $params['limit'];
            if ($valuec > $config['total_rows'])
                $find_total_record = $params['offset'] + $countMerchant;
            else
                $find_total_record = $params['offset'] + $params['limit'];
        }
        $per_page_total = $find_total_record;
    
        $initial = $params['offset'] == 0 ? 1 : $params['offset'];
        $data['showing'] = "Showing  " . $initial . " to " . $per_page_total . " of " . $config['total_rows'] . " results";
    
    
        $data['_view'] = 'cy_controller/action';
        $this->load->view('layouts/main', $data);
    

    And in view file

     <div class="pull-left">
         <?php echo $showing; ?>
      </div>
    
    0 讨论(0)
  • 2021-01-15 06:38

    Keeping it simple.

    You need 3 variables. Result start, result end (in the page, not the whole) and total result.

    1. You already know the total results (from the pagination). Let's call it $total.

    2. so, now get the current page ($curpage) value from CI instance. Then,

      $result_start = ($curpage - 1) * $per_page + 1;
      if ($result_start == 0) $result_start= 1; // *it happens only for the first run*
      
    3. for $result_end, you just need to add the per page value but considering it'll be 1 less,

      $result_end = $result_start+$per_page-1;
      
      if ($result_end < $per_page)   // happens when records less than per page  
          $result_end = $per_page;  
      else if ($result_end > $total)  // happens when result end is greater than total records  
          $result_end = $total;
      
    4. send all those 3 values to view.

      echo "displaying $result_start to $result_end of $total";
      
    0 讨论(0)
提交回复
热议问题