Main Menu
Lecture 1
Lecture 2
Lecture 3
Lecture 5
Lecture 6
Lecture 7
 

 

 

Iteration


Normally when data is retrived from the database, there is more than one element retrived. How then do you display this information when you get it back ? For eaxample if you where to queary the WOMovies database for all the movies beginging with T, how could you display the results on your HTML page ? You certaintly can't add a WOString for each title as you never know how many titles will be returned.


Before we move no to the solotuion we must look at Iteration. Iteration is the process of moving through an array one element at a time. This can be done by using a "for" loop that repeats for the size of the array that you have ( in our case the array size would be the same as the number of entries that was returned from the database ).

//Declare variables to be used
EOEditingContext ec = session().defaultEditingContext();
Movie currentStudent;
NSArray studentList;
NSMutableDictionary bindings = new NSMutableDictionary();

bindings.setObjectForKey("*e*","title");
studentList = EOUtilities.objectsWithFetchSpecificationAndBindings (ec,"Student","searchStudents",bindings);

//Iterate through the array and print the name of each element to the std output.

for(i=0;i<studentList.count();i++){
currentStudent = (Student)studentList.objectAtIndex(i);
System.out.println(i + ") " + currentStudent.lastName());
}


The other option is to use a Enumerator.

//Use an enumerator to iterate through the moviesList.

Enumeration enumeration = moviesList.objectEnumerator();
while(enumeration.hasMoreElements()){
currentMovie = (Movie)enumeration.nextElement

//Do something with/to the currentMovie
}

 

You will notice that in both cases we have a list of elements, as well as an instance of a Student ( currentStudent) which holds the current element we are up to as we iterate through the array ( studentList). WebObjects has a dynamic element that has the same behaviour as this. It is called a WORepition. A WORepition allows you to repeat a section of HTML over and over, for the number of elements in an array.