0

我是使用 codeigniter 的新手,所以现在有点困惑。

当我将玩家数据对象传递给视图时,它会正确列出所有内容。我正在尝试使用codeigniter的分页。

我在控制器的一个函数中加载了分页:

public function playersHub(){
    $playersData = $this->cms_model->getAllPlayers();
    $pageConf = array(
        'base_url' => base_url('home/playersHub/'),
        'total_rows' => count($playersData),
        'per_page' => 20,
        'uri_segment' => 3,
    );
    $this->pagination->initialize($pageConf);
    $data = array(
        'players'=> $playersData,
        'content'=> 'home',
        'link' => $this->pagination->create_links() 
    );
    $this->load->view('template/template',$data);
}

在我看来:

<div class="container-fluid">
<div class="row-fluid">
    <table class = "table table-strip">
        <?php foreach($players as $p): ?>
            <tr>
                <td><?php echo $p->ufbid; ?></td>
                <td><?php echo $p->uname; ?></td>
                <td><?php echo $p->ulocation; ?></td>
                <td><?php echo $p->uemail; ?></td>
                <td><?php echo $p->umobile; ?></td>
                <td><?php echo (($p->ugrand == 0) ? 'no':'yes'); ?></td>
                <td><?php echo $p->points; ?></td>
                <td><?php echo $p->pticks; ?></td>
            </tr>
        <?php endforeach; ?>
        <tr><td colspan = "9"><?php echo $link; ?></td></tr>
    </table>

</div>

我在构造函数方法中加载了分页。它创建了一个分页,但数据仍然在一页中。当我单击 2 或“下一步”时,它仍然执行相同的操作。

4

2 回答 2

2

1.首先加载分页库:

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

2.从数据库返回正确的计数,做一个不同的函数来返回总计数。不计算 getAllPlayers(). 根据您的情况,用于getAllPlayers()返回 要在页面中显示的数据。20 (per_page)offset($this->uri->segment(3))

例如。

function playersHub(){
    $perPage    = 20;
    $playersData = $this->cms_model->getAllPlayers($perPage, $this->uri->segment(3));
    $pageConf = array(
        'base_url' => base_url('home/playersHub/'),
        'total_rows' => $this->model_name->count_all(),
        'per_page' => $perPage,
        'uri_segment' => 3,
    );
    $this->pagination->initialize($pageConf);
    $data = array(
        'players'=> $playersData,
        'content'=> 'home',
        'link' => $this->pagination->create_links() 
    );
    $this->load->view('template/template',$data);
}

#function to count all the data in the table
function count_all(){
    return $this->db->count_all_results('table_name');
}

#function to return data from the table depending on the offset and limit
function getAllPlayers($limit, $offset = 0){
    return $this->db->select('')->where('')->get('table_name', $limit, $offset)->result_array();
}
于 2013-10-17T09:21:04.003 回答
0

像这样的东西

public function playersHub(){
$playersData = $this->cms_model->getAllPlayers();
$pageConf = array(
    'base_url' => base_url('home/playersHub/'),
    'total_rows' => count($playersData),
    'per_page' => 20,
    'uri_segment' => 3,
);
$this->pagination->initialize($pageConf);


//get the start
    $start = ($this->uri->segment(3))?$this->uri->segment(3):0;
    //apply the pagination limits.You may need to modify your model function
    $playersData = $this->cms_model->getAllPlayers($start,$pageConf['per_page']);

$data = array(
    'players'=> $playersData,
    'content'=> 'home',
    'link' => $this->pagination->create_links() 
);
$this->load->view('template/template',$data);

}

于 2013-10-17T09:27:58.353 回答