Get Single Item in Front End

The view asks the model for the item. In the model, you need the id of the item for querying the database. You can get this id from the Joomla system URL and store it as a state variable in the populateState(). You can also load the global parameters in the state variable.

Then, you can use this id in the getItem() to query the database to get the required item.

Step 1: View File


class HelloWorldViewHelloWorld extends JViewLegacy

function display($tpl = null)
// Assign data to the view
$this->item = $this->get('Item');

// Check for errors.
if (count($errors = $this->get('Errors')))
JLog::add(implode('<br />', $errors), JLog::WARNING, 'jerror');

return false;

// Display the view

Step 2: Model File


class HelloWorldModelHelloWorld extends JModelItem

protected $item;

protected function populateState()
// Get the message id
$jinput = JFactory::getApplication()->input;
$id = $jinput->get('id', 1, 'INT');
$this->setState('', $id);

// Load the parameters.
$this->setState('params', JFactory::getApplication()->getParams());

public function getItem()
if (!isset($this->item))
$id = $this->getState('');
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('h.greeting, h.params, c.title as category')
->from('#__helloworld as h')
->leftJoin('#__categories as c ON')
->where('' . (int)$id);

if ($this->item = $db->loadObject())
// Load the JSON string
$params = new JRegistry;
$params->loadString($this->item->params, 'JSON');
$this->item->params = $params;

// Merge global params with item params
$params = clone $this->getState('params');
$this->item->params = $params;
return $this->item;

Step 3: Layout File

The layout can now display the item.


<h3><?php echo $this->item->greeting ; ?></h3>


Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive