Skip to main content

<CSP:WHILE>

Delimits the body of a WHILE loop.

Synopsis

<CSP:WHILE>...</CSP:WHILE>

Attributes

General Attributes

Attribute Description Value
CONDITION A server-side expression evaluated after each iteration. If the expression evaluated to true, then the loop is iterated again. A server-side expression.
COUNTER The name of a local variable that is automatically incremented upon each iteration. A valid variable name.
CURSOR For use with embedded SQL only. An SQL cursor that gets the row for the next iteration. A valid SQL cursor name.
INTO For use with embedded SQL only. Comma-separated INTO list for the SQL FETCH of the next row. Specifies the variables to receive the row's data. A comma-separated list of variables..

Description

The CSP:WHILE tag repeatedly executes its contents while the server-side expression specified by the CONDITION attribute is true. Upon each iteration, a local variable, specified by the COUNTER attribute, is incremented. The counter variable is initialized to 0 before the loop and is incremented at the beginning of each iteration. If the counter attribute is not specified, no counter will be generated. The CSP:WHILE tag may also be used to loop over a cursor defined for embedded SQL using the SCRIPT LANGUAGE=SQL CURSOR= tag.

The basic structure of a CSP WHILE loop is:

<CSP:WHILE attributes>
content
</CSP:WHILE>

where attributes are the attributes of the CSP:WHILE tag (listed above) and content is the content to display during each iteration of the loop.

WHILE Loop Condition

At the end of each iteration of a WHILE loop, a condition is evaluated to determine whether the loop will execute again. If the condition is true, another iteration of the loop is performed, but if the condition evaluates to false the loop is exited. The relevant condition is specified using the CONDITION attribute of the CSP:WHILE tag:

<CSP:WHILE CONDITION="cond">

where cond is the expression to evaluate at the end of each iteration.

You must specify a CONDITION attribute for every CSP:WHILE tag.

Counter in a WHILE Loop

A WHILE loop can maintain a counter in a local variable. This counter is incremented by 1 upon each iteration of the loop. The local variable used as a counter is specified by the COUNTER attribute:

<CSP:WHILE CONDITION="cond" COUNTER="count">

where count is the name of the counter. The initial value of the counter is always 0.

If you do not specify a COUNTER attribute, then the WHILE loop will not maintain a counter variable.

Using SQL in a WHILE Loop

You can define a static SQL cursor using the <SCRIPT> tag and then use a WHILE loop to display the results of the query.

For example, the following code defines a static SQL cursor named query:

<SCRIPT LANGUAGE=SQL CURSOR="query">
SELECT Name,SSN FROM Sample.Person ORDER BY Name
</SCRIPT>

You can use a CSP:WHILE tag to iterate over this cursor as follows:

<CSP:WHILE CURSOR="query" INTO="name,ssn">
    Name: #(name)#, SSN: #(ssn)# <BR>
</CSP:WHILE>

The CURSOR attribute specifies the name of an SQL cursor defined using a <SCRIPT> tag while the INTO attribute specifies a comma-delimited list of local variables into which the column values retrieved by the query are placed in their respective order.

<CSP:WHILE> Example

This simple WHILE loop displays the value of the counter during each iteration of the loop:

<UL>
<CSP:WHILE COUNTER="i" CONDITION="i<10">
    <LI>The value of the counter is #(i)#.
</CSP:WHILE>
</UL>

Note that the < (less than) operator is represented using the HTML &lt; entity as HTML does not allow <, >, or &, within an attribute value. This entity is interpreted as “<” by the CSP page compiler when it generates code for the WHILE loop.

FeedbackOpens in a new tab