PhixFlow Help
Looping Statements
forEach()
PhixFlow provides the forEach() function to enable a script to loop over a set of statements for each entry in an Array or Recordset.
Expression | Result |
---|---|
| In this example, invoices is a Pipe which contains a Recordset of invoice details for a customer. invoices.amount plucks out just the amount Attribute for each invoice to create an Array of all the due amounts. tax.taxRate provides the tax rate to be applied to each invoice. The forEach() function sets $value to the first element in invoices.amount and then processes the script lines in the remainder of the forEach() function. Note that the script does not need to be enclosed in a do() function. When the script is completed, PhixFlow assigns the next value in invoices.amount to $value and repeats the script. This processing continues until the forEach() function has processed all items in invoices.amount. The final result should be that $tot contains the total amount due on the invoices and $tax contains the total tax value. |
n..m
If you just want to iterate through a loop a known number of times, PhixFlow provides the n..m syntax to create an Array which you can then iterate over.
For example, 1..100, creates an Array of 100 elements with entries [1, 2, 3, ..., 99, 100].
Expression | Result |
---|---|
| This simple example just adds up all the numbers from 1 to 10. |
continue()
There may be occasions inside a loop that you want to ignore the rest of the script and just move on to the next item in the Array. In this case use the continue() function.
Expression | Result |
---|---|
| This example extends the case above to only add up invoices where the customer owes some amount e.g. if a credit note is found with $value < 0 then that amount is not included in the total and the continue() function is used to move on to the next item without processing the remaining script lines. |
| Just to promote good scripting style, it is worth noting that the previous example was a slightly artificial way to write the expression but was done just to illustrate the use of continue(). The script here gives exactly the same result but is a little clearer. |
break()
There may also be occasions inside a loop that you no longer need to process the remaining items in the Array. In this case use the break() function.
Expression | Result |
---|---|
| This example loops over each $invoice for a customer and quits the loop when the first overdue invoice has been found. Note that if the invoices Pipe had been set up to order the data so that the oldest invoices were first, then this script identifies the oldest, overdue invoice. Please note that on leaving the loop, $invoice retains its value (this is also true in earlier examples). This example also shows that you can loop over Recordsets. For each iteration, $invoice is an individual Record within the Recordset referenced by the Pipe invoices. This enables you to look at all the attributes of the invoice within the loop. |
Please let us know if we could improve this page feedback@phixflow.com