【问题标题】:pagination with codeigniter, other then index function使用 codeigniter 进行分页,除索引功能外
【发布时间】:2012-12-01 01:00:33
【问题描述】:

我已将 rout 设置为

$route['dog/(:any)']        = "dog/index/$1";  /// for single dog info
$route['dog/list']              = "dog/listing";   /// for dog list, display all dogs.
$route['dog/list/(:num)']       = "dog/listing/$1"; /// for pagination

单狗网址类似于 dog/dogName-4.html

我的控制器是

public function index()
{
    $dogInfo = $this->uri->segment(2);

    if ($dogInfo != "")
    {
        $dogDetails = explode('-', $dogInfo);

        $this->load->view('common/header',$header);
        $this->load->view('dog/dog_info', $content);
        $this->load->view('common/footer', $footer);
    }
    else
        redirect('welcome', 'location', 301);
}
public function listing()
{
    $this->load->library("pagination");
    $breed  = $this->input->get('breed');
    $gender = $this->input->get('gender');
    $state  = $this->input->get('state');
    $seller = $this->input->get('seller');

    $config = array();
    $config["base_url"] = base_url() . "dog/list/";
    $config["total_rows"] = $this->dogs->get_dog_list_count($breed, $gender, $state, $seller);
    $config["per_page"] = 5;
    $config["uri_segment"] = 2;
    $config['use_page_numbers'] = TRUE;
    $this->pagination->initialize($config);
    $page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
    $content['details'] = $this->dogs->get_dog_list($config["per_page"], $page,$breed, $gender, $state, $seller);

    $content['paginatonLinks'] = $this->pagination->create_links();
    $content['total_dogs'] = $config['total_rows'];
    $content['cur_page'] = $page + 1;
    $content['total_pages'] = ceil($config["total_rows"] / $config["per_page"]);

    $this->load->view('common/header');
    $this->load->view('dog/dog_list', $content);
    $this->load->view('common/footer', $footer);
}

控制器的索引功能是显示一条狗的信息,列表功能是针对所有狗的,

我必须为列表功能设置分页我确实为分页设置了所有必需的变量,分页也显示结果

找到 [134] 个广告 :: 第 1 页,共 27 页 1 2 3 > 最后 ›

但是当我点击页面上的分页 1 2 3 else 时,它​​会将我带到索引页面。

我需要在控制器的列表功能上。请任何人帮助我。


这是更新的代码

溃败代码是

$route['dog/list']      = "dog/listing";
$route['dog/list/(:num)']       = "dog/listing/$1";
$route['dog/(:any)']        = "dog/index/$1";

这是更新后的控制器代码

    $config = array();
    $config["base_url"] = base_url() . "dog/list/";
    $config['suffix'] = '?'.http_build_query($_GET, '', "&");
    $config["total_rows"] = $this->dogs->get_dog_list_count($breed, $gender, $state, $seller);
    $config["uri_segment"] = 3;

    $config["per_page"] = 5;
    $config['use_page_numbers'] = TRUE;
    $config['full_tag_open'] = '<div class="pagination">';
    $config['full_tag_close'] = '</div>';
    $config['cur_tag_open'] = '<a href="#" class="page_selected">';
    $config['cur_tag_close'] = '</a>';
    $this->pagination->initialize($config);
    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
    $this->load->model('DogsListing_model', 'dogs');
    $this->load->model('Breed_model', 'breeds');
    $content['breeds']  = $this->breeds->get_all_id_title();
    $content['details'] = $this->dogs->get_dog_list($config["per_page"], $page,$breed, $gender, $state, $seller);
    $content['search']  = array('breed' => $breed, 'gender' => $gender, 'state' => $state, 'seller' => $seller);
    $content['paginatonLinks'] = $this->pagination->create_links();
    $content['total_dogs'] = $config['total_rows'];
    $content['cur_page'] = $page + 1;
    $content['total_pages'] = ceil($config["total_rows"] / $config["per_page"]);

【问题讨论】:

  • 我可以看到您使用第三个 uri 段作为页码。所以它不应该是 $this->uri->segment(2) 而是 $this->uri->segment(3) 和 $config["uri_segment"] = 3; ???
  • 如果你的 url 以 .html 结尾,那么你可能有一些 htaccess 重写。这也可能会搞砸你......
  • 我需要知道为什么分页链接会转到控制器的索引功能。以及我可以进行哪些更改以使分页链接保留在控制器的同一页面或列表功能上。如果您想访问工作站点,这里是链接,link
  • @Jtheman 你是对的,在制作了这样的网址$this-&gt;uri-&gt;segment(3);$config["uri_segment"] = 3; 之后,我也重新排列了路线。这是工作。但现在我的下一个问题是我如何发送变量以及像/dog/list/?seller=29 这样的分页,我如何发送这个卖家。我有分页的基本网址,例如 $config["base_url"] = base_url() . "dog/list/"; 在此先感谢您的帮助。

标签: php codeigniter pagination


【解决方案1】:

我找到了我的问题的解决方案

首先我使用$this-&gt;uri-&gt;segment(3) and $config["uri_segment"] = 3; 我使用了错误的uri->segment 进行分页。这解决了我与分页相关的问题。

我的下一部分是我必须为卖家设置变量或使用分页搜索结果。例如我有 url doglist.html/?seller=29 我需要用这个分页嵌入分页。所以结果就像dog/list/3?breed=&amp;gender=female&amp;state=&amp;submit2=Fetch 我已经用这行代码$config['suffix'] = '?'.http_build_query($_GET, '', "&amp;"); 解决了这个问题现在我的变量分页工作正常。

感谢@jtheman 和@Venkat,但主要感谢@jtheman 的贡献。

【讨论】:

    【解决方案2】:

    如果您对pagination 有任何疑问,请查看我写的这个通用函数作为另一个问题的答案。

    【讨论】:

    • 是的,我的部分问题在@jtheman 的建议下得到了解决。分页部分已解决,但现在我想知道如何使用分页处理 url 变量,如何使 url 处理 url 变量。就像我有一个变量 doglist.html/?seller=29 而分页形式像这样的 url dog/list/3.
    • 请任何人都可以帮助我,为此我需要制作一个可用于分页的网址。
    • 这个网址很好用,/dog/list/2/?seller=29 我如何在分页中设置这个网址,而我的基本网址如$config["base_url"] = base_url() . "dog/list/"; 我如何编辑基本网址。
    • 你想要哪个网址。 /dog/list/2?seller=29 或 /dog/list/2
    • 谢谢@venkat 我想要这个网址http://urpuppy.com/dog/list/2?seller=29 它现在正在工作。但是当您单击 1 页时,它会重定向到主页,为什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-22
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    • 2018-04-23
    • 1970-01-01
    相关资源
    最近更新 更多