Page 446 - Beginning PHP 5.3
P. 446

Part III: Using PHP in Practice
                  Building a Member Registration Application

                   Now that you know how to insert records into a MySQL table via PHP, you can write a script that
                 lets new members sign up for your book club. Rather than reinventing the wheel, you build on the
                 object - oriented member viewer application that you created in Chapter 13, extending the classes to add
                 new functionality and creating a script to register new members.


                  Adding More Common Code
                   First, add some extra code to the  common.inc.php  file that ’ s inside your  book_club  folder. Open this
                file in your editor.
                  Within this file, it makes sense to include the other common files that are used by the rest of the
                application. That way, scripts only need to include   common.inc.php , and the other files will be included
                 automatically. Add the following to the start of the   common.inc.php  file:

                    require_once( “config.php” );
                    require_once( “Member.class.php” );
                    require_once( “LogEntry.class.php” );

                   Now add the following line to the CSS declarations within the  displayPageHeader()  function:

                         < style type=”text/css” >
                          th { text-align: left; background-color: #bbb; }
                          th, td { padding: 0.4em; }
                          tr.alt td { background: #ddd; }
                          .error { background: #d33; color: white; padding: 0.2em; }
                         < /style >

                   This line creates a CSS  .error  class that you ’ ll use to highlight any problems with the registration form.

                   Finally, add three extra utility functions to help with displaying the registration form:

                    function validateField( $fieldName, $missingFields ) {
                      if ( in_array( $fieldName, $missingFields ) ) {
                        echo ‘ class=”error”’;
                      }
                    }

                    function setChecked( DataObject $obj, $fieldName, $fieldValue ) {
                      if ( $obj- > getValue( $fieldName ) == $fieldValue ) {
                        echo ‘ checked=”checked”’;
                      }
                    }

                    function setSelected( DataObject $obj, $fieldName, $fieldValue ) {
                      if ( $obj- > getValue( $fieldName ) == $fieldValue ) {
                        echo ‘ selected=”selected”’;
                      }

                    }


              408





                                                                                                      9/21/09   9:14:04 AM
          c14.indd   408
          c14.indd   408                                                                              9/21/09   9:14:04 AM
   441   442   443   444   445   446   447   448   449   450   451