Codeigniter:带分页的URL帮助程序

时间:2016-02-13 11:56:48

标签: php codeigniter url-rewriting pagination

我遇到了uri_string()的问题以及与codeigniter的分页

我的代码是:

<?php echo anchor(site_url(uri_string().'/'.$item->url_product),$item->name_product);?>

我当前的网址(第2页,$config['per_page'] = 3):

http://localhost:8080/ci/index.php/product/phone/page/3/

运行代码并获取产品URL的结果(因为我使用了分页):

http://localhost:8080/ci/index.php/product/phone/page/3/lumia_950_xl

但是,我想要像这样核心这个网址:

http://localhost:8080/ci/index.php/product/phone/lumia_950_xl

我希望在输出中删除page/3/。我该如何解决?

现在在我脑海里

(if any 'page/3/' string at site_url())
{
   delete 'page/3/';
}

但是我认为还有另一种更简单的方法,而不是像那样使用if语句,有人吗?感谢

好吧最后我使用了替换语句,因为没有人在这里给出点

$url_produk = uri_string();
$patern_url[0] = '(\/page\/\d)';
$patern_url[1] = '(\/page)';
$url_produk = preg_replace($patern_url, '', $url_produk);

1 个答案:

答案 0 :(得分:0)

您可以参考此代码。 这是我在Codeigniter中完成的分页。

public function managecategory() {
        $this->load->helper(array('form', 'url'));
        $this->load->view('moderator/templates/header');
        $this->load->view('moderator/templates/sidebar');
        $parent = '0';
        $data['catdata'] = $this->b2bcategory_model->form_select($parent);
        $this->load->library('pagination');
        // bootstrap style for maintaining pagination
        $config = array();
        $config["base_url"] = base_url() . "moderator/B2BCategory/managecategory";
        $config["total_rows"] = $this->b2bcategory_model->record_count();
        $config["per_page"] = 20;
        $config["uri_segment"] = 4;
        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        $config['first_link'] = 'first';
        $config['last_link'] = 'last';
        $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(4)) ? $this->uri->segment(4) : 0;
        $data["results"] = $this->b2bcategory_model->fetch_data($config["per_page"], $page);
        $data["links"] = $this->pagination->create_links();
        $this->load->view('moderator/managecategory', $data);
        $this->load->view('moderator/templates/footer');
    }