php - 回显函数的特定结果

标签 php mysql function joomla foreach

我有以下功能,目前在 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

最佳答案

你需要在调用的地方定义变量author:

print stripslashes($author[$i]->name);

在尽可能小的例子中:

$author = getauthor(....)
...
print stripslashes($author[$i]->name);

关于php - 回显函数的特定结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14184718/

相关文章:

php - 通过 PHP REST API 检索 JSON

php - 使用 $.ajax 和 php 的进度条

mysql - 使用 mysql 触发器在更新事件后备份行

mysql - Grails - 日期 0000 :00:00 00:00:00

mysql - 在 yii2 的 $query 中添加 beetween 选项

PHP 闭包和回调

php - Symfony 2 创建具有 2 个属性的实体表单字段

javascript - 将 Base64/BLOB 图像数据从 PHP 发送到 Javascript

c++ - 将 3D 指针数组传递给函数

android - 如何在android项目的每个 Activity 中调用一个函数?