Reputation: 407
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
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
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