user982124
user982124

Reputation: 4610

PHP Loop Implementation

I'm working on a script that requests Invoices from an online service - 100 invoices are returned at a time. I've got the basic request working to return the first 100 invoices and loop through the results. Here's my current code:

// set pagination to page 1
$page = 1;

// Request Invoices
$response = $XeroOAuth->request('GET', $XeroOAuth->url('Invoices', 'core'), array( 'page' => $page ));


    if ($XeroOAuth->response['code'] == 200) {

        // Parse Invoices
        $invoices = $XeroOAuth->parseResponse($XeroOAuth->response['response'], $XeroOAuth->response['format']);

        // Get total found invoices
        $totalInvoices = count($invoices->Invoices[0]);


        // Loop through found invoices 
        if ($totalInvoices > 0) {

            foreach ($invoices->Invoices->Invoice as $invoice) {
                $invoiceNumber = $invoice->InvoiceNumber;
                // Process Invoice as required
            } 

        }

    } else {

        // Request Error
    }   

I need to extend this as follows:

I can't get the logic correct here to extend this with another loop to continue the requests until it gets less than 100 invoices

Upvotes: 0

Views: 56

Answers (2)

Tom
Tom

Reputation: 4292

Personally, I'd wrap the code you have in a function and then invoke it from within itself for as long as you need to.

fetchInvoices(1);

function fetchInvoices($page) {
    // Request Invoices
    $response = $XeroOAuth->request('GET', $XeroOAuth->url('Invoices', 'core'), array( 'page' => $page ));

    if ($XeroOAuth->response['code'] == 200) {

        // Parse Invoices
        $invoices = $XeroOAuth->parseResponse($XeroOAuth->response['response'], $XeroOAuth->response['format']);

        // Get total found invoices
    $totalInvoices = count($invoices->Invoices[0]);


        // Loop through found invoices 
        if ($totalInvoices > 0) {

            foreach ($invoices->Invoices->Invoice as $invoice) {
                $invoiceNumber = $invoice->InvoiceNumber;
                // Process Invoice as required
            } 
        }

        if($totalInvoices == 100) {
            fetchInvoices($page + 1)
        }
    } else {

        // Request Error
    }
}   

Upvotes: 1

Sayantan Das
Sayantan Das

Reputation: 1641

This should work:

<?php

$breakFlag = 0;

// set pagination to page 1
$page = 1;

do {

    // Request Invoices
    $response = $XeroOAuth->request('GET', $XeroOAuth->url('Invoices', 'core'), array( 'page' => $page ));

    if ($XeroOAuth->response['code'] == 200) {

        // Parse Invoices
        $invoices = $XeroOAuth->parseResponse($XeroOAuth->response['response'], $XeroOAuth->response['format']);

        // Get total found invoices
        $totalInvoices = count($invoices->Invoices[0]);

        if ($totalInvoices < 100) {
            $breakFlag = 1;
        }

        // Loop through found invoices 
        if ($totalInvoices > 0) {

            foreach ($invoices->Invoices->Invoice as $invoice) {
                $invoiceNumber = $invoice->InvoiceNumber;
                // Process Invoice as required
            } 

        }

        $page += 1;

    } else {

        // Request Error
    } 
} while($breakFlag == 0)

Upvotes: 1

Related Questions