foreach
Username:  
Password:
  > Home
> User Guide
v Reference
    > Introduction
    > CROFT
    > BEE Variables
    > BEE Syntax
    v BEE Commands
       > Variable Operations
       > Conditional
       v Loop
          o foreach
          o for
          o while
          o continue
          o break
       > Module Calling
       > Remote Calling
       > Authentication
       > Data Access
       > Socket
       > Special Functions
    > Database Operation
    > Content Management
    > Objects and Classes
    > Interface with others
    o Glossary
> Portal Object
> Development Guide


Shortcuts
sys Class
debug Class
Intrinsic Conversions
>> Reference >> BEE Commands >> Loop >> foreach <=  =>      <  1  >  
foreach – loop through a variable or a data access result

BEE Script:      foreach [maxiter=num] (source [as var]) statement;

or      foreach [maxiter=num] (source [as var])

                  { statement; ... }

 

BEE Tag:     <beeforeach source var=var [maxiter=num]>

                  tag

                  ...

            </beeforeach>

 

"Foreach" loop is commonly used to populate an HTML table.  For example, you can put a template data row within a "foreach" loop, such that in each iteration a data record is extracted for a display.

It is easier to have it done than said.

Example (Script Form):

database "customer" query="select * from Company

      where Balance > 1000";

display '<table>\n';

foreach ((db)customer as custrec)

{

      display '<tr>\n';

      display '<td>{custrec:CompanyName}</td>\n';

      display '<td>{custrec:ContactPerson}</td>\n';

      display '<td>${custrec:Balance}</td>\n';

      display '</tr>\n';

}

display '</table>\n';

 

Example (Tag Form):

<beedatabase "customer" query="select * from Company

      where Balance > 1000">

<table>

<beeforeach (db)customer var=custrec>

      <tr>

            <td>${custrec:CompanyName}</td>

            <td>${custrec:ContactPerson}</td>

            <td>$${custrec:Balance}</td>

      </tr>\n';

</beeforeach>

</table>

 

Parameters

foreach (the source) is the structure to loop through.  There are four types of source: var (default), csv, database, and dbtree.

(var)source

"var" type is the default if no source type is specified.  The variable name source is taken as the name of the array to loop through.  (If a single element is specified, only that element will be used and the loop will be executed only once.)

The elements of the array will be extracted, one in every iteration, and assigned to the "Loop Variable": "foreach:value" is the element value, "foreach:key" is the element key.  As a short hand, "foreach" (the default element of the loop variable) contains the same value as "foreach:value".

Example:

foreach (sys%auth)

      display "User's {foreach:key} is {foreach}<br>\n";

// Sample display:

//    User's username is jack

//    User's Name is Jack Lee

//    User's Tel is 98765432

 

The following BEE Variables will be made available within a "var" loop:
("foreach" is to be substituted by the Loop Variable name if one is explicitly specified.)

foreach:key

The current key (index) of the Loop Variable

foreach:value

The current value of the Loop Variable

foreach:

Same value as foreach:value

result%foreach:iteration

The iteration count.  It contains 1 in the first iteration and 2 for the second etc.

 

(csv)source

"csv" type is very similar to the "var" type.  The only difference is that instead of the usual {foreach:key}, {foreach:value} (or {foreach}), the Loop Variable will contain an array derived from the comma-delimited value list contained in the source element.  (The "key" is insignificant in this case.  If you're interested in its value, it is stored in result%foreach:key.)

The "csv" type is useful in processing a CSV file.

Example:

foreach ((csv)file%myFile) {

      display "Month: {foreach:#0}<br>\n";

      display "Income: {foreach:#1}<br>\n ";

      display "Expense: {foreach:#2}<br>\n ";

}

 

 

(database)dbobj  or  (db)dbobj

"database" type can be shortened to just "db".  The dbobj that follows is from a previous "database" command, as shown in the example.  (Please see the "database" command for more details about the data access mechanism.)

Data records will be fetched from the last data access via the database object (dbobj), one record in every iteration, and assigned to the "Loop Variable".  The fields are assigned to individual elements, indexed by the field name.

Example:

foreach ((db)phonebook) {

      display "Name: {foreach:Name}";

      display "Telephone: {foreach:Tel}";

}

 

The following BEE Variables will be made available within a "database" loop:
("foreach" is to be substituted by the Loop Variable name if one is explicitly specified.)

foreach:fieldname

Value of the field in the data record

status%database
also in dbobj%status

Error code of the database retrieval, or 0 if successful

message%database
also in dbobj%message

Error message of the database retrieval, or blank if successful

result%foreach:iteration

The iteration count.  It contains 1 in the first iteration and 2 for the second etc.

 

(dbtree)dbobj

In this version, a "foreach" loop is the only mechanism to access a "dbtree".  (Please see the "dbtree" command for more details about building and accessing a dbtree.)

The nodes of the dbtree will be fetched from the last data access via the database object (dbobj), one record (or node) in every iteration, and assigned to the "Loop Variable".  The attributes of the dbtree are assigned to individual elements, indexed by the attribute name.

Three more values will be made available inside a dbtree foreach loop: result%foreach:activeness, result%foreach:isparent, and result%foreach:level.

The following BEE Variables will be made available within a "dbtree" loop:
("foreach" is to be substituted by the Loop Variable name if one is explicitly specified.)

foreach:fieldname

Value of the field in the dbtree node

result%foreach:activeness

The "activeness" value of the current node

result%foreach:isparent

0 if the current node is a leaf node, or 1 if it is a parent node.

result%foreach:level

The number of active parents inclusively between itself and the root.  (Note: The root does not count because there is no root node.)

result%foreach:iteration

The iteration count.  It contains 1 in the first iteration and 2 for the second etc.

 

var is the Loop Variable.  The default Loop Variable is "foreach".  Explicitly specifying the Loop Variable helps to make the code reads better, and also is necessary in a nested loop to avoid crashing of the Loop Variables if the inner loop use the same Loop Variable as the outer one.

The Loop Variable is NOT a reference to the source variable being looped through.  You can modify the Loop Variable and use the new value within the loop, and the change has NO effect on the source variable.  Upon the next iteration, the Loop Variable will be assigned the next value it should receive.

The Loop Variable is an array.  If you specify an element as the Loop Variable, the element part will be ignored and the entire array will be used as the Loop Variable.

maxiter is the maximum number of iterations the loop will execute.  It is default to 1000.  Zero means infinity.

Previous Page       Next Page

Accsoft Computer Technology Pty Ltd     ABN: 98 065 617 549
PO Box 892, Epping NSW 1710         Level 1, Epping Office Park, 242 Beecroft Rd, Epping NSW 2121, Australia
Tel: Sydney - (02)98691668     National - 1300-881668         Fax: (02)98691866
© Copyright 2003 Accsoft Computer Technology Pty Ltd