From the course: Learning the Standard PHP Library
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Treating the final element differently with CachingIterator - PHP Tutorial
From the course: Learning the Standard PHP Library
Treating the final element differently with CachingIterator
- In courses.xml, the subjects and software elements have an indeterminate number of children. Some have only one, like this example of software here. But in other courses, it might have two, three or even more types. When displaying multiple values, it would be useful to know if any more follow the current element so we can treat the last one differently by inserting the word "and" in front of it. That's easily done with the CachingIterator class. So let's just open this page, caching.php, which you can find in the Chapter 6 folder of the exercise files. It loads courses.xml as a simple XML and then a foreach loop goes through the courses displaying the title and description of each course. To display the software related to each course, we need to get the software element and then retrieve its child element. So let's add a new line inside the foreach loop, and we'll store the software elements as software. So software = course and then we access its software property and then we…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
(Locked)
Creating a repeat sequence with InfiniteIterator3m 35s
-
(Locked)
Treating the final element differently with CachingIterator4m 14s
-
(Locked)
Using RecursiveCachingIterator with multidimensional structures2m 59s
-
(Locked)
Generating an ASCII graphic tree with RecursiveTreeIterator4m 28s
-
(Locked)
NoRewindIterator and EmptyIterator2m 26s
-
(Locked)
-
-