我有以下函数,目前在default.php文件中,稍后我将转移到helper.php
function getauthor($shouts, $i){
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*')
->from('#__users')
->where('name = '. $shouts[$i]->name);
$db->setQuery($query);
$rows = $db->loadObjectList();
$i=0;
foreach ($rows as $row){
$author[$i]->id = $row->id;
$author[$i]->name = $row->name;
$i++;
}
return $author;
}
基本上我想要做的是,打印$author[$i]->name
,但每次我尝试使用以下代码执行此操作时:
print stripslashes($author[$i]->name);
我收到以下错误:
Undefined variable: author in C:\wamp\www\Joomla25\modules\mod_xxx\tmpl\default.php on line 98
Trying to get property of non-object in C:\wamp\www\Joomla25\modules\mod_xxx\tmpl\default.php on line 98
Cannot redeclare getauthor() (previously declared in C:\wamp\www\Joomla25\modules\mod_xxx\tmpl\default.php:60) in C:\wamp\www\Joomla25\modules\mod_xxx\tmpl\default.php on line 60
有人能告诉我出错的地方以及如何打印$author[$i]->name
?
答案 0 :(得分:1)
您需要在您调用的地方定义变量author
:
print stripslashes($author[$i]->name);
在尽可能小的例子中:
$author = getauthor(....)
...
print stripslashes($author[$i]->name);