Kohana 3 pagination

后端 未结 3 1293
既然无缘
既然无缘 2021-02-09 13:40

I\'m really lost on how pagination works in kohana 3. Is there a good example of pagination in Kohana 3 anywhere?

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-09 14:07

            // Get the total count of articles
        $count = $this
            ->_profil
            ->articles
            ->count_all();
    
        // Create the pagination object
        $pagi = Pagination::factory(array(
            'items_per_page'    =>  4,
            'total_items'       =>  $count,
        ));
    
        // Find actual articles
        $articles = $this->_profil
            ->articles
            ->join_categories()
            ->order_by('id','DESC')
            ->limit($pagi->items_per_page)
            ->offset($pagi->offset)
            ->find_all();
    

    and then in the View, you just do

    echo $pagi; // ofc, after passing the Pagination object to view
    

    What happens here is Pagination class using it's View's __toString() magic method to render html needed to display pagination. All pagination params can be modified when creating the object (passing appropriate keys to the array passed to factory() method in our case).

    Default key for pagination is "page" (query string), while you can modify that as well. Pagination also has a default config, which you can override by copying it to application/config folder.

    Enjoy using it :)

提交回复
热议问题