如何更改网格中的记录号?

时间:2012-11-25 16:14:20

标签: codeigniter

我在这里使用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){
     echo $i; 
         echo $row->username;
         echo $row->name;
         echo $row->city;
         echo $row->address;        
     $i++;
     }
    ?>

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

3 个答案:

答案 0 :(得分:1)

$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++;
}

答案 1 :(得分:1)

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

替换:

$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(约109行)中将类扩展前缀正确设置为MY_

然后您可以这样访问它:

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

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

How to echo page number with codeigniter pagination library?

答案 2 :(得分:1)

您可以在任何........

的任何地方使用此代码进行分页

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

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

 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();

}