Employee Contacts (Part 5) – Building the Presentation Layer with PHP and HTML

  1. Building the MySQL database.
  2. Building a Data Abstraction Layer with PHP
  3. Building the Data Access Layer with PHP
  4. Building the Business Object Layer with PHP
  5. Building the Presentation Layer with PHP & HTML

Part 1 of this series covered building the MySQL database. In Part2, we built the data abstraction layer. With Part 3, we built our data access layer. At the data access layer, we made generated the queries to execute through the data abstraction layer. Part 4 of our series dealt with the business object layer and implementing one of our business rules. Finally, in Part 5, we are ready to begin our presentation layer. We will not be dealing with how to properly format the page, or creating forms. Our goal is to show you how to move data between the presentation layer and the business layer.

Reference Our Business Object Layer – Before we can begin using our presentation layer, we first have to set a reference to the Business Layer to get access to the methods that we will use.

require_once('business_objects/employees.bo.php');
// instantiate the business object
$bo_emp = new bo_employees();

Display List of Employees – One of the primary things we are going to want to do is to display a list of employees.

// call the get_all_employees method
$emps = $bo_emp->get_all_employees();

// loop throw our array and display the first and last name
while ($row = mysql_fetch_array($emps, MYSQL_ASSOC)){
    echo $row["first_name"] . ' ' . $row["last_name"] . '';
}

Display A Single Employee – Whether attempting to make modifications to an employee or looking at details about someone, the ability to display everything about a single employee is vital.

$id = $_GET["employee_id"];

$emp = $bo_emp->get_one_employee($id);

echo "First Name: ".$emp["first_name"];
echo "Last Name: ".$emp["last_name"];

Add A New Employee – Though it is possible to add a new employee using MySQL and phpMyAdmin, it will much easier to manager by creating a web form for this. You can either have the following code in a seperate file, or in the same php file as the form. Remember, if you are using the same page as the form to check to see if the form has been posted. You can easily do that by adding something like if($_POST).

$first = $_POST["txtFirst"];
$last = $_POST["txtLast"];
$dId = $_POST["selDepartment"];
$lId = $_POST["selLocation"];
$jId = $_POST["selTitle"];

$emp = array("first_name" => "$first"
    ,"last_name" => "$last"
    ,"department_id" => "$dId"
    ,"location_id" => "$lId"
    ,"jobtitle_id" => "$jId");

$bo_emp->add_new_employee($emp);

Modify An Employee – In our example, you could have an employee that moves to a new office, or maybe takes on a new position. At that point you would need to modify the employee details. In the real world, you would have details about phone numbers, email addresses, etc that need to be modified. We do this by calling our update method.

$id = $_POST["hdnId"];
$first = $_POST["txtFirst"];
$last = $_POST["txtLast"];
$dId = $_POST["selDepartment"];
$lId = $_POST["selLocation"];
$jId = $_POST["selTitle"];

$emp = array("employee_id" => "$id"
    ,"first_name" => "$first"
    ,"last_name" => "$last"
    ,"department_id" => "$dId"
    ,"location_id" => "$lId"
    ,"jobtitle_id" => "$jId");

$bo_emp->edit_existing_employee($emp);

Delete An Employee – There will always be times when you will need to remove an employee from the database. You may have a list of all employees with the option to either DELETE or MODIFY, or you may have a button on your employee details page that will allow you to remove the employee. But in order to remove the employee, you must call the delete method from the business layer.

$id = $_GET["employee_id"];

$emp = $do_emp->remove_employee($id);

No comments yet

Leave a reply