dscjp19831212 2013-10-17 09:17
浏览 46
已采纳

codeigniter分页没有正确加载

I'm new in using codeigniter so am a bit confused now.

When I pass the players data object to the view, it lists all correctly. I'm trying to use codeigniter's pagination.

I loaded the pagination in a function in my controller:

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

In my view:

<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>

I loaded the pagination in my constructor method. It creates a pagination but the data is still in one page. When I click 2 or "next", it still does the same thing.

  • 写回答

2条回答 默认 最新

  • douhuo0884 2013-10-17 09:21
    关注

    1.Load the pagination library first:

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

    2.Return the correct count from the database, make a different function to return the total count. dont count the data returned by the getAllPlayers(). Use getAllPlayers() to return 20 (per_page) data to show in the page depending on the offset($this->uri->segment(3)) in your case.

    Eg.

    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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?