【问题标题】:pagination bootstrap in codeigniter can't show other pagecodeigniter中的分页引导程序无法显示其他页面
【发布时间】:2015-05-29 01:22:29
【问题描述】:

我尝试在 codeigniter 中使用引导程序进行分页。分页显示在我看来。但是,当我想去其他页面时,我得到了 404 Page Not Found The page you request was not found。怎么了?

这是我的控制器

public function shop($page = 'shop', $offset = 0) {
    $data['title'] = ucfirst($page);
    // breadcrumb start
    $breadcrumb = array(
        "Beranda" => "index",
        "Belanja Sekarang" => "",
    );
    $data['breadcrumb'] = $breadcrumb;
    // breadcrumb end

    // pagination start
    $jml = $this->db->get('produk');
    $num_rows = $this->db->count_all("produk");

    $config['base_url'] = base_url() . "page/shop";
    $config['total_rows'] = $num_rows;
    $config['per_page'] = 9;
    $config['uri_segment'] = 2;
    $this->pagination->initialize($config);

    $data['pagination'] = $this->pagination->create_links();

    $data['offset'] = $offset;

    $page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
    $offset = $page == 0 ? 0 : ($page - 1) * $config["per_page"];

    // memanggil method di model
    $data['produk'] = $this->cart_model->retrieve_products($config['per_page'], $page);

    $this->load->view('user/templates/header', $data);
    $this->load->view('user/templates/navigation', $data);
    $this->load->view('user/pages/' . $page, $data);
    $this->load->view('user/templates/footer', $data);
}

这是我的模特

class Cart_model extends CI_Model {
// Our Cart_model class extends the Model class
// Function to retrieve an array with all product information
function retrieve_products($num, $offset) {
    $query = $this->db->get('produk', $num, $offset); // Select the table products
    return $query->result_array(); // Return the results in a array.
}

}

【问题讨论】:

  • 您的起始 url 错误 &=9 不是有效的 url 参数,第一个应该以 ? 开头,后跟一个识别键。 shop?offset=9。但在这种情况下,Alghi Fari 回答了问题,您要查找的网址是 page/shop/9
  • 你用偏移量检查什么??
  • 那么,我该怎么办?

标签: php twitter-bootstrap codeigniter pagination


【解决方案1】:

网址有误:

网址是:http://localhost/sjpro/page/shop&=9

真:http://localhost/sjpro/page/shop/9

从函数

public function shop($page = 'shop', $offset = 0)

【讨论】:

    【解决方案2】:

    在控制器中

    $config['base_url'] = base_url() . "page/shop";
    $config['total_rows'] = $num_rows;
    $config['per_page'] = 9;
    $config['uri_segment'] = 2;
    $limit =  $config['per_page'];
    
    // boostrap Pagination. Just import boostrap. This will take CSS code automatically
    $config['full_tag_open'] = '<ul class="pagination">';
    $config['full_tag_close'] = '</ul>';
    $config['first_link'] = false;
    $config['last_link'] = false;
    $config['first_tag_open'] = '<li>';
    $config['first_tag_close'] = '</li>';
    $config['prev_link'] = '&laquo';
    $config['prev_tag_open'] = '<li class="prev">';
    $config['prev_tag_close'] = '</li>';
    $config['next_link'] = '&raquo';
    $config['next_tag_open'] = '<li>';
    $config['next_tag_close'] = '</li>';
    $config['last_tag_open'] = '<li>';
    $config['last_tag_close'] = '</li>';
    $config['cur_tag_open'] = '<li class="active"><a href="#">';
    $config['cur_tag_close'] = '</a></li>';
    $config['num_tag_open'] = '<li>';
    $config['num_tag_close'] = '</li>';
    
    $this->pagination->initialize($config);
    
    $page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
    
    $data['links'] = $this->pagination->create_links();
    
    $data['produk'] = $this->cart_model->retrieve_products($limit, $page);
    
    $this->load->view('user/templates/header', $data);
    $this->load->view('user/templates/navigation', $data);
    $this->load->view('user/pages'. $data);
    $this->load->view('user/templates/footer', $data);
    

    在模型中

    class Cart_model extends CI_Model
    {
        public function retrieve_products($limit , $page)
        {
            $query = $this->db->query("SELECT * FROM produk LIMIT $page,$limit ");
            $result = $query->result_array();
            return $result;
        }
    }
    

    可见

    foreach ( $produk as $new_produk )
    {
      //This should be your data base fields
        echo $new_produk['database field1'];
        echo $new_produk['database field2'];
    }
    

    主 div 的结尾只是回显链接(使用 boostrap 样式的分页)

        <?$php    
        echo $links;//This create your pagination.
        ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-01
      • 2016-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      • 2013-11-17
      相关资源
      最近更新 更多