1
votes

I have successfully implemented the pagination box in a component front end listing template. however, when i try to set the limit of listing items, it won't work, i wonder what is missed out.

in the model

  var $_total = null;

  /**
   * Pagination object
   * @var object
   */
  var $_pagination = null;


  function __construct(){

    parent::__construct();

    $mainframe = JFactory::getApplication();

    // Get pagination request variables
    $limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
    $limitstart = JRequest::getVar('limitstart', 0, '', 'int');

    // In case limit has been changed, adjust it
    $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);

    $this->setState('limit', $limit);
    $this->setState('limitstart', $limitstart);
  }


     function _buildQuery(){
         $query = ' SELECT * '
                 . ' FROM #__event '
                 .'Where published = 1'
            ;

         return $query;
    }

      function getData() {
    // if data hasn't already been obtained, load it
            if (empty($this->_data)) {
            $query = $this->_buildQuery();
            $this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));    
            }
            return $this->_data;
      }


    function getTotal(){
    // Load the content if it doesn't already exist
        if (empty($this->_total)) {
        $query = $this->_buildQuery();
        $this->_total = $this->_getListCount($query);       
        }
        return $this->_total;
     }

    function getPagination(){
     // Load the content if it doesn't already exist
        if (empty($this->_pagination)) {
        jimport('joomla.html.pagination');
        $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
        }
        return $this->_pagination;
    }

in the views/view.html.php (full version of this document)

 class EventViewListing extends JViewLegacy
{ 
// Overwriting JView display method
function display($tpl = null) 
{

            $model= & JModelLegacy::getInstance('Event','EventModel');
            $pagination = $model->getPagination();
            $this->assignRef('pagination',   $pagination);



  $JDoc =& JFactory::getDocument();
  $db = JFactory::getDBO();

  $sql = "SELECT * FROM #__event WHERE published = 1 ORDER BY id DESC";
  $db->setQuery($sql);
  $rows = $db->loadObjectList();


  $sql2 = "SELECT * FROM #__user_usergroup_map WHERE group_id = 5 or group_id = 8";
  $db->setQuery($sql2);
  $rows2 = $db->loadObjectList();


   $this->assignRef('rows',$rows);
   $this->assignRef('rows2',$rows2);


 //  $JDoc->setTitle('  ');

    // Display the view
    parent::display($tpl);
}
}

in the default.php

<form action="<?php echo JRoute::_('index.php?option=com_event'); ?>" method="post" name="adminForm">


<?php echo $this->pagination->getListFooter(); ?>
<input type="submit" name="submit" value="GO!"  /> 
</form>

hope someone could help thank you!

1
Can you try to echo $this->getState('limitstart'); die(); in your getData() method? I wonder if it gets the correct value.di3sel
What version of Joomla are you using?David Fritsch
Joomla 3.2, i am building my own componentSoya
this is the url, index.php?option=com_event&limitstart=0, i can select the limit number and click on 'go' to update the limit, but the listing item amount won't change, i guest the query may be wrong.Soya
So is the pagination showing up wrong or are the wrong number of items displayed on the page or can you not navigate to the second page?David Fritsch

1 Answers

4
votes

The limits need to also be used in the queries to limit the number of records that are displayed to the page that you are on. Typically this can be done in the setQuery function, which allows a second and third parameter to set the limit size and start position.

  $sql = "SELECT * FROM #__event WHERE published = 1 ORDER BY id DESC";
  $db->setQuery($sql, $model->getState('limitstart'), $model->getState('limit'));
  $rows = $db->loadObjectList();


  // I'm not sure what this query is for, but since it probably isn't supposed to be limited to a set number of items, don't update it's setQuery call.
  $sql2 = "SELECT * FROM #__user_usergroup_map WHERE group_id = 5 or group_id = 8";
  $db->setQuery($sql2);
  $rows2 = $db->loadObjectList();

I think that fixes the problem that you are having.


That being said, you have a host of minor issues that are making this a lot harder for you or just using outdated practices:

 $limitstart = JRequest::getVar('limitstart', 0, '', 'int');

Using JRequest::getVar() is deprecated and likely to be removed in future versions of Joomla. Instead use this:

 $limitstart = JFactory::getApplication()->input->get('limitstart', 0, 'INT');

Note that the parameters have changed slightly. This uses a different class to parse input to the application.

$this->assignRef('rows',$rows);

The above is unnecessary anymore (was only needed back in PHP4 from what I understand). Instead just do $this->rows = $rows;

Finally, the big overall issue is that you aren't really using Joomla's help.

Your model should just be extending from the class JModelList since you are trying to create a list of events. If you extend from that class and name your functions properly, Joomla will do most of the work:

  1. Rename _buildQuery to getListQuery.

  2. Pretty much delete every other function in your model, since Joomla has all of them in JModelList doing basically the same things.

  3. Update your view to this:

    class EventViewListing extends JViewLegacy
    { 
    // Overwriting JView display method
    function display($tpl = null) 
    {
    
      $JDoc = JFactory::getDocument();
      $db = JFactory::getDBO();
    
      $this->pagination = $this->get('Pagination');
      $this->rows = $this->get('Items');
    
      $sql2 = "SELECT * FROM #__user_usergroup_map WHERE group_id = 5 or group_id = 8";
      $db->setQuery($sql2);
      $this->rows2 = $db->loadObjectList();
    
      //  $JDoc->setTitle('  '); 
    
         // Display the view
         parent::display($tpl);
     }
     }
    

$this->get() in the JViewLegacy class will call the model (with the same name as the view) and run the method of that model that starts with get followed by whatever word is in the function, so $this->get('Pagination') calls the models getPagination function.

And again, all of the functions that you are adding in the model exist already in libraries/legacy/model/list.php, so just use them!