After the Master Page is created, it is best to create the Detail Page with an ID passed to it from the Master Page to show more DETAIL information about the selected item from the Master Page.
The code that will be created will perform the following tasks:
PART A: CREATE STATIC CONTENT
Create Static Image
Create Static Table
Create Static Button
PART B: ADD DYNAMIC SCRIPTS
Add SQL SELECT Recordset Script
PART C: REPLACE STATIC CONTENT WITH DYNAMIC CONTENT
Make Image Dynamic
Make Dynamic Table
Make Button Button
Make Confirmation Dialog Box Dynamic
Option 1: Create Non User Data
Option 2: Provide An Avatar
Option 3: Create Alternating Row Colors
PART A: CREATE STATIC CONTENT
There are three static components that will be created:
Image
Table
Button
Create Static Image
Create static image that is needed for the Employee Detail Page.
Open the employee_detail.php page that you created earlier.
Write (or copy) the following highlighted code below the
<h2>
tag. Comment is optional. WHY: To create a static image below the header.
The <img> tag has a inline style with a hard-wired image of one of the images in the images folder. It could be any one of the images in this folder. Later, part of the src attribute will be will be replaced with its PHP dynamic counterpart.
The inline style adds:
a 20 px right margin that will be used to create a padding for a table that will be created later.
a 1 px solid black border to confine the image if it is against a white background.
a float that will be used to float the image to the left so that it is responsive.
CHECK POINT: Save the file and preview it in a browser. You should see the image displayed below the header when the page loads.
Create Static Table
Write (or copy) the following highlighted code below the
<img>
tag. Comment is optional: WHY: To create a static table below the static image.
CHECK POINT: Save the file and preview it in a browser. You should see a blank table displayed below the image if the screen is small. The table will be to the left of the image on a larger screen.
Create Static Button
The DELETE EMPLOYEE button will be created now but will be used later with the delete_employee.php page.
Create a new merged row at the end of the table with a button and several attributes:
CHECK POINT: Save the file and preview it in a browser. If you pass it a recordID from the Employee Master Page or from the browser, you should see the DELETE BUTTON at the bottom of the page. If you hover over it, you see the recordID that it will pass to the Delete Employee Page.
PART B: ADD DYNAMIC SCRIPTS
Add SQL SELECT Recordset Script
A recordset will be created using the SQL SELECT clause to retrieve a SINGLE employee record from the database based on the URL variable pass FROM the Employee Master Page (index.php) when a link in the listview is clicked.
Write the following highlighted code block below the
<h2>
tag. Comments are NOT highlighted because they are optional but are written to explain code. WHY: To create a recordset that will be used create a dynamic image and to populate the table.
<h2 class="ui-body ui-body-a ui-corner-all">EMPLOYEE DETAIL</h2>
<!-- SQL SELECT QUERY -->
<?php // Store reference of SQL statement in a variable. $query_getSelectedEmployees="SELECT * FROM employees WHERE EmployeeID = $_GET[recordID]"; // Store a reference of the query in this variable from the SQL variable using the mysql_query function. $result_getSelectedEmployees=mysql_query($query_getSelectedEmployees); // Store the result of the query into an associative array using the mysql_fetcth_assoc function. $row_getSelectedEmployees=mysql_fetch_assoc($result_getSelectedEmployees);
// Close connection to database mysql_close(); ?>
CODE EXPLANATION:
Since most of the columns are needed for the detail page, the wildcard asterisk (*) is used to return ALL of the COLUMNS from the database. However, because the wildcard asterisk is used, we will have to MANUALLY concatenate the FirstName and LastName with a space or a non-breaking space ( ) between the two fields to create an employee's full name.
The WHERE clause is used as a FILTER to return a SINGLE ROW from the database based on the EmployeeID that is passed from the Employee Master Page (e.g., index.php).
The $_GET[recordID] is a dynamic variable that get resolved to the EmployeeID value that was passed from the Employee Master Page (index.php) when a list item is clicked.
Save the file.
PART C: REPLACE STATIC CONTENT WITH DYNAMIC CONTENT
Make Image Dynamic
Replace the static reference of the image (e.g., ann_ricoh.jpg) with the following highlighted code to the
<img>
tag above the opening
<table>
tag: WHY: To make the image dynamic instead of static.
CAUTION: Pay attention to the angle braces and forward slash at the end..
CODE EXPLANATION: Notice there are two part to the src value. First, there is the static part that is common to all images ("images/") and there is a dynamic text to the image from the database.
CHECK POINT: Save the file and preview it in a browser. Initially, you will see an error message (e.g., Notice: Undefined index: recordID...) and a warning (e.g., Warning: mysql_fetch_assoc() expects parameter 1 to be resource...). That's because the recordID is not yet defined. To test the page, do one of the two steps below:
Type a path to the file in a browser: http://localhost/EmployeeDirectory/employee_detail.php?recordID=3. Repeat this step several times with a different recordID number. You may need to go to phpMyAdmin to look at the database to see what the EmployeeID numbers are.
NOTE: Notice the ?recordID=3 at the end of the URL which represent an EmployeeID value. This is a query string that simulate the EmployeeID value that will be used in the next example.
Go to the Employee Master Page (e.g., index.php) and click on any one of the link. Repeat this step several times to see different images being displayed.
You should see the image displayed above the table if the screen is small. The image will display to the right of the table on a larger screen.
- It is important to note that the image is not coming from the database. The image name (e.g., ann_ricoh.jpg) in in the database. The images actually is a reference to the images in the images folder. .
Make Table Dynamic
Replace the static references of the non-breaking spaces (e.g., ) in the table with the following highlighted code in the
<th>
tags. WHY: To make the table dynamic instead of static.
CODE EXPLANATION: It is important to note that the PHP code blocks using regular <?php ... ?> tags instead of curly braces { .... } because they are not nested inside of a larger PHP code block.
CHECK POINT: Save the file and preview it in a browser. Initially, you will see an error message (e.g., Notice: Undefined index: recordID...) and a warning (e.g., Warning: mysql_fetch_assoc() expects parameter 1 to be resource...). That's because the recordID is not yet defined. To test the page, do one of the two steps below:
Type a path to the file in a browser: http://localhost/EmployeeDirectory/employee_detail.php?recordID=3. Repeat this step several times with a different recordID number. You may need to go to phpMyAdmin to look at the database to see what the EmployeeID numbers are.
NOTE: Notice the ?recordID=3 at the end of the URL which represent an EmployeeID value. This is a query string that simulate the EmployeeID value that will be used in the next example.
Go to the Employee Master Page (e.g., index.php) and click on any one of the link. Repeat this step several times to see different images being displayed.
You should see the table POPULATED with data from the database displayed below the image if the screen is small. The table will be to the left of the image on a larger screen.
Make Button Dynamic
The DELETE EMPLOYEE button will be created now but will be used later when the delete_employee.php page is created.
Replace the static number (1) in the Delete Button code with its dynamic counterpart:
CHECKPOINT: Save the file and preview it in a browser. You should see DELETE EMPLOYEE button at the bottom of the table. If you click on it, it will take you to the Delete Employee Page. However, the employee record will NOT be deleted because we have not written the code for the Delete Employee Page yet.
Make Confirmation Dialog Box Dynamic
It is best practice to give the user a confirmation notice that the record will be deleted and the option to cancel the process if the wrong record is selected or the user changes his or her mind on deleting the record.
Add the following highlighted code to the end of the opening
<a>
tag of the DELETE EMPLOYEE button:
CAUTION: Pay attention to the single and double quotes.
<tr> <td colspan = "2" align = "center"><a data-role = "button" data-inline = "true"
href = "delete_employee.php?recordID=<?php print $row_getSelectedEmployees["EmployeeID"];?>"
onclick = "return confirm('Are you sure you want to delete this employee?')">DELETE EMPLOYEE</a>
</td> </tr> </table>
CHECKPOINT: Save the file and test it in a browser. Open the EMPLOYEE MASTER PAGE (index.php) and click on one of the list item. Then, in the EMPLOYEE DETAIL page, click on the DELETE EMPLOYEE button. You should see an Alert dialog appear.
First, click the Cancel button. Nothing should happen except the dialog box will close. Now, try again and click the OK button. You will automatically be redirected back to the DELETE EMPLOYEE PAGE (delete_employee.php) which has not be completed yet.
OPTION 1: Create Non User Data
Sometimes it is useful to display non user data (e.g., date entered) that is not saved to the database by a user. Instead, the data is created automatically with a date/time method (e.g., NOW()). See Enhancement tab for details.
OPTION 2: Provide An Avatar
If there is an image for each record in the database, you would not need to do this enhancement. However, there may be a case where a employee is added to the database but a picture of that person was not available at the time the information was entered. As a result, a broken image icon would be displayed. See Enhancement tab for details.
OPTION 3: Create Alternating Row Color (APPLIES TO ALL PAGES)
It is best practice to creating alternating row colors for a large table to make it easier to read each row. It also help users with low vision users that use a screen magnifier like ZoomText to easily scan the rows better. To pull off this feat, we can use the new CSS3 nth-child selector. See Enhancement tab for details.