codeigniter与动态url结构分页

时间:2011-11-07 04:07:14

标签: php codeigniter pagination

在我的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段,因此分页不起作用。无论如何,我可以调整分页以使用我当前的设置吗?

1 个答案:

答案 0 :(得分:0)

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

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段。

OR

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