类型为stdClass的对象的错误为数组?

时间:2018-02-24 11:37:24

标签: php

我收到了这个错误,这是我的代码我试图显示一个带有链接标题的表,但我试图使用分页,我收到了这个错误

foreach ($actor as $projects ) {
       echo '<tr><td><a href="'.
       base_url('show_project?id='.
       $projects['proj_id']).
       '">'.
       $projects['title'].
       '</td><td>'.
       $projects['abbrev'].
       '</td><td>'.
       date('F d, Y (h:i A)', strtotime($projects['date_uploaded'])).
       '</td><td>'.
       $projects['professor'].
       '</td></tr>'
       ;
    }

1 个答案:

答案 0 :(得分:0)

你的主要问题是你有一个对象而不是一个数组。要修复该对象,请$projects->title

我已经清理了代码,主要是使用"$object"解释的字符串。

// your data should be changed to something like foreach($actor->projects as 
$project) to make more sense
foreach ($actor as $project ) {
    echo generateTableRow($project);
    }

function generateTableRow($project)
{
    $projectUrl = base_url("show_project?id=$project->project_id");
    $date = date('F d, Y (h:i A)', strtotime($project->date_uploaded));

    $tableRow =  '<tr>';
    $tableRow .= "<td><a href=\"$projectUrl\">$project->title</td>"
    $tableRow .= "<td>$project->abbrev</td>";
    $tableRow .= "<td>$date</td>";
    $tableRow .= "<td>$project->professor</td>";
    $tableRow .= '</tr>';

    return $tableRow;
}