Codeigniter:分页,未找到2页

时间:2017-10-31 11:33:41

标签: php codeigniter codeigniter-3

我一直在努力解决这个问题一段时间,但它没有成功。 第二页显示404错误。如果有人能抽出时间审查代码,我们将不胜感激。我也在路线上工作过,但它仍然在2页上显示404错误。 这是 的控制器

public function __construct()
    {
        parent::__construct();
        $this->load->model('Products_model');
        $this->load->helper('form');
        $this->load->library('pagination');
    }

public function index($offset = 0) {
        //how many blogs will be shown in a page
        $limit = 3;
        $result = $this->Products_model->get_blogs($limit, $offset);
        $data['blog_list'] = $result['rows'];
        $data['num_results'] = $result['num_rows'];
        // load pagination library
        $config = array();
        $config['base_url'] = 'http://localhost/nwpgroup/nwp2/index.php';
        $config['total_rows'] = $data['num_results'];
        $config['per_page'] = $limit;
        //which uri segment indicates pagination number
        $config['uri_segment'] = 3;
        $config['use_page_numbers'] = TRUE;
        //max links on a page will be shown
        $config['num_links'] = 5;
        //various pagination configuration
        $config['full_tag_open'] = '<div class="pagination">';
        $config['full_tag_close'] = '</div>';
        $config['first_tag_open'] = '<span class="first">';
        $config['first_tag_close'] = '</span>';
        $config['first_link'] = '';
        $config['last_tag_open'] = '<span class="last">';
        $config['last_tag_close'] = '</span>';
        $config['last_link'] = '';
        $config['prev_tag_open'] = '<span class="prev">';
        $config['prev_tag_close'] = '</span>';
        $config['prev_link'] = '';
        $config['next_tag_open'] = '<span class="next">';
        $config['next_tag_close'] = '</span>';
        $config['next_link'] = '';
        $config['cur_tag_open'] = '<span class="current">';
        $config['cur_tag_close'] = '</span>';
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $this->load->view('products', $data);
    }

模型部分

public $products = 'products';


     public function get_blogs($limit, $offset) {
            if ($offset > 0) {
                $offset = ($offset - 1) * $limit;
            }
            $result['rows'] = $this->db->get($this->products, $limit, $offset);
            $result['num_rows'] = $this->db->count_all_results($this->products);
            return $result;
        }

查看。

<?php
        foreach ($blog_list->result() as $blog) {
            ?>
            <div class="post">
                <h2 class="title"><?php echo $blog->id; ?></h2>
                <p class="meta">
                    <?php
                    echo $blog->name;
                    ?>
                <div class="entry">
                    <p><?php echo $blog->price; ?></p>
                </div>
            </div>
        <?php
        }
        if (strlen($pagination)) {
            echo $pagination;
        }
    ?>

感谢您抽出宝贵时间查看此代码。

2 个答案:

答案 0 :(得分:2)

您在base_url中遇到分页问题

变化

$config['base_url'] = 'http://localhost/nwpgroup/nwp2/index.php';

$config['base_url'] = 'http://localhost/nwpgroup/nwp2/index.php/products/index/';

答案 1 :(得分:0)

分页中的基本URL指向控制器功能,在该功能中从模型中获取数据。

所以你必须将base_url设置为

 $config['base_url'] = site_url("products/index/");

site_url将使用index.php返回您的项目网址。