Computer Bookshelf

Foundation PHP 5 for Flash — Contents

Foundation PHP 5 for Flash assumes no previous knowledge of PHP or database management, although readers are expected to be at home working with Flash, and preferably have some knowledge of ActionScript. The book is based on Flash MX 2004, but all the example files (available directly from the friends of ED website after publication) are also available in Flash MX format. Similarly, although the focus is on PHP 5, the code has been designed so that 95% of it will work with PHP 4.2.0 or above.

Chapter 1: Getting Ready to Work with PHP

Interaction of Apache, PHP, MySQL and FlashThe chapter sets the scene by explaining how the Apache web server, PHP, and MySQL integrate with each other to provide dynamic interactivity with Flash. Then it's down to business with detailed installation and configuration instructions for both Windows and Mac OS X. Although configuring Apache and PHP is far from difficult, it's a subject that tends to trip up quite a lot of people, so there are extensive troubleshooting sections to make sure you get off to a smooth start.

Chapter 2: Flash Breaks Free

With PHP up and running, this chapter provides a quick overview of the basic syntax of PHP, and then puts your knowledge to practical use by creating an online Flash form to send feedback from your website to your email inbox. The chapter also shows how to check that variables are being sent and received correctly between Flash and PHP.

Chapter 3: Calculations and Decisions

Multiconverter applicationControl flow — the use of conditional statements — is an important part of any scripting or programming language. This chapter uses a multiconverter application for weights and measures to demonstrate control flow and mathematical calculations in PHP. The interface is built in Flash, but all the calculation and formatting is done in the background by PHP. Although weights and measures are fixed, the application lays the foundation for applying the same principles to other calculations (such as foreign exchange rates) that cannot be stored inside a Flash movie.

Chapter 4: Of Strings and Things

Because of its background in the development of dynamic web pages, PHP has a much more powerful arsenal of string manipulation methods than ActionScript. After demonstrating the most important of these, and showing how to build your own functions in PHP, the chapter applies them to completing the multiconverter application from Chapter 3.

Chapter 5: Working Smarter with Arrays and Loops

Arrays and loops are common to all programming languages — but subtle differences between their implementation in PHP and ActionScript are likely to catch out the unwary. The differences between the two languages are explored in depth through many practical examples. The chapter rounds off with a Flash application that merges two live RSS feeds, demonstrating how complex information from independent external sources can be broken down into its component parts and reorganized.

Chapter 6: PHP and Databases: Packing Real Power behind your Applications

PHP and MySQL are a perfect match for each other, but even though MySQL is the world's most popular open source database, it's not without its critics. This chapter takes a look at both its strengths and drawbacks, and also considers the merits of a new database — SQLite — which comes automatically bundled with PHP 5. Detailed instructions are given for installing MySQL and for creating a first database — a word list that will be used to drive the Hangman word game in the following chapter. The chapter concludes with instructions for installing phpMyAdmin, a graphical interface for MySQL, as well as a brief look at two new offerings from the developers of MySQL.

Chapter 7: Playing with Words

Time for a bit of semi-serious fun — adapting the Hangman game from Sham Bhangal's Foundation ActionScript for Flash MX 2004 to work with a database. Instead of being limited to a hand-coded array of 20 words or so, teaming up Flash, PHP and MySQL gives the game a vocabulary of more than 21,000 words. Hangman gameDriving everything in the background is one of the key features of Foundation PHP 5 for Flash — a set of PHP classes that enables you to interact with a MySQL database using exactly the same code, regardless of whether your host is running PHP 4 and MySQL 3.23 or 4.0, or PHP 5 and MySQL 4.1. In addition to the basics of OOP programming with PHP 5, the chapter also shows you how to build a scoring system with the Flash SharedObject, as well as using SQLite as an alternative database.

Chapter 8: Creating a User Registration Database

Delving deeper into working with MySQL, this chapter teaches you the basics of creating a relational database, and deciding which column types to use. It also demystifies collation — a new feature in MySQL 4.1 that has confused many users (it's essential for setting the correct sort order for different languages). All this knowledge is then put to use in a practical application — a user registration system built in Flash.

Chapter 9: Protecting your Data with Sessions

There's little point in having a registration system if you can't restrict access to certain parts of your site. This chapter shows you how to do it simply and effectively with PHP sessions. It also shows you how to prevent unauthorized access to your content, even if an unregistered user manages to open your Flash movie in a browser.

Chapter 10: Keeping Control with a Content Management System

Screenshot of content management systemThe user registration system from Chapter 8 is developed to build a content management system (CMS) in Flash. This shows you how to use the four essential SQL commands: INSERT, SELECT, UPDATE, and DELETE. Although it uses only a simple database, all the techniques employed in this CMS will be invaluable in building more sophisticated applications. It shows you how to build a navigation system to page through a long set of database results; how to prevent duplicate entries, and accidental deletion of records; and how to build a search system. The chapter also suggests a strategy for adapting an existing application when needs change, as well as adding extra columns to database tables. The entire content management system is protected by the login system developed in the previous chapter.

Chapter 11: Working with Dates

Although we work with dates every day, they represent a significant problem for online applications. Do you put the month before the day (as Americans do)? Or do you do it the other way round (European-style)? Or should you put the year first (as the Chinese and Japanese do)? This practical chapter guides you through the maze of working with dates, showing you along the way how to calculate someone's age accurately with a database query. By the end of the chapter, you will have built a Flash component that formats dates correctly for MySQL (and converts them back).

Chapter 12: Working with Multiple Tables and XML

The final chapter is devoted to an in-depth case study that builds the back-end for a multi-table relational database, such as might be used for an online bookstore. The content management interface is built in XHTML, and the front-end in Flash, bringing together all the skills taught throughout the book. The database demonstrates the use of a lookup table, full joins and left joins, as well as deleting records simultaneously from multiple tables. The case study also shows you how to use one of the most powerful additions to PHP 5 — SimpleXML — to convert an XML document (such as price information from an online resource) and insert the values directly into a database. (Note: the book concentrates on working with XML in PHP; it does not cover the ActionScript XML Class, which is used exclusively inside Flash.)

Appendix A: When Things Go Wrong with PHP and MySQL

Twenty pages packed with hints and advice on troubleshooting.

Appendix B: Converting Applications to ActionScript 1.0

Advice on how to adapt the code in the book to work with Flash MX. (Note: all the download files are available in both MX and MX 2004 formats.)

Appendix C: Installing Older Versions of MySQL on Windows

Detailed installation instructions for developers who need to use an older version for testing.

Appendix D: Using Languages Other than English in MySQL

An overview of the new features in MySQL 4.1 that enable you to build multilingual databases.

Appendix E: Essential MySQL Maintenance

All you need to know about backups, upgrading, and recovery after losing your root password.

Buy Foundation PHP 5 for Flash