Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




Databases - Practical PostgreSQL
Previous Page Home Next Page

Loop Iteration

If you have an LXP region that you wish to iterate more than once, the <for> tag exists for this purpose. It requires at least a start attribute, and either an end or endbefore attribute. Each attribute should be given a numeric value.

The start attribute defines a whole integer value to begin the loop iteration with. That value initializes an iteration count, which will be incremented by 1 for each iteration of the loop. If the end attribute is defined, the loop will stop iterating after the iteration count has looped through the number specified by end. Alternatively, if the endbefore attribute is defined, the loop will stop one iteration earlier. Using end and endbefore is respectively equivalent to using the <= and < operators in a programming language such as PHP or C.

While iterating, a special LXP object called for maintains a value called count, which stores the value of the current loop's iteration count. Example 13-19 demonstrates a simple for loop that will iterate from 1 to 5.

Example 13-19. A simple <for> loop

  <for start="1" end="5">
    Iterating loop: <putvar name="for.count" /><br />

Here is the output from this loop, when processed by LXP:

Iterating loop: 1<br />

Iterating loop: 2<br />

Iterating loop: 3<br />

Iterating loop: 4<br />

Iterating loop: 5<br />

The <for> loop iterator can be invaluable when dealing with arrays of values that you need to return by using LXP. As mentioned earlier in this chapter, if a variable is defined with trailing square-brackets ([]), it will be implicitly given an offset by LXP for each value found with that name. LXP will also create an object variable of the same name, but without square-brackets, with two attributes: size and last. The size value (e.g., my_array.size) stores the number of elements in the implicitly defined array, while the last value (e.g. my_array.last) stores the offset of the last value.

Example 13-20 demonstrates the handling of a passed variable called my_array[].

Example 13-20. Handling array results with <for>

  <for start="0" end="@my_array.last">
    Here is the value of my_array, at offset <putvar name="for.count" />:
    <putvar name="my_array[@for.count]" />
    <br />

Notice that the at sign (@) for the my_array object is only used where its variable value is desired, rather than its name. Thus, it is omitted in the <putvar> tag, since the name attribute expects a literal variable name, and not the variable's value.


If you manually assign index offsets to variables in a form (e.g., my_array[0], my_array[1]) rather than creating an implied array (e.g., my_array[]), LXP will not set the size and last values for such an array of values.

Databases - Practical PostgreSQL
Previous Page Home Next Page

  Published under the terms of the Open Publication License Design by Interspire