joomla 2.5模块:如何在tmpl / default.php中预先设置循环

时间:2013-11-27 15:44:31

标签: php sql database joomla module

在joomla模块中

从数据库中获取数据我们使用代码

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那么它对我来说会更好。请帮忙

1 个答案:

答案 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>";
}