php – 如何使用codeigniter 3.0.1进行分页
发布时间:2020-12-13 13:49:29 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试使用CodeIgniter创建分页.它显示链接很好,但是当我点击链接时它显示404错误.怎么解决这个? 控制器Example.php class example extends CI_controller{ public function __construct() { parent::__construct(); $this-load-model('Donor_model');
我正在尝试使用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”.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |