Masum
Masum

Reputation: 407

joomla 2.5 module retrieve data from database

i want to retrieve data from database in my module page.for that my helloworld.php put the code

// Get a database object
$db = JFactory::getDbo();

$query = $db->getQuery(true);
$query->select('id, description');
$query->from('#__banners');

// sets up a database query for later execution
$db->setQuery($query);

// fetch result as an object list
$result = $db->loadObjectList();

now i want to show the results in default.php. but when echo $result in default.php it dose not show anything. how to i show the result? how to i get data from #__banners table?

Upvotes: 0

Views: 1024

Answers (2)

tttpapi
tttpapi

Reputation: 887

You have to load the results from the model method in view.html.php.

In view.html.php

function display($tpl = null) {
  $model = JModelLegacy::getInstance('ModelName', 'FrontendModel'); //(or BackendModel)
  $variable = $model->getNameOfModelMethod();

  $this->assignRef('variable', $variable);
 }

And in default.php just call $this->variable.

Upvotes: 0

Lodder
Lodder

Reputation: 19733

$db->loadObjectList() returns an array which you can't echo. You can create a foreach loop like so:

foreach ( $result as $row ) {
    echo $row->description;
}

Upvotes: 1

Related Questions