Alan
Alan

Reputation: 13

Handling Query Results - Quickbooks API PHP

I am new to the Quickbooks API and am having some trouble doing something with the data I pull from a query. I am wanting to make a list of Vendor names. Normally I would use a while loop pulling from an SQL DB, however, I am not even able to print one result. I am sure it is something basic, however, I am not familiar with OOP queries and it seems that is similar to how Quickbooks runs their queries. I am able to connect and print an array with the below info.

Note: I just did MAXRESULTS 1 so that I could get it down to just displaying the DisplayName property.

// 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>";

With that I get the result of this:

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
    )
)

I have tried with no luck these:

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*/

First, how do I print just the DisplayName property?

Second, how would I do a loop via the OOP method to make a table of all Vendor names?

Upvotes: 0

Views: 524

Answers (1)

nice_dev
nice_dev

Reputation: 17805

You can't access properties of an object with [''] unless IPPVendor class implements ArrayAccess.

To access the properties while looping, you will need to use the -> syntax like below:

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

To display these details in a HTML table, you can make use of heredoc syntax while looping to make your code look clean.

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

Upvotes: 2

Related Questions