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