Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
DTML Basics (part 3)
Twist and turn with DTML's loops.

| Playing The Numbers |

Before we get started, let's get the jargon straight - what's a sequence anyway?

As traditionally understood, a "sequence" is a series of items, usually connected to each other by a logical thread. For example,


0, 1, 2, 3, 4, 5...



p, q, r, s, t...



huey, dewey, louie...


are all valid sequences

In the Zope context, the definition of a sequence can be scoped down a little further - a sequence here is usually a set of objects. For example, if you create a folder in Zope, the objects stored within that folder can be considered a sequence. Or, if you retrieve a set of records from a database, the resultset returned can also be considered a sequence of data items.

A sequence, however, is just one part of the jigsaw. In order to access the elements that make up a sequence, you usually need a programming structure that will iterate, or loop, through the sequence, processing each element in turn. This loop can be combined with decision-making logic (remember what you learnt last time?) to perform specific actions or execute specific commands while processing the elements in a sequence.

In order to demonstrate this, let's create a sequence in Zope and write a loop to process it. Fire up Zope, log into the Zope management interface, and create an instance of the DTML Document object. Name it "SampleSequence" and fill it with the following code:


<h3><dtml-var title_or_id></h3><br>

<ul>
<dtml-in expr="0, 1, 2, 3, 4, 5">
<li> I am <dtml-var sequence-item></li>
</dtml-in>
</ul>


First, I've created a simple sequence by brute force - it contains numbers from 0 to 5. Then, the <dtml-in> tag is used to loop through the sequence, in a manner similar to the "for" loops that PHP and Perl programmers are familiar with. From the output, it's obvious that this script loops six times to display the items in the sequence.

The individual elements of the sequence can be accessed via the special "sequence-item" variable. This is true in all cases, except when the items in the sequence are object that cannot be converted to strings.

Save the code and view the output of the script - you should see something like this:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
* I am 0
* I am 1
* I am 2
* I am 3
* I am 4
* I am 5
').'
'


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback