php – 如何使用codeigniter 3.0.1进行分页

前端之家收集整理的这篇文章主要介绍了php – 如何使用codeigniter 3.0.1进行分页前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用CodeIgniter创建分页.它显示链接很好,但是当我点击链接时它显示404错误.怎么解决这个?

控制器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)

扩展My_Model类的Example_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,$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,$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>
你确定从网址中删除了index.PHP吗? 此外,如果您重新定义了路线,以便“info”指向“example / get_details”(正如我从您的评论中看到的那样),那么$config [‘uri_segment’]应该设置为“2”,而不是“3”.

猜你在找的PHP相关文章