PHP DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


Simplify PHP Development with WASP
Pages: 1, 2, 3, 4, 5

In order to fill the arTasks placeholder, you need an array of task records that exist in the database. You have access to this data through WASP's db model layer, using the Wrapper classes that you generated by running phing db and located in the Todo/db/ directory.

The code:

$oTasks = new TaskWrapper();
$oTasks->findAll();

constructs a DataObjectWrapper object for the Task table, and finds all of the existing records.

Now you can loop over the values with the code:

while ($oTasks->next())

The next() method will return false when there are no more records.

While looping through the set of tasks, go ahead and add them to an array to pass to the view:

$arTasks[$oTasks->getId()] = $oTasks->toArray();

This code uses the built-in method getId() to index the array using the primary key of the table you're working with. Referring back to the database-create SQL, this gives the value of the column TaskId. At that index, set the value of the array to the array value of the Wrapper object. The built-in toArray() method returns an array representation of the column data in the table. For example, the array might look something like this:

{ 'TaskId' = '1', 'Name' => 'Buy Groceries'}

The view code doesn't care about the TaskId field, but if you remember, the line:

{task[Name]}

uses the Name field to display the name of the task on the page.

As there isn't a form to process on this page, you don't need a handleEvents() method this time.

Now that you have the code written for the front page of the task list, consider what happens when the page loads. If you haven't saved any task items, there won't be any entries in $arTasks. Because the Flexy placeholder arTasks is null, the block:

<li flexy:foreach="arTasks,key,task">{task[Name]} - <i>{task[Due]}</i></li>

will not display.

Once you go to the Todo/Entry/ page and create the first task, the flexy:foreach has a value to loop over, and the list item will display, as Figure 3 shows.

showing one task
Figure 3. Showing one task

As you create more entries, Flexy automatically displays more list items (Figure 4).

showing multiple list items
Figure 4. Showing multiple list items

Finishing Touches

Congratulations! You now have a functioning task list where you can go and post items for anyone to see. Clearly, however, you have some work to do in the way of tailoring the design to your own style, and adding features such as completion marking and category grouping. Fortunately the HTML is all in the view layer, where you can create and modify templates without fussing over embedded PHP code.

Conclusion

Tools such as DB_DataObjects and Flexy have been available for use in PHP for a while, but few projects have put them together in such a unified way. With the enhanced object-oriented nature of PHP 5, working in three tiers with PHP using WASP is easier. Hopefully this tutorial has provided all of the information you need to go out and create feature-rich dynamic web applications in WASP. For more information on creating applications in WASP, see the WASP documentation.


Return to the PHP DevCenter.


Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!


Sponsored by: