我是Quickbooks API的新手,在处理从查询中提取的数据时遇到了一些麻烦。我想列出供应商名称。通常,我会使用从SQL DB中提取的while循环,但是,我什至无法打印一个结果。我确信这是基本的东西,但是,我对OOP查询并不熟悉,似乎与Quickbooks运行查询的方式类似。我可以使用以下信息连接并打印阵列。
注意:我只是做了MAXRESULTS 1,所以我可以将其简化为仅显示DisplayName属性。
// Run a query
$entities = $dataService->Query("Select * from Vendor MAXRESULTS 1");
$error = $dataService->getLastError();
if ($error) {
echo "The Status code is: " . $error->getHttpStatusCode() . "\n";
echo "The Helper message is: " . $error->getOAuthHelperError() . "\n";
echo "The Response message is: " . $error->getResponseBody() . "\n";
exit();
}
print "<pre>";
print_r($entities);
print "</pre>";
有了这个,我得到了这个结果:
Array
(
[0] => QuickBooksOnline\API\Data\IPPVendor Object
(
[IntuitId] =>
[Organization] =>
[Title] =>
[GivenName] =>
[MiddleName] =>
[FamilyName] =>
[Suffix] =>
[FullyQualifiedName] =>
[CompanyName] =>
[DisplayName] => Bob's Burger Joint
[PrintOnCheckName] => Bob's Burger Joint
[UserId] =>
[Active] => true
)
)
我没有运气就尝试过这些:
echo "Test 1: " . $entities->DisplayName;
echo "Test 2: " . $entities[0]['DisplayName'];
echo "Test 3: " . $entities[0][DisplayName];
echo "Test 4: " . $entities->0->DisplayName;
/*Start Test 5*/
$sql = 'Select * from Vendor MAXRESULTS 1';
foreach ($dataService->Query($sql) as $row) {
print $row['DisplayName'] . "\t";
print $row['PrintOnCheckName'] . "\t";
print $row['Active'] . "\n";
}
/*End Test 5*/
首先,我如何仅打印DisplayName属性?
其次,我将如何通过OOP方法进行循环以创建一个包含所有供应商名称的表?
除非使用['']
类实现IPPVendor
,否则无法使用ArrayAccess访问对象的属性。
要在循环时访问属性,您将需要使用如下所示的->
语法:
$sql = 'Select * from Vendor MAXRESULTS 1';
foreach ($dataService->Query($sql) as $row) {
echo $row->DisplayName . "\t";
echo $row->PrintOnCheckName . "\t";
echo $row->Active . "\n";
echo PHP_EOL; // to end the current line
}
要在HTML表中显示这些详细信息,可以在循环时使用heredoc语法以使代码看起来更干净。
$sql = 'Select * from Vendor MAXRESULTS 1';
$html = <<<html
<table border='1'>
<thead>
<tr>
<th>DIsplayName</th>
<th>PrintOnCheckName</th>
<th>Active</th>
</tr>
</thead>
<tbody>
html;
foreach($dataService->Query($sql) as $row) {
$html .= <<<html
<tr>
<td>$row->DisplayName</td>
<td>$row->PrintOnCheckName</td>
<td>$row->Active</td>
</tr>
html;
}
$html .= <<<html
</tbody>
</table>
html;
echo $html;