【问题标题】:Handling Query Results - Quickbooks API PHP处理查询结果 - Quickbooks API PHP
【发布时间】:2019-11-24 04:14:49
【问题描述】:

我是 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 方法循环来制作所有 Vendor 名称的表?

【问题讨论】:

  • 由于 $entitiesarrayIPPVendor 对象,您可以使用 $entities[0] 检索索引 0 处的对象。这将表示标准对象变量,允许您使用 @ 987654328@或$entity = $entities[0]; $entity-&gt;DisplayName;
  • 这适用于单个对象。谢谢。

标签: php arrays oop quickbooks-online


【解决方案1】:

您不能使用[''] 访问对象的属性,除非IPPVendor 类实现ArrayAccess

要在循环时访问属性,您需要使用-&gt; 语法,如下所示:

$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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-08
    • 2019-03-18
    • 1970-01-01
    相关资源
    最近更新 更多