从数据库中获取数据我们使用代码
public static function getdb($params)
{
// Get a database object
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__categories');
// sets up a database query for later execution
$db->setQuery($query);
// fetch result as an object list
$result = $db->loadObjectList();
foreach ( $result as $row ) {
echo "$row->extension .<br>";
}
}
我的问题是如何在tmpl / default.php中使用这个foreach循环?然后wat将是我的helper.php代码?
foreach ( $result as $row ) {
echo "$row->extension .<br>";
}
如果我将这个foreach循环用于default.php那么它对我来说会更好。请帮忙
答案 0 :(得分:1)
<强> helper.php 强>
public static function getdb($params) {
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__categories');
$db->setQuery($query);
$result = $db->loadObjectList();
return $result;
}
<强>如default.php:强>
//call the function from the helper.php
$result = modHelloWorldHelper::getdb($params);
//display the results
foreach ( $result as $row ) {
echo $row->extension . "<br>";
}