Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to set pagination in Joomla with MVC workflow?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.34k
    Comment on it

    Hello Readers,

    The below blog shows the pagination in Joomla.

    When we use the pagination in Joomla, we require the model and the view to write the code.

    Suppose we have a component (com_example).Open the component file (com_example/models/items.php) write the below code.

     

    Step 1 : The model - an overview

    <?php 
    defined('_JEXEC') or die( 'Restricted access' ); 
    
    jimport('joomla.application.component.model'); 
    
    class ExampleModelItems extends JModel 
    { 
    var $_data = null;
    var $_total = null;
    var $_pagination = null;
    
        function __construct() {}
    
          function _loadData() {}
    
            function getData() {}
    
              function getTotal() {}
    
                function getPagination() {} 
    }
    ?>
    

    In the above code, we define some internal variable ($_data,$_total,$_pagination).

    1. $_data variable is an array containing the items that are fetched from the database.
    2. $_total variable contain the total number of records found into the database.
    3. $_pagination variable contain a getPagination() method.

    Now we can define the above function's one by one.

    1. The model constructor :

    function __construct() 
    { 
        parent::__construct(); 
    
        $application = JFactory::getApplication() ; 
    
        $config = JFactory::getConfig() ; 
    
          $limitstart = JRequest::getInt( 'limitstart', 0 );
          $limit = $application->getUserStateFromRequest( 'global.list.limit', 
          'limit', $config->getValue('config.list_limit'), 'int' );        
    
        $this->setState('limitstart', $limitstart); 
        $this->setState('limit', $limit); 
    }

     

    2. The model: Loading the data :

    function _loadData() 
    { 
        if (empty($this->_data) && empty($this->_total)) 
        { 
    
            $query = 'SELECT * FROM table_name' ;
            $this->_db->setQuery($query); 
    
            $this->_data = $this->_db->loadObjectList(); 
            $this->_total = count( $this->_data ) ; 
        } 
    
        return $this->_data ; 
    }

    the purpose of _loadData() method is to fetch the data from the database. In this function, first, check the empty array if array is empty then performs the database query to fetch the record.

     

    3. The model: Fetching the data

    function getData() 
    { 
        $this->_loadData() ; 
        
        $limitstart = $this->getState('limitstart');
        $limit = $this->getState('limit');
    
           return array_slice( $this->_data, $limitstart, $limit ); 
    }

    After, loading the _loadData() method and hold the items from the database we are ready to use this method outside the world. Within the getData() method we call _loadData() method. We use array_slice() (array function) to return only the items needed.

     

    4. The model: Pagination

    Before using the getPagination() function getTotal() method used. It is just a simple getter:

    function getTotal() 
    { 
        return $this->_total; 
    }
    
    function getPagination() 
    { 
        $this->_loadData() ;
    
        if (empty($this->_pagination)) 
        { 
            jimport('joomla.html.pagination'); 
    
            $limitstart = $this->getState('limitstart');
            $limit = $this->getState('limit');
            $total = $this->getTotal();
    
            $this->_pagination = new JPagination( $total, $limitstart, $limit ); 
        } 
    
        return $this->_pagination; 
    }

    Again _loadData() method call inside the getPagination() method to load the data from the database. JPagination involves the library-file through jimport() and creating the object with the right arguments.

     

    Step 2 : The view and the layout file :

    Component (com_example) view-name so the file "views/items/view.html.php" should contain a class named ExampleViewItems.

    <?php
    // File "views/items/view.html.php"
    defined('_JEXEC') or die( 'Restricted access' ); 
    
    jimport( 'joomla.application.component.view'); 
    
    class ExampleViewItems extends JView 
    { 
        function display($tpl = null) 
        { 
    
            $items = $this->get('Data');
            $this->assignRef( 'items', $items );
    
            $pagination = $this->get('Pagination') ;
            $this->assignRef( 'pagination', $pagination );
    
            parent::display($tpl);
        }
    }
    ?>

    In view, section fetch the data and pagination methods by using the $this->get('Data') and $this->get('Pagination').

    Now render the view inside File "views/items/default.php".

    <?php
    // File "views/items/tmpl/default.php"
    defined('_JEXEC') or die( 'Restricted access' );
    ?>
    
    <?php echo $this->pagination->getPagesCounter(); ?>
    
    <?php if( count( $this->items )) : ?>
        <table>
    
        <?php foreach( $this->items as $item ) : ?>
            <tr>
                <td><?php echo $item->title; ?></td>
            </tr>
        <?php endforeach; ?>
    
        </table>
    <?php endif; ?>
    
    <?php echo $this->pagination->getPagesLinks(); ?>

    In the above code, default.php view the JPagination-object that we fetched data from the model and contains 2 methods getPagesCounter() and getPagesLinks() methods. First, print the (Page count 1 of 2) and second prints the (navigation box).

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: