沃梦达 / 编程问答 / php问题 / 正文

如何使用 codeigniter 3.0.1 进行分页

How to paginate with codeigniter 3.0.1(如何使用 codeigniter 3.0.1 进行分页)

本文介绍了如何使用 codeigniter 3.0.1 进行分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

控制器示例.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 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>

推荐答案

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

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

这篇关于如何使用 codeigniter 3.0.1 进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何使用 codeigniter 3.0.1 进行分页

基础教程推荐