如何使用codeigniter 3.0.1进行分页

时间:2015-10-10 06:55:47

标签: php codeigniter pagination

我正在尝试使用CodeIgniter创建分页。它显示链接很好,但是当我点击链接时它显示404错误。怎么解决这个问题?

Controller Example.php

class example extends CI_controller
{

 public function __construct()
  {
    parent::__construct();
    $this->load->model('Donor_model');
    $this->load->library('pagination');
    $this->load->helper('url');
    $this->load->library('form_validation');

  }


  public function get_details()
   {
    $config['base_url'] = base_url('info');
    $config['total_rows'] = $this->Donor_model->count();
    $config['per_page'] = 10;
    $config['uri_segment'] = 3;
    $choice = $config['total_rows']/$config['per_page'];
    $config['num_links'] = round($choice);


    $this->pagination->initialize($config);

    $page =($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
    $data['data'] = $this->Donor_model->get_data([], [],$config['per_page'],$page);
    $data['links'] = $this->pagination->create_links();


    $this->load->view('admin/info',$data);
   }
}

模型(Example_Model.php)

扩展Example_Model

My_Model class类:

class Donor_model extends MY_Model
{
protected $table = 'donors';

public function __construct()
{
    //call the MY_Model construtor
    parent::__construct();  
}   

public function get_data($where, $fields,$limit, $start)
{
    return $this->get($where,$fields , $limit, $start);
}
public function count_data()
{

    return $this->count();
}

模型(My_Model.php)

class MY_Model extends CI_Model
{
protected $table = NULL;

// protected $table_fields = [];

// protected $fillable = [];

// protected $protected = [];

protected $primary_key = 'id';



function __construct()
{
    parent::__construct();
    $this->load->database();
}

/**
* Return all table contents
* @param array $where ,$fields, $limit 
* @return array
*defualt $sortOrder = dsc
*/

// public function get(array $where = NULL, $limit = 1000, $sortOrder = 'dsc')
public function get(array $where = NULL, array $fields = NULL, $limit = NULL, $start = NULL)
{
    $query = NULL;
    // return all records with all fields from table
    if($fields == NULL and $where == NULL){

        $this->db->limit($limit, $start);
        $query = $this->db->get($this->table);
        if ($query->num_rows() > 0 ) {

            return $query->result();
        }
        else
            return false;
    }

    // rteurn all records with only my fields
    elseif($fields != NULL and $where == NULL){

        $this->db->limit($limit, $start);
        $this->db->select($fields);
        $query = $this->db->get($this->table);
        if ($query->num_rows() > 0) 
            return $query->result();

        else
            return false;

    }

    // return all records through condition
    elseif($fields == NULL and $where != NULL){

        $this->db->limit($limit, $start);
        $query = $this->db->get_where($this->table, $where);
        if ($query->num_rows() > 0) 
            return $query->result();

        else
            return false;
    }

    // return all records with only my fields through condition 
    else{

        $this->db->limit($limit, $start);
        $this->db->select($fields);
        $query = $this->db->get_where($this->table, $where);

        if ($query->result() > 0 )
            return $query->result();

        else
            return false;
    }
}

public function count()
{
    return $this->db->count_all($this->table);
}

}

查看(info.php)

<!DOCTYPE html>
 <html lang="en">
 <head>
  <meta charset="UTF-8">
  <title>Applicants - admin</title>
  <link rel="stylesheet" href="<?php echo base_url('css/normalize.css'); ?> ">
  <link rel="stylesheet" href="<?php echo base_url('css/admin-style.css'); ?> ">
 </head>
 <body>
   <header>
      <img src="<?php echo base_url('images/path1.png'); ?>" alt="">
   </header>
 <section>
    <table class="show-item">
        <tbody>
            <th>Donor details</th>
            <tr>
                <th>Name</th>
                <th>Place</th>
                <th>Phone</th>
            </tr>
            <?php foreach ($data as $key => $row) {
            ?>
            <tr>
                <td>
                    <a href=""><h3><?php echo $row->name;?></h3></a>
                </td>
                <td>
                    <p><?php echo $row->place;?></p>
                </td>
                <td>
                    <p><?php echo $row->phone;?></p>
                </td>
                </tr>
            <?php } ?>
        </tbody>
    </table>
<?php echo $links ?>
</section>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

您确定已从网址中删除了 index.php 吗? 此外,如果您重新定义了路线,以便“info”指向“example / get_details”(正如我从您的评论中看到的那样),那么$config['uri_segment']应该设置为“2”,而不是“3”。