处理查询结果 - Quickbooks API PHP

Handling Query Results - Quickbooks API PHP

我是 Quickbooks 的新手 API,在处理从查询中提取的数据时遇到了一些问题。我想列出供应商名称。通常我会使用从 SQL 数据库中提取的 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 方法进行循环以生成所有供应商名称的 table?

您无法使用 [''] 访问对象的属性,除非 IPPVendor class 实现了 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 table 中显示这些详细信息,您可以在循环时使用 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;