【问题标题】:codeigniter pagination with dynamic url structure具有动态 url 结构的 codeigniter 分页
【发布时间】:2011-11-07 04:07:14
【问题描述】:

在我的 codeigniter 应用程序中,我有一个文章模块,它具有三个类别,

主要类别 次要类别 第三类 文章列表

现在第一页除了文章列表外显示了一个带有上述层次结构的手风琴,用户可以点击展开或直接点击一个类别名称。

因此,如果用户点击主要类别名称,所有文章将根据 codeigniter 的内置分页库分页列出。

同样,如果点击了第三类文章,则该文章将被单独列出。问题是所有文章列表都由一个函数处理,因此我有这样的路线。

$route['article/(:any)/(:any)/(:any)/(:any)'] = "articles/articles/show_article/$4";
$route['article/(:any)/(:any)/(:any)'] = "articles/articles/find_type/$3";
$route['article/(:any)/(:any)'] = "articles/articles/find_type/$2";
$route['article/(:any)'] = "articles/articles/find_type/$1";

而我的 find_type 方法是

function find_type($str)
    {
        $rawstr = $str;
        $str = deurl($str);
        $ch = 1;
        $id = 0;
        $query = $this->db->get_where("articles_primary",array("primary_name"=>$str));
        if($query->num_rows==1) {
          $ch = 1;
          $id = $query->row('id');
        }
        $query = $this->db->get_where("articles_secondary",array("secondary_name"=>$str));
        if($query->num_rows==1) {
          $ch = 2;
          $id = $query->row('id');
        }
        $query = $this->db->get_where("articles_tertiary",array("tertiary_name"=>$str));
        if($query->num_rows==1) {
          $ch = 3;
          $id = $query->row('id');
        }
        $query = $this->db->get_where("articles_list",array("url_title"=>$rawstr));
        if($query->num_rows==1) {
          $ch = 4;
        }
        switch ($ch)
        {
            case 1:
              $this->show_articles_list($id,"primary",$rawstr);
              break;
            case 2:
              $this->show_articles_list($id,"secondary",$rawstr);
              break;
            case 3:
              $this->show_articles_list($id,"tertiary",$rawstr);
              break;
            case 4:
              $this->show_article($rawstr);
              break;
        }
    }

最后我的文章列表是

function show_articles_list($id,$tbl,$rawstr)
    {
        $query = $this->db->get_where("articles_list",array($tbl."_id"=>$id));

        $this->load->library('pagination');
        $config['base_url'] = current_url();
        $config['total_rows'] = $query->num_rows;
        $config['per_page'] = 9;
        $this->pagination->initialize($config);
        $page = $this->uri->segment(3);

        if($page=="") { $page = 0; }
        $this->db->limit(9,$page);
        $data["query"] = $this->db->get_where("articles_list",array($tbl."_id"=>$id));

        $this->template->write_view('maincontent', 'articles/articles_list',$data);
        //$this->template->write_view('subcontent', 'articles/related_articles',$data);
        $this->template->write("title","Laws",true);
        $this->template->write_view('rightcontent','general/include/query_document_tab');
        $this->template->render();
    }

如您所见,路线基于 uri 段,因此分页不起作用。无论如何我可以调整分页以使用我当前的设置吗?

【问题讨论】:

    标签: php codeigniter pagination


    【解决方案1】:

    如何在函数中添加另一个参数并相应地更改路由文件:

    function find_type($str, $page=0){
    ...
    $this->show_articles_list($id,"primary",$rawstr, $page);
    etc...
    ...
    }
    
    function show_articles_list($id,$tbl,$rawstr,$page)...
    

    路线:

    $route['article/(:any)/(:any)/(:any)/(:any)'] = "articles/articles/show_article/$4/4";
    $route['article/(:any)/(:any)/(:any)'] = "articles/articles/find_type/$3/3";
    $route['article/(:any)/(:any)'] = "articles/articles/find_type/$2/2";
    $route['article/(:any)'] = "articles/articles/find_type/$1/1";
    

    您将使用该参数来表示用于分页的 uri 段。

    你可以使用$this->uri->total_segments()

    【讨论】:

      猜你喜欢
      • 2019-07-19
      • 1970-01-01
      • 2011-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多