Home > Net >  Recursive function to API returning duplicate records
Recursive function to API returning duplicate records

Time:12-15

I can't see what I'm doing wrong, but I keep getting duplicate records when calling this recursive function.

The data is returned with keys to give the total records in the database (recsindb) and the total records returned in this call (recsonpage). I've written a class to query the API better ($this->site), and can add parameters to the request such as getting the page number and records per page:

  • $this->site->add('page-no', $page);
  • $this->site->add('no-of-records', 10);
public function syncTransactions($page = 1, $int = 0, $return = [])
{
    $this->site->add('page-no', $page);
    $this->site->add('no-of-records', 10);
    $result = $this->site->getData($this->site->api() . 'transactions/search.json', TRUE);
    foreach ($result as $data) {
        if (is_array($data)) {
            $return[$int]['transid'] = $data['customer_transaction.transid'];
            $return[$int]['description'] = $data['customer_transaction.description'];
            $return[$int]['sellingcurrencysymbol'] = $data['customer_transaction.description'];
            $return[$int]['customerid'] = $data['customer_transaction.customerid'];
            $return[$int]['sellingamount'] = $data['customer_transaction.sellingamount'];
            $return[$int]['type'] = $data['customer_transaction.type'];
            $return[$int]['key'] = $data['customer_transaction.key'];
            $return[$int]['transactiondate'] = date('Y-m-d h:i:s', $data['customer_transaction.transactiondate']);
        }
        $int  ;
    }
    
    if (
        ($result['recsindb'] >= ($result['recsonpage'] * $page)) &&
        ($result['recsonpage'] != 0)
    ) {
        $this->syncTransactions($page   1, $int   1, $return);
    }
    echo "<pre>" . print_r($return, TRUE) . "</pre>";
}

CodePudding user response:

I'm not sure about this but maybe you should try to set the values before calling the function.

Like this

$page  ;
$int  ;
$this->syncTransactions($page, $int, $return);

But again, i'm not so sure that this is the problem.

EDIT

I think i found it. Try this change

public function syncTransactions($page = 1, $int = 0, $return = [])
{
    $this->site->add('page-no', $page);
    $this->site->add('no-of-records', 10);
    $result = $this->site->getData($this->site->api() . 'transactions/search.json', TRUE);
    foreach ($result as $data) {
        if (is_array($data)) {
            $return[$int]['transid'] = $data['customer_transaction.transid'];
            $return[$int]['description'] = $data['customer_transaction.description'];
            $return[$int]['sellingcurrencysymbol'] = $data['customer_transaction.description'];
            $return[$int]['customerid'] = $data['customer_transaction.customerid'];
            $return[$int]['sellingamount'] = $data['customer_transaction.sellingamount'];
            $return[$int]['type'] = $data['customer_transaction.type'];
            $return[$int]['key'] = $data['customer_transaction.key'];
            $return[$int]['transactiondate'] = date('Y-m-d h:i:s', $data['customer_transaction.transactiondate']);
        }
$int  ;
    }
    
    if (
        ($result['recsindb'] >= ($result['recsonpage'] * $page)) &&
        ($result['recsonpage'] != 0)
    ) {
        return $this->syncTransactions( ($page   1), ($int   1), $return);
    }
    return $return;
}

// Suppose that you initiated the object
print_r($obj->syncTransactions());

The change is the return $this->syncTransactions instead of just calling the function. With that you can achieve a unified object of data without repeating the same structure 1 every time.

  •  Tags:  
  • php
  • Related