PHP 6: MVC & PDO In-class Steps

  1. If necessary, import employees.sql (see RDD folder) into MySQL Workbench.
  2. Review the folder structure of the web site:
  3. index
  4. css (forms and database)
  5. javascript (employees.js validation, later one per form)
  6. model (one library per table)
    connectemployees
    utilities (review)
  7. views
  8. Open employeeDetails.php. Note JavaScript include files
  9. Review PDO concepts
  10. Open connectemployees.php. Discuss along with notes
    Note require statement in index.php
  11. Prepared statements concepts
  12. Controller: change default action to employeeList
  13. Cases: call function getEmployeeList in list case
  14. Insert prepared statement in getEmployeeList (model) to get all empId, last+first from tblEmployees order by name.
  15. View: employeeList: print_r $emps (results from model call)
  16. Review structure of result sets (array of records, each record is an array of fields, each field duplicated: indexed and keyed).
  17. Insert PDO::FETCH_ASSOC in fetch command to eliminate indexed array items
  18. Remove print_r, replace with foreach; display ID and Name
  19. Replace names with name links that sends back empId
    href=’?action=details&empID=#####’
  20. (Cases) employeeDetails case: echo empId
  21. (Cases) replace test empId (0) with empId from REQUEST
  22. (model)employees.php, replace test data array with code to get details of record with given empID. Note only one record retrieved (fetch)
  23. In employeeDetails.php, display details array. Ensure data in form matches print_r then remove print_r
  24. (model) plants.php replace sample array with code to get plantId and plantName from database
  25. (Cases) call getPlantList in details case
  26. (view) employeeDetails, first print_r plant results. Test
  27. (view) employeeDetails update code to fill Plant combo box with appropriate name and ID (value)
  28. (view) Add a Search text box (in a mini-form) to employee List
  29. (model) Modify getEmployeeList to accept filter parameter
  30. (model) Determine where clause based on whether filter is empty and update SQL
  31. (Cases) Add IF statement before case to set $employeeFilter
  32. (Cases) Add $employeeFilter to getEmployeeList call
  33. Test (search for “nn”)
  34. View employee details. Return to Employee List. Note search setting removed.
    Will be fixed in Unit 7: Session Variables
  35. (view) employeeList, add New Employee button with icon (mini form with hidden action variable or link).
  36. (Cases) add case ‘employeeNew’ (just echo that new was selected for now).
  37. Test to ensure controller recognizes new action
  38. (Cases) Add code to clear $details variable, get plants, show details form
  39. Test
  40. (JavaScript) employees: note cancel and delete buttons verify
  41. (view) employeeDetails: note all buttons are submit buttons, each has different name.
  42. (Cases) Replace Save authorized message with print_r of REQUEST.
  43. Test. Pick any employee. Click Save. Note btnSave in REQUEST. Back. Repeat for other buttons.
  44. (Cases) Remove print_r. Add switch(true) determine which button was set. Just echo.
  45. Test
  46. btnCancel: header(“Location: ?action=employeeList”); Test.
  47. (model) employees: add deleteEmployee to delete employee given ID.
  48. Delete corresponding trips too.
  49. (Cases) call function and simply echo complete
  50. Test (note empId). Using MySQL Browser, verify trips are gone too.
  51. (Cases) After delete, redisplay list. Test.
  52. (Cases) btnSave: call updateEmployee
  53. (model) code updateEmployee
  54. Unformat phone and salary
  55. Phone, hire date and salary are optional
  56. Test with just a complete echo. Use MySQL Browser to verify.
  57. Add header call to redisplay list.
  58. Test.
  59. (view) empDetails: Since ID can change, add orgEmpID hidden variable.
    Define after PHP that sets $empID so can use $empID.
  60. (model) updateEmployee:
    If necessary, add empId to Set field list
    change where parameter to orgEmpID.
    Note orgEmpID must have a value (this is an update function).
  61. Change ID and Test. Note list automatically updates.
  1. (Cases)employeeSaveNewbelow employeeUpdate
  2. (view)employeeDetails: modify value of action field. If action is employeeNew, employeeSaveNew otherwise employeeUpdate
  3. View page source: check action variable
  4. (Cases)btnSave case: Add if statement: if action is update, update, else insertEmployee
  5. (model) employees: Create insertEmployee function.
  6. Test. Maybe look at data in MySQL Browser
  7. Turn JavaScript off. Enter new record making an error. Note action is employeeSaveNew, but (view source) new action is update.
  8. (view) employeeDetails: add empSaveNew to action determination
    Tip: use strpos($action, ‘New’)>0
  9. Test
  10. (view) employeeDetails: Add if statement to skip delete button if New or SaveNew
  11. Test. Make sure stays off if make an error.
  12. (view) employeeDetails: Turn JavaScript back on.
  13. If new, add a blank option to combo box (value Missing). Since need to know if new so often, create a variable.
  14. (view)employeeDetailsreview use of orgKey. orgEmpID also allows checking for duplicate record.
  15. Add orgFirst and last names.
  16. (model)employees.php add isDuplicateID and isDuplicateName
  17. (model) validateEmployeeInputs, only call the isDuplicate functions if the keys have changed
  18. Test
  19. Note, if you make a PHP error, the error version of the form includes the new data as org values.
  20. (view) Modify definition of orgVariables. If org variable exists, use it, otherwise use regular variable.
  21. Test by entering dup id and dup name.
  22. Test by editing an existing record and entering a dup id and name
  23. After error, Reset button resets to bad data value. Modify resetForm to restore orgEmpId, orgFirstName, orgLastName (getElementsByName(“fieldname”)[0].value;
  24. Test
  25. Copy areyousure.js to javascript folder.
  26. Link areyousure to employeeDetails.php view
  27. See notes: add AreYouSure to employee.js
  28. (view) Add Trips button to employeeDetails but only if not new employee (image plane.png)
  29. (Cases) Add case to handle btnTrips
  30. (Cases) Copy all employee cases as trip cases (add separators and update)
  31. tripList: add employeeId parameter
  32. tripDetails: generate list of employees
  33. tripDetails: getTripDetails: 2 parameters (employeeId and tripDate)
  34. tripNew: generate list of employees
  35. tripSaveNew: header: include employeeId
  36. Same for btnCancel and btnDelete
  37. (Cases) tripList: comment out include, call printArray to display trips
  38. (model) code getEmployeeTripsNOTE: employeeID, not empID
  39. Test
  40. <view> Add code to create table of links for each trip
  41. Test. Touch each link to ensure employeeId and tripDate are correct
  42. Test Employee List button
  43. Test New Trip button. Ensure combo box filled
  44. Test Trip List button. Note Employee Id gone.
  45. tripList> Add hidden employeeId variable to new button.
  46. Cases newTrip $details = $_REQUEST
  47. Test. Test Trip List button.
  48. Review tripDetails
  49. Note action variable
  50. Note employeeId and tripDate are logical keys (org variables defined).
  51. (model) Code getTripDetails
  52. Test Trip Details view
  53. (model) Code updateTrip
  54. (Cases) Double-check updateTrip logic
  55. Test
  56. (model) Code insertTrip
  57. Test
  58. (model) Code deleteTrip
  59. Test
  60. (model) Validation: update duplicate record check
  61. Test on both new and existing records
  62. (view) Modify tripList to show Employee Name
  63. (model) Create function to get employee name based on ID
  64. (Cases) Add call to getEmployeeName to tripList case
  65. Test. View trip details and return to list to ensure name still correct.