【问题标题】:CodeIgniter pagination Previous and active Links not WorkingCodeIgniter 分页以前和活动的链接不起作用
【发布时间】:2015-04-01 17:59:20
【问题描述】:

我有一个代码,点击分页链接功能正常,但如果我点击一个链接,该特定链接不会显示为活动链接。 我的代码如下

function viewcategory($name) {
    $this->load->database();
    $this->load->model('categorypostmod');

    $page = $this->uri->segment(5);
    $this->load->helper("url");
    $this->load->library('table');

    $this->load->model("site_model");
    $this->load->helper('form');
    $this->load->library('pagination');
    $config['base_url'] = "http://localhost/b3/index.php/CategoryPost/viewcategory/" . $name . "/page/";
    $config['per_page'] = 2;
    $config['num_links'] = 5;

    log_message('info', 'count is ' . $this->categorypostmod->getCategorycount($name));

    $config['total_rows'] = $this->categorypostmod->getCategorycount($name);
    $config['full_tag_open'] = '<ul class="pagination">';

    $config['full_tag_close'] = '</ul>';

    $config['use_page_numbers'] = TRUE;

    $config['next_link'] = 'Next';
    $config['next_tag_open'] = '<li class="next page">';
    $config['next_tag_close'] = '</li>';

    $config['prev_link'] = ' Previous';
    $config['prev_tag_open'] = '<li class="prev page">';
    $config['prev_tag_close'] = '</li>';

    $config['cur_tag_open'] = '<li class="active"><a href="">';
    $config['cur_tag_close'] = '</a></li>';

    $config['num_tag_open'] = '<li class="page">';
    $config['num_tag_close'] = '</li>';

    $data['query'] = $this->categorypostmod->getCategorypost($name, $config['per_page'], $this->uri->segment(5));
    $records = $this->db->get('post', $config['per_page'], $this->uri->segment(5));
    $this->pagination->initialize($config);

    $this->load->helper("url");
    $this->load->view('script');
    $this->load->view('head');
    $this->load->view('cat_content_list', $data);

    $this->load->view('aside');
    $this->load->view('bottom');
}

控制器名称是 categorypost,请帮助我,因为相同的代码正在其他控制器上工作,我想我错过了如何创建链接的工作,在我看来 CI 无法获取被点击的链接是一个活跃的,请帮我解决这个问题。

【问题讨论】:

  • 第一个错误:您正在加载$this-&gt;load-&gt;helper("url"); 两次。尝试将所有 load() 函数放在最顶部。
  • 去掉base_url末尾的斜杠/
  • 为什么你的网址$config['base_url'] = "http://localhost/b3/index.php/CategoryPost/viewcategory/" . $name . "/page/" 中有page参数?
  • @codeGodie 在视图类别中我将参数作为类别传递,例如。 html和我想在此基础上获取所有记录,所以我给了$name我在函数中使用的参数,如果我不给它,它将无法获取参数和搜索,我和你在一起吗? ?这就是你要问的?

标签: php codeigniter pagination codeigniter-url


【解决方案1】:

您错过了 uri_segment 的配置。添加此配置

$config['uri_segment'] = 5;//for you its 5

【讨论】:

  • 感谢 Shaiful,它起作用了 :),但你能告诉我它的用途是什么以及为什么它没有服用吗???
  • CI 分页尝试检测您当前的页码是什么,哪个段将是页码。uri_segment 告诉您的页码包含哪个段。如果您不告诉 CI 尝试检测自动这实际上是第 3 段。在你的情况下,第 3 段不包含页码,因此 CI 无法检测到页码。希望你明白我的意思
  • 感谢您的信息,我不知道。再次感谢
【解决方案2】:

尝试使用以下代码,让我知道你得到了什么结果:

function viewcategory($name) {
    $this->load->database();

    $this->load->helper("url");
    $this->load->helper('form');

    $this->load->library('table');
    $this->load->library('pagination');

    $this->load->model('categorypostmod');
    $this->load->model("site_model");

    $page = $this->uri->segment(5);
    $categCount = $this->categorypostmod->getCategorycount($name);

    $config['base_url'] = "http://localhost/b3/index.php/CategoryPost/viewcategory/" . $name . "/page/";
    $config['per_page'] = 2;
    $config['num_links'] = 5;

    log_message('info', 'count is ' . $categCount);

    $config['total_rows'] = $categCount;
    $config['full_tag_open'] = '<ul class="pagination">';

    $config['full_tag_close'] = '</ul>';

    $config['use_page_numbers'] = TRUE;

    $config['next_link'] = 'Next';
    $config['next_tag_open'] = '<li class="next page">';
    $config['next_tag_close'] = '</li>';

    $config['prev_link'] = ' Previous';
    $config['prev_tag_open'] = '<li class="prev page">';
    $config['prev_tag_close'] = '</li>';

    $config['cur_tag_open'] = '<li class="active"><a href="">';
    $config['cur_tag_close'] = '</a></li>';

    $config['num_tag_open'] = '<li class="page">';
    $config['num_tag_close'] = '</li>';

    $data['query'] = $this->categorypostmod->getCategorypost($name, $config['per_page'], $page);

    $records = $this->db->get('post', $config['per_page'], $page);

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


    $this->load->view('script');
    $this->load->view('head');
    $this->load->view('cat_content_list', $data);

    $this->load->view('aside');
    $this->load->view('bottom');
}

【讨论】:

  • 另外,你在哪里使用$records 变量?
  • 我现在实际上已经删除了它,我正在分享链接,您可以在其中看到它的工作原理youtu.be/G6p7jm08uMk 并分享模型。
  • 请查看模型和控制器的 github url github.com/Sanjay007/Blog,如果有帮助请告诉我。
  • 好的,明白了。所以看起来 .active 类没有被添加到您点击的 li 中,如果您使用 Web Developer 工具检查元素,您是否看到添加了活动类?
  • nope active class is no there ..but 添加 $config['uri_segment'] = 5;有效,但我不知道它是如何解决的?而在前一个中,我没有添加它,但它仍然有效..
【解决方案3】:
$config['uri_segment'] = 5 /*aca test values ​​( 1 or 2 or 3 or 4 or 5 or 6 or 7)*/

$data['query'] = $this->categorypostmod->getCategorypost($name, $config['per_page'], $this->uri->segment(5/*aca test values ​​( 1 or 2 or 3 or 4 or 5 or 6 or 7)*/) )

使用每个值尝试 uri(1,2,3,4)

您可以使用数字 1 或 2 或 3 或 4 或 5 或 6。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-03
    • 2014-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    相关资源
    最近更新 更多