Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Pagination sample ?
  • Can anyone show me an example of pagination in fuelphp 1.4?
    what I need is to list data from a table and pagenate.

    Thx
  • ilNotturnoilNotturno
    Accepted Answer
    Here is the common method I use for all pagination in my projects:

        public function action_index() 
        {
            // Create a pagination 
            $pagination = \Pagination::forge('pagination', array(
                'pagination_url' => \Uri::base(false) . 'blog/index/',
                'total_items' => Model_Content::find()->where('published', 1)->count(),
                'per_page' => 10,
                'uri_segment' => 3,
                'num_links' => 5,
            ));
            
            // Get contents based on pagination
            $contents = Model_Content::find()
                ->where('published', 1)
                ->order_by('id', 'desc')
                ->offset($pagination->offset)
                ->limit($pagination->per_page)
                ->get();
            
            // Set templates variables
            $this->data['template_values']['subtitle'] = 'Blog';
            $this->data['partials']['pagination'] = $pagination->render();
            $this->data['page_values']['contents']= $contents;
            
            \Theme::instance()->set_partial('content', 'public/nvblog/index');
        }

    All links in this section are in the form "www.site.com/blog/index/page".
  • Hi, thanks.

    How to display the data and paginator on the view ? 

    Sorry for so many questions I'm learning fuelphp.

    Thx
  • echo \Pagination::create_links();

    if you use the default instance, or pass the instance ($pagination in the above example) to the view, and use

    echo $pagination->render();
  • My controller:

    public function action_index() 
    {
    $view = View::forge('empresa/index');
      // Create a pagination 
            $pagination = \Pagination::forge('pagination', array(
                'pagination_url' => \Uri::base(false) . 'empresa/index',
                'total_items' => Model_Empresa::find()->where('activa', 1)->count(),
                'per_page' => 10,
                'uri_segment' => 3,
                'num_links' => 5,
            ));
            
            // Get contents based on pagination
            $contents = Model_Empresa::find()
                ->where('activa', 1)
                ->order_by('id', 'desc')
                ->offset($pagination->offset)
                ->limit($pagination->per_page)
                ->get();
            
            // Set templates variables
            $this->data['template_values']['subtitle'] = 'Blog';
            $this->data['partials']['pagination'] = $pagination->render();
            $this->data['page_values']['contents']= $contents;
            
            Theme::instance()->set_partial('content', 'public/empresa/index');

    im get this error: 

    Fuel\Core\ThemeException [ Error ]: Theme "default" could not be found.

    I'm learning fuelphp

    what code I need to display data in view ? 

    Sorry for the inconvenience I hope someone can help me.
    Thank you.
  • That snippit of code requires you to setup a theme, which contains the views your going to use. Did you do that? He probably also has some means of passing $this->data to the theme, which isn't in this snippet.

    If you're new to Fuel it's probably better to follow the documenation and stick to using Controller_Template.




  • ilNotturnoilNotturno
    Accepted Answer
    Sorry, I posted it while at work, so I didn't modify the answer.
    RTj, if you are learning Fuel I suggest you to use the View and not the Theme, you can learn about it later.

    To do that follow this code:

    class Controller_Example extends Controller_Template
    {   
       public function action_index()
       {
            // Create a pagination
            $pagination = \Pagination::forge('pagination', array(
                'pagination_url' => \Uri::base(false) . 'blog/index/',
                'total_items' => Model_Content::find()->where('published', 1)->count(),
                'per_page' => 10,
                'uri_segment' => 3,
                'num_links' => 5,
            ));
           
            // Get contents based on pagination
            $contents = Model_Content::find()
                ->where('published', 1)
                ->order_by('id', 'desc')
                ->offset($pagination->offset)
                ->limit($pagination->per_page)
                ->get();
           
            // Set templates variables
            $this->template->content = View::forge('example/index', $contents);
        }
    }

    To understand how to call it from browser, I suggest you to read the doc.

  • All I need is to create a paginator for the displayed records from db.
    the controller and the view.
    I'm reading the documentation but it is very little, I do not understand how  to do this.

    Can someone show me an example of controller / view to the pager to do this.

    It would help me a lot to understand how to do this.

    I have fuelphp 1.4

    Thank you very much in advance.


  • I already posted that on December 7th.

    If you use the static interface, use

    echo \Pagination::create_links();

    in your view. If you use an instance, like in the example above, you have to pass that instance to your view (disable encoding when you do), and then in your view use

    echo $pagination->render();
  • Is solved.
    The files were corrupt
    My copy of fuel was corrupt.
    I have re-downloaded fuelphp and now it works perfectly.
    Thank you very much to all.
  • ilNotturnoilNotturno
    Accepted Answer
    You are welcome 

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion