-1

I am trying create an invoice using Xero accounting software's API (php). I can create an invoice and add the products without any issues if I use the below code (excerpt):

$lineitem1 = new XeroAPI\XeroPHP\Models\Accounting\LineItem;
$lineitem1
->setItemCode(MODEL-REF1)
->setQuantity(10)
->setTaxType(NONE);
$arr_lineitem = [];
array_push($arr_lineitem, $lineitem1);

$lineitem2 = new XeroAPI\XeroPHP\Models\Accounting\LineItem;
$lineitem2
->setItemCode(MODEL-REF2)
->setQuantity(20)
->setTaxType(NONE);   
$arr_lineitem = [];
array_push($arr_lineitem, $lineitem2);

This works without any issues and will create two products on the invoice (MODEL-REF1 and MODEL-REF2) with their associated quantities and tax types.

The problem starts when I try to populate these details from a form from the previous page (since there could be 1 or 100+ different products on each invoice). The form will pass on the products model item code etc with a unique id (auto incrementing by 1 shown by"X" in the example below) and will also pass on the total amount of product lines purchased.

e.g. the form for the above would be:

<input type="text" id="ItemCode**X**" name="ItemCode**X**" value="MODEL-REF1">
<input type="text" id="Quantity**X**" name="Quantity**X**" value="10">
<input type="text" id="TaxType**X**" name="TaxType**X**" value="NONE">

<input type="text" id="ItemCode**X**" name="ItemCode**X**" value="MODEL-REF2">
<input type="text" id="Quantity**X**" name="Quantity**X**" value="20">
<input type="text" id="TaxType**X**" name="TaxType**X**" value="NONE">

<input type="text" id="TotalProductsInLoop" name="TotalProductsInLoop" value="2">

As such, I thought I could then loop through the values as follows on the script API side (and in turn submit the products to xero) using the below:

for ($i=1; $i<=$TotalProductsInLoop; $i++) {
   
    $ItemCode = $_POST['ItemCode'.$i];
    $Quantity = $_POST['Quantity'.$i];
    $TaxType = $_POST['TaxType'.$i];

    $lineitem . $i = new XeroAPI\XeroPHP\Models\Accounting\LineItem;
    $lineitem . $i
    ->setItemCode($ItemCode)
    ->setQuantity($Quantity)
    ->setTaxType($TaxType);
    
    $arr_lineitem = [];
    array_push($arr_lineitem, $lineitem . $i);
    
}

However, this just submits the last product in the loop e.g. MODEL-REF2 and not the first product of the others to the invoice.

Can any one see what I'm missing here?

I'm assuming it's something to do with using a counting loop in an array??

1
  • 1
    You overwrite your array in each iteration: $arr_lineitem = [];. The definition should come before the loop. Commented Dec 13, 2020 at 21:31

1 Answer 1

1
  1. I highly doubt that the following lines execute without any warnings or errors:
    $lineitem . $i = new XeroAPI\XeroPHP\Models\Accounting\LineItem;
    $lineitem . $i
    ->setItemCode($ItemCode)
    ->setQuantity($Quantity)
    ->setTaxType($TaxType);

This ($lineitem . $i) is standard string concatenation, numbered variables cannot be created like this.

In fact, you do not need numbered variables, you can reuse a single variable in the loop.

  1. You are resetting the result container array ($arr_lineitem) in every loop step. So it always will contain only the latest value.

The correct code should look more like this:

$arr_lineitem = [];
for ($i=1; $i<=$TotalProductsInLoop; $i++) {
   
    $ItemCode = $_POST['ItemCode'.$i];
    $Quantity = $_POST['Quantity'.$i];
    $TaxType = $_POST['TaxType'.$i];

    $lineitem = new XeroAPI\XeroPHP\Models\Accounting\LineItem;
    $lineitem
    ->setItemCode($ItemCode)
    ->setQuantity($Quantity)
    ->setTaxType($TaxType);
    
    array_push($arr_lineitem, $lineitem);
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.