我的对象的print_r($ results)返回以下内容:
QueryResult Object
( [queryLocator] => [done] => 1 [records] =>
Array ( [0] => SObject Object ( [type] => Partner_Research__c [fields] => stdClass Object ( [Partner_Research_URL__c] => http://www.forms.com/184320?tfa_PRID=PR-0167 [Partner_Research_Name__c] => MM Sample Organization-TBR Partner 2011 [Id] => a0V80000003FwjjEAC ) [Id] => a0V80000003FwjjEAC )
[1] => SObject Object ( [type] => Partner_Research__c [fields] => stdClass Object ( [Partner_Research_URL__c] => http://www.forms.com/184320?tfa_PRID=PR-0170 [Partner_Research_Name__c] => Kansas City, Missouri Public Schools-TBR Partner 2011 [Id] => a0V80000003Fxf9EAC ) [Id] => a0V80000003Fxf9EAC )
[2] => SObject Object ( [type] => Partner_Research__c [fields] => stdClass Object ( [Partner_Research_URL__c] => http://www.forms.com/184320?tfa_PRID=PR-0169 [Partner_Research_Name__c] => Newark Public Schools-TBR Partner 2011 [Id] => a0V80000003FxQ2EAK ) [Id] => a0V80000003FxQ2EAK )
[3] => SObject Object ( [type] => Partner_Research__c [fields] => stdClass Object ( [Partner_Research_URL__c] => http://www.forms.com/184320?tfa_PRID=PR-0168 [Partner_Research_Name__c] => Breakthrough Charter Schools-TBR Partner 2011 [Id] => a0V80000003FxPxEAK ) [Id] => a0V80000003FxPxEAK )
[4] => SObject Object ( [type] => Partner_Research__c [fields] => stdClass Object ( [Partner_Research_URL__c] => http://www.forms.com/184320?tfa_PRID=PR-0004 [Partner_Research_Name__c] => KIPP, San Antonio-TBR Partner 2011 [Id] => a0V80000003FrBUEA0 ) [Id] => a0V80000003FrBUEA0 )
[5] => SObject Object ( [type] => Partner_Research__c [fields] => stdClass Object ( [Partner_Research_URL__c] => http://www.forms.com/184320?tfa_PRID=PR-0003 [Partner_Research_Name__c] => KIPP, Chicago - Gary-TBR Partner 2011 [Id] => a0V80000003FrB5EAK ) [Id] => a0V80000003FrB5EAK )
[6] => SObject Object ( [type] => Partner_Research__c [fields] => stdClass Object ( [Partner_Research_URL__c] => http://www.forms.com/184320?tfa_PRID=PR-0023 [Partner_Research_Name__c] => Harlem Village Academies-TBR Partner 2011 [Id] => a0V80000003FrEOEA0 ) [Id] => a0V80000003FrEOEA0 ) ) [size] => 7 )
我想使用类似于下面显示的循环来显示一系列结果,但foreach语句不正确。
foreach ($results as $result)
{
$id = $result[fields][Id];
$name = $result[fields][Partner_Research_Name__c];
$url = $result[fields][Partner_Research_URL__c];
$html .= "<tr><td>$id</td><td>$name</td><td>$url</td></tr>";
}
我需要对foreach语句进行哪些更改才能使我的代码重回正轨?
答案 0 :(得分:2)
在这样的问题中帮助我的是尝试在foreach循环中打印。例如,您可以对每个$ result执行var_dump并查看该结构是什么,它可以帮助确定如何继续。
答案 1 :(得分:2)
这是我最终如何做到这一点,感谢贡献者提供的帮助。
foreach ($results->records as $result)
{
$id = $result->fields->Id;
$name = $result->fields->Partner_Research_Name__c;
$url = $result->fields->Partner_Research_URL__c;
$html .= "<tr><td>$id</td><td>$name</td><td>$url</td></tr>";
}
答案 2 :(得分:1)
如果我正确读取您的样本,您的项目是std对象而不是数组。您可能需要像这样引用它们:
foreach ($results as $result)
{
$id = $result->fields->Id;
$name = $result->fields->Partner_Research_Name__c;
$url = $result->fields->Partner_Research_URL__c;
$html .= "<tr><td>$id</td><td>$name</td><td>$url</td></tr>";
}
上面的print_r示例难以阅读。如果你能提供缩进,那将会有所帮助。
答案 3 :(得分:1)
$results
是QueryResults对象的名称,我不知道代码中的真实姓名是什么。
foreach ($results->records as $result)
{
$id = $result->Id;
$name = $result->fields->Partner_Research_Name__c;
$url = $result->fields->Partner_Research_URL__c;
$html .= "<tr><td>$id</td><td>$name</td><td>$url</td></tr>";
}