Page 474 - Beginning PHP 5.3
P. 474

Part III: Using PHP in Practice
                   Next, the script makes a decision about which function to call. If the Save Changes button was
                 clicked,   saveMember()  is called to update the member record in the database. If Delete Member was
                clicked,   deleteMember()  is called to remove the member from the database. Otherwise,
                   displayForm()  is called to display the member details form and access log records. The script passes
                the retrieved   $member  object to  displayForm()  so that its data can be displayed.

                  The   displayForm()  function works in a similar way to its counterpart in the  register.php  script you
                 created earlier in the chapter. First it retrieves the list of   LogEntry  objects pertaining to the member in
                 question, and stores them in a   $logEntries  array. After displaying the page header, the function
                 outputs any error messages at the top of the page.

                  Next,   displayForm()  retrieves the  start  and  order  parameters from either the query string or the
                 form post, and stores them in   $start  and  $order . Then the form itself is displayed. The form includes
                three hidden fields:

                      < input type=”hidden” name=”memberId” id=”memberId” value=” < ?php echo $member-
                      > getValueEncoded( “id” ) ? > ” / >
                      < input type=”hidden” name=”start” id=”start” value=” < ?php echo $start ? > ” / >
                      < input type=”hidden” name=”order” id=”order” value=” < ?php echo $order ? > ” / >
                    memberId  tracks the ID of the member being viewed or edited, and  start  and  order  propagate their
                 respective values from the member list page, so that the administrator can return to the same point in the
                 member list after viewing or editing the member.

                   The rest of the form works much like it does in   register.php . Each form field is displayed, using
                   Member::getValueEncoded() ,  setChecked() , and  setSelected()  to retrieve the data from the  Member
                object and display it. The bottom of the form contains a Save Changes button and a Delete Member button.
                After the form, the access log details are displayed in the same way as the old   view_member.php  script.
                   The Back link at the bottom of the form works slightly differently than   register.php . Because you
                don ’ t know how many times the administrator has submitted the form, you can ’ t use a JavaScript
                function call to move back one page to the members list page, as   register.php  did. So instead you
                 construct a new link to return to   view_members.php , passing in the  $start  and  $order  values as
                   start  and  order  query string parameters, in order to return the administrator to the same point in the
                members list.

                   saveMember()  checks the member data that was submitted in the form and, if valid, updates the
                member record in the database. It works much like   processForm()  in  register.php . A new  Member
                object is created that contains the filtered values sent from the form. If any required fields were missing,
                or if the chosen username or email address is already used by another member, an error message is
                generated, and the form is redisplayed by calling   displayForm() .
                   If all went well, the member record is updated by calling   $member - > update() , and a success message is

                 displayed by calling   displaySuccess() . Note that, thanks to the design of the  Member::update()
                method, the administrator can leave the password field blank in order to retain the member ’ s existing
                password.








              436





                                                                                                      9/21/09   9:14:15 AM
          c14.indd   436
          c14.indd   436                                                                              9/21/09   9:14:15 AM
   469   470   471   472   473   474   475   476   477   478   479