【问题标题】:How to change record numbers in the grid?如何更改网格中的记录数?
【发布时间】:2012-11-25 16:14:19
【问题描述】:

我正在使用 codeigniter 分页,这是我的代码

$config['base_url'] = $this->config->base_url().'users/allusers/';
    $config['uri_segment'] = 3;
    $config['next_link'] = 'Next';
    $config['prev_link'] = 'Prev';
    $config['full_tag_open'] = '<div id="pagination">';
    $config['full_tag_close'] = '</div>';   
    $config['total_rows'] = $this->usersmodel->getUsersCount();
    $config['per_page'] = 20;   
    $this->pagination->initialize($config);     
    $data['paging']=$this->pagination->create_links();      
    $data['query']=$this->usersmodel->getAllusers($config['per_page'],$this->uri->segment(3));

现在我需要在网格中添加记录编号,这是我的网格代码。

$i=1; foreach ($query->result() as $row){ 回声 $i; 回声$行->用户名; 回声$行->名称; 回声$行->城市; 回声$行->地址; $i++; } ?>

即使我在第二页,这里的 $i 总是打印数字 1-20。实际上我需要打印 20-40 我需要知道我该怎么做?

【问题讨论】:

    标签: codeigniter


    【解决方案1】:

    您必须将 $i 设置为当前页面的值减一,乘以页面中的行数:

    替换:

    $i=1;
    

    $i = ( $this->pagination->cur_page > 1 ) ? ( $this->pagination->cur_page - 1 )  * $config['per_page'] : 1 ;
    

    如果 cur_page 不可访问,请尝试使用以下方式扩展分页类:

    创建一个名为 MY_Pagination.php 的文件并将其放在 application/libraries/ 中

    将此代码添加到扩展分页类;

    class MY_Pagination extends CI_Pagination {
    
        public function __construct() {
            parent::__construct();
        }
    
        public function getCurrentPage() {
            return $this->cur_page;
        }
    }
    

    不要忘记确保在 config.php 中将类扩展前缀正确设置为 MY_(大约第 109 行)

    然后你可以像这样访问它:

    $i = ( $this->pagination->getCurrentPage() > 1 ) ? ( $this->pagination->getCurrentPage() - 1 )  * $config['per_page'] : 1 ;
    

    我使用并稍微修改了这个问题的一些内容:

    How to echo page number with codeigniter pagination library?

    【讨论】:

      【解决方案2】:

      $i 设置为页码乘以每页的行数。因此,当您循环到数组时,它将在第一页从 0-20 开始,在第二页从 20-40 开始。

      但是,使用这种方法,您需要确保第一页为 0,第二页为 1,等等...

      $i = ($page_number - 1) * $config['per_page']; 
      // $page_number may be $this->uri->segment(3) from what I can determine from your code.
      
      foreach ($query->result() as $row){
          echo $i; 
          echo $row->username;
          echo $row->name;
          echo $row->city;
          echo $row->address;        
          $i++;
      }
      

      【讨论】:

      • 感谢 Max_ 我将此代码更改为 $i = ($this->uri->segment(3)=="" ? 1 : $this->uri->segment(3)+1 );这对我来说很好。
      【解决方案3】:

      您可以在任何地方使用此代码进行分页......

      codeIgniter 提供了一个内置的分页类。您可以在用户指南中找到它。

      在函数中定义一个起始索引变量,您希望在其中使用分页为零。

       public function pagination($start_index = 0)
      

      {

       $result = $this->model->get_results($data); //this $data is the argument which you are   passing to your model function. If you are using database to get results array.
      
       $items_per_page = 10;   //this is constant variable which you need to define
      
       $filtered_result = array_splice($result, $start_index, ITEM_PER_PAGE_USERS);
      
       $model['$filtered_result'] = $filtered_result;
      
       $total_rows = count($result);
      
      $model['page_links'] = create_page_links  (base_url()."/controlelr_name/pagination",ITEM_PER_PAGE_USERS, $total_rows);
      
      $this->load->view('controller_name/view_file_name', $model);
      

      }

       function create_page_links($base_url, $per_page, $total_rows) 
      

      {

       $CI = & get_instance();
       $CI->load->library('pagination');
      
      $config['base_url'] = $base_url;
      $config['total_rows'] = $total_rows;
      $config['per_page'] = $per_page; 
      
      $CI->pagination->initialize($config); 
      
      return $CI->pagination->create_links();
      

      }

      【讨论】:

        猜你喜欢
        • 2011-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-26
        相关资源
        最近更新 更多