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

前端 未结 3 1883
名媛妹妹
名媛妹妹 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: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";
      

提交回复
热议问题