Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
768 views
in Technique[技术] by (71.8m points)

php - How to paginate with codeigniter 3.0.1

I am trying to create pagination using CodeIgniter. It displays links fine, but when I click the links it shows 404 error. How to solve this?

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

Model (Example_Model.php)

Example_Model class which extends 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();
}

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

}

view (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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Did you make sure you've removed index.php from your url? Also, if you've redefined the route so that "info" points to "example/get_details" (as I see from your comments...), then the $config['uri_segment'] should be set to "2", and not "3".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...