joomla3.0 Ordering and setting a limit


Example

Ordering the results and setting a limit can easily be achieved with 2 additional lines added to the chain, like so:

$db = JFactory::getDbo();

$query = $db->getQuery(true);
$query->select('*')
      ->from('#__users')
      ->where('username = '. $db->q('John'))
      ->order('id DESC')
      ->setLimit(15, 0);
 
$db->setQuery($query);

$results = $db->loadObjectList();

This orders the results by id in descending order showing only the first 15 records.

The setLimit() function takes 2 parameters. limit and offset. In the example above, we're only taking 15 records starting from the first row.