Page 454 - Beginning PHP 5.3
P. 454

Part III: Using PHP in Practice
                   Also, notice that the  Member  object ’ s  joinDate  field is set:

                    “joinDate” = >  date( “Y-m-d” )

                  PHP ’ s  date()  function is used to generate a string representing the current date in the format  YYYY - MM - DD .
                This string is then stored in the   joinDate  field, reflecting the date that the member completed their
                 registration.

                      You can find out more about   date()  in Chapter 16.

                   Now that a   Member  object has been created and populated with the submitted form data, the script
                performs various checks on the data. First, it runs through the list of required field names; if any
                required field was not filled in, its field name is added to the   $missingFields  array:
                      foreach ( $requiredFields as $requiredField ) {
                        if ( !$member- > getValue( $requiredField ) ) {
                          $missingFields[] = $requiredField;
                        }

                      }
                   If any missing fields were encountered, an appropriate error message is added to the  $errorMessages
                 array:

                      if ( $missingFields ) {
                        $errorMessages[] = ‘ < p class=”error” > There were some missing fields in the
                    form you submitted. Please complete the fields highlighted below and click
                    Send Details to resend the form. < /p > ’;
                      }

                   Error messages are also created if the entered passwords didn ’ t match, or if the entered username or
                 email address is already taken. For the username check,   Member::getByUsername()  is called with the
                 entered username; if it returns a   Member  object, the script knows that the username is taken and
                 generates an error message. Similarly, for the email address,   Member:: getByEmailAddress()  is called
                 to determine if a member with the entered email address already exists in the database.

                   Finally, if any error messages were raised, the script calls   displayForm()  to redisplay the form to the
                user, passing in the list of error messages, the list of missing fields (if any), and the populated   Member
                 object containing the data already entered by the user, for redisplaying in the form. On the other hand, if
                 the submitted data was correct, the member record is created in the   members  table by calling the  Member
                object ’ s   insert()  method:
                      if ( $errorMessages ) {
                        displayForm( $errorMessages, $missingFields, $member );
                      } else {
                        $member- > insert();
                        displayThanks();
                      }

                   The final function in the script,  displayThanks() , simply displays a thank - you message to thank the
                 member for registering.




              416





                                                                                                      9/21/09   9:14:07 AM
          c14.indd   416                                                                              9/21/09   9:14:07 AM
          c14.indd   416
   449   450   451   452   453   454   455   456   457   458   459