为什么在CodeIgniter中分页链接对我不起作用?

时间:2017-11-29 10:31:37

标签: php codeigniter pagination codeigniter-3

我试图为我的产品使用codeigniter分页,所以有多个页面哪些产品但它不适合我,我不知道为什么......

这是我的控制器中的分页功能:

//code om in allecadeaus te bepalen hoeveel producten er per pagina zitten
   public function pagination() {
        //load pagination library
        $this->load->library('pagination');
        //laad Products_model voor pagination
        $this->load->model('Pagination_model');
        $this->load->model('Product_model');

        $config = array();
        $config["base_url"] = base_url() . "AlleCadeausController/pagination";
        $config["total_rows"] = $this->products->record_count();
        $config["per_page"] = 24;
        $config['cur_tag_open'] = '<a><b class="text-success">';
        $config['cur_tag_close'] = '</b></a>';
        $config["uri_segment"] = 3;
        $config['use_page_numbers'] =True;
        $config['enable_query_strings'];

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

        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data['title'] = "Products";
        $data['products'] = $this->Product_model->selectProducts();
        $data["links"] = $this->pagination->create_links();

        $this->load->view("allecadeaus", $data);
    }

通过这一行,我可以从产品表中获取所有产品:

$data['products'] = $this->Product_model->selectProducts();

我的分页模式:

<?php
class Pagination_model extends CI_Model
{

    public function __construct() {
        parent::__construct();
    }

    public function record_count() {
        return $this->db->count_all("products");
    }

     public function fetch_products($limit, $start) {
        $this->db->limit($limit, $start);
        $query = $this->db->get_where('users',array('Is_Hidden'=>0));

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
    }

?>

在我的所有产品页面上,我现在尝试回显链接,但它不起作用。我没有看到正确的链接,只有1个链接可以导致其他内容。这是我在所有产品页面视图中的代码:

<div class="text-center"><nav aria-label="Page navigation">
                                            <ul class="pagination">

                                                <li><?php echo $links; ?></li>

                                            </ul>
                                        </nav></div>

我做错了什么?

2 个答案:

答案 0 :(得分:1)

请进行如下更改:

控制器:

public function pagination($row = 0) {
     //load pagination library
        $this->load->library('pagination');
        //laad Products_model voor pagination
        $this->load->model('Pagination_model');
        $this->load->model('Product_model');
        $config = array();
        $limit = '24';      
        $config['base_url'] = site_url() . '/AlleCadeausController/pagination';
        $config['full_tag_open'] = "<ul class='pagination'>";
        $config['full_tag_close'] = '</ul>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';
        $config['prev_link'] = '<i class="fa fa-long-arrow-left"></i>Previous Page';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = 'Next Page<i class="fa fa-long-arrow-right"></i>';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';     
        $config["total_rows"] = $this->Product_model->record_count();            
        $config['per_page'] = $limit;
        $this->pagination->initialize($config);  
        $data['links'] = $this->pagination->create_links();
        $data['products'] = $this->Product_model->selectProducts($row,$limit);
        $this->load->view("allecadeaus", $data);
    }

型号:

function selectProducts($row,$limit)
        {   
            $query = $this->db->get('Your_table_name',$limit,$row);

            if ($query->num_rows() > 0){
              return $query->result_array();
            }
             else {
              return array();
            }

        }

function record_count(){        
        $this->db->from('Your_table_name');         
        $query = $this->db->get();
        $row = $query->num_rows();
        return $row;
    }

这会帮助你......谢谢!

答案 1 :(得分:0)

你有混合的代码,分页和非分页结果。

添加,

$config["num_links"] = 5; //Number of links in pagination

初始化Code​​igniter分页库之后。您必须在数据库中执行分页查询,例如:

$where = "IF you have got";
$orderby "IF you have got";

$data['products'] = $this->Pagination_model->select_products($config["per_page"], $this->uri->segment(3), $where, $orderby);

然后在你的模型Pagination_model中,你应该得到这个:

public function select_products($per_page, $segment, $where, $orderby)
{
    if (!isset($segment)) $segment = 1;

    $init= ($segment - 1) *  $per_page;

    $sql = "SELECT *
              FROM Your_table
              $where 
              ORDER BY $orderby
              OFFSET $init ROWS
              FETCH NEXT $per_page ROWS ONLY";

    $query= $this->db->query($sql);
    return $query->result_array();
  }

确保你是对的 - &gt; uri-&gt; segment()是3。 我认为你没有分页链接,因为你的查询中没有结果。