doubi7496 2013-01-18 07:51 采纳率: 0%
浏览 42
已采纳

分页在localhost上的Codeigniter中不起作用

I am new to Codeigniter. This is my first experience in Codeigniter. I have problem in pagination. I have tried lot of codes given on forums and blogs. Lastly I followed a tutorial, link: CodeIgniter from Scratch- Day 16 - Displaying & Sorting Tabular Data - Nettuts+

Here is my code:

user.php

public function index($offset=0)
{
    $limit  =   10;
    $this->load->model('user_model');
    $results                =   $this->user_model->get_all_users($limit,$offset);
    $data['users']          =   $results['rows'];
    $data['num_results']    =   $results['num_rows'];

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

    $config =   array();
    $config['base_url']     =   site_url('admin/user');
    $config['total_rows']   =   $data['num_results'];
    $config['per_page']     =   $limit;
    $config['uri_segment']  =   3;
    $this->pagination->initialize($config);
    $date['pagination'] =   $this->pagination->create_links();
    $data['page_title'] =   "List Of Users";
    $this->load->view('admin/users', $data);
}

user_model.php

function get_all_users($limit, $offset)
{
    $q  =   $this->db
                    ->select('user_id,user_email,login_name,phone_num,user_status')
                    ->from('tbl_user')
                    ->limit($limit,$offset);
    $ret['rows']    =   $q->get()->result();

    $q      =   $this->db
                        ->select('COUNT(*) as count', FALSE)
                        ->from('tbl_user');
    $tmp    =   $q->get()->result();
    $ret['num_rows']    =   $tmp[0]->count;
    return $ret;
}

users.php

<?php 
$this->load->view('includes/header');
$this->load->view('includes/menu-admin');
?>
<div id="container">

    <?php if(isset($users)) { ?>
            <p><?php $this->load->view('includes/footer'); ?></p>
        <table id="users_table" class="board">
            <tr>
                <th class="blue-gradient">#</th>
                <th class="blue-gradient">Email Address</th>
                <th class="blue-gradient">Login Name</th>
                <th class="blue-gradient">User Status</th>
                <th class="blue-gradient">Current Status</th>
            </tr>
            <?php $count=1; ?>
        <?php foreach ($users as $user) { ?>
            <tr id="user_<?php echo $user->user_id; ?>" class="darker-on-hover">
                <td><?php echo $count; ?></td>
                <td><?php echo anchor('admin/profile/view/'.$user->user_id,$user->user_email , 'class="view-profile-details"'); ?></td>
                <td><?php echo $user->login_name; ?></td>
                <td><?php echo $user->user_status; ?></td>
                <td>
                    <?php echo anchor('admin/user/edit/'.$user->user_id , '<img src="'.base_url().'images/edit.png" title="Edit User"/>'); ?>
                    <?php echo anchor('admin/user/remove/'.$user->user_id , '<img src="'.base_url().'images/remove.png" title="Remove User"/>', 'class="remove-user-event"'); ?>
                </td>
            </tr>
        <?php $count++; } ?>
        </table>



    <?php } ?>

    <div id="pageNum">
        <?php if(strlen($pagination)): ?>       
            Pages: <?php echo $pagination; ?>
        <?php endif; ?>
    </div>
</div>

Please let me know if I am wrong?

  • 写回答

1条回答 默认 最新

  • douwei6478 2013-01-18 07:56
    关注

    Set your config like this

    $config['base_url']     =   site_url('admin/user/');
    $config['total_rows']   =   $data['num_results'];
    $config['per_page']     =   $limit;
    $config['uri_segment']  =   3;
    $this->pagination->initialize($config);
    

    Please note / in site_url. It often happens that the url genetrated for pagination is like this

     http://localhost/myapp/index.php/admin/user10
    

    Which is wrong. If you put a slash you will have correct pagination links

    http://localhost/myapp/index.php/admin/user/10
    

    EDITS :

    your controller should look like this

    Class User Extends CI_Controller
    {
        public function __construct()
        {
            parent::__construct();
        }
        public function index($offset=0)
        {
            $limit  =   10;
            $this->load->model('user_model');
            $results                =   $this->user_model->get_all_users($limit,$offset);
            $data['users']          =   $results['rows'];
            $data['num_results']    =   $results['num_rows'];
    
            $this->load->library('pagination');
    
            $config =   array();
            $config['base_url']     =   site_url('user/index');
            $config['total_rows']   =   $data['num_results'];
            $config['per_page']     =   $limit;
            $config['uri_segment']  =   3;
            $this->pagination->initialize($config);
            $date['pagination'] =   $this->pagination->create_links();
            $data['page_title'] =   "List Of Users";
            $this->load->view('admin/users', $data);
        }
    }
    

    See the class name and site_url('class/method') and in your case site_url('user/index')

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?