LAB11 PHP Ma026
LAB11 PHP Ma026
MVC
Que-1.
• Implement simple employee information management functionality using MVC. Manage Employee
Data like Employee ID, name, department.Provide insert, view, update and delete functionality using
appropriate design
• Also Manage Leaves Taken by Employee: id, emp-id, leave date, leave reason, leave-status. Provide
facility to request leave, approve disapprove leave and list leave records.
File Strucutre:
Code:
1 MA026
Index.php:
<?php
require_once './Controller/EmployeeController.php';
use lab11_php_ma026\Controller\EmployeeController;
EmployeeController::All();
Config.php:
<?php
class Config
{
protected const HOST = "localhost";
protected const DATABASE = "employee_leave";
protected const USERNAME = "root";
protected const PASSWORD = "";
protected const PORT = 3308;
}
Model\Database.php
<?php
require_once __DIR__ . '/../config.php';
use LAB11\Config;
use PDO;
use PDOException;
2 MA026
return self::$pdo;
}
}
Model\EmployeeModel.php
<?php
use PDO;
use PDOException;
class EmployeeModel
{
private function __construct()
{
}
Model\LeaveModel.php
<?php
use lab11_php_ma026\Model\Database;
use PDOException;
class LeaveModel
{
public function getAllLeaves()
{
try {
$pdo = Database::getInstance();
$query = "SELECT * FROM leaves";
$stmt = $pdo->query($query);
return $stmt->fetchAll();
} catch (PDOException $e) {
die("Database error: " . $e->getMessage());
}
}
4 MA026
public function requestLeave($data)
{
try {
$pdo = Database::getInstance();
$query = "INSERT INTO leaves (empid, leave_date,
leave_reason, leave_status) VALUES (?, ?, ?, ?)";
$stmt = $pdo->prepare($query);
$stmt->execute([$data['empid'], $data['leave_date'],
$data['leave_reason'], 'Pending']);
return true;
} catch (PDOException $e) {
die("Database error: " . $e->getMessage());
}
}
Controller\EmployeeController.php
<?php
use LAB11\Controllers\LeaveController;
5 MA026
use LAB11\Model\EmployeeModel;
<?php
use lab11_php_ma026\Models\LeaveModel;
class LeaveController
{
public static function listLeaves()
{
$leaveModel = new LeaveModel();
$leaves = $leaveModel->getAllLeaves();
$success = $leaveModel->requestLeave($data);
if ($success) {
header("Location: index.php?page=leave&action=list");
} else {
die("Failed to request leave.");
}
}
$success = $leaveModel->approveLeave($leaveId);
if ($success) {
echo <<<EOL
<script>
alert('leave approved...')
</script>
EOL;
} else
echo <<<EOL
7 MA026
<script>
alert('leave rejected...')
</script>
EOL;
}
$success = $leaveModel->disapproveLeave($leaveId);
if ($success) {
echo <<<EOL
<script>
alert('leave disapproved...')
</script>
EOL;
} else
echo <<<EOL
<script>
alert('failed to disapprove leave...')
</script>
EOL;
}
}
View\employee\list.php
<?php
use lab11_php_ma026\Controller\EmployeeController;
use lab11_php_ma026\Model\EmployeeModel;
if (isset($_POST['view'])) {
EmployeeController::ViewById($_POST['empid']);
}
if (isset($_POST['update'])) {
$empid = $_POST['empid'];
$employee = EmployeeModel::ViewById($_POST['empid']);
EmployeeController::UpdateView($empid, $employee['name'],
$employee['department']);
}
if (isset($_POST['delete'])) {
$empid = $_POST['empid'];
EmployeeController::Delete($empid);
}
if (isset($_POST['insert'])) {
header("location:./View/employee/add.php");
}
unset($_POST);
?>
8 MA026
<!DOCTYPE html>
<html>
<head>
<title>Employee List</title>
</head>
<body>
<h1>Employee List</h1>
<table>
<thead>
<tr>
<th>Employee ID</th>
<th>Name</th>
<th>Department</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($employees as $employee) : ?>
<tr>
<td><?php echo $employee['empid']; ?></td>
<td><?php echo $employee['name']; ?></td>
<td><?php echo $employee['department']; ?></td>
<td>
<table>
<tr>
<td>
<form action="index.php?page=employee&action=view" method="post">
<input type="hidden" name="empid" value="<?php echo $employee['empid'];
?>">
<button type="submit" name="view">View</button>
</form>
</td>
<td>
<form action="./" method="post">
<input type="hidden" name="empid" value="<?php echo
$employee['empid']; ?>">
<button type="submit" name="update">Update</button>
</form>
</td>
<td>
<form action="index.php?page=employee&action=delete" method="post">
<input type="hidden" name="empid" value="<?php echo
$employee['empid']; ?>">
<button type="submit" name="delete">Delete</button>
</form>
</td>
</tr>
</table>
</td>
9 MA026
</tr>
<?php endforeach; ?>
</tbody>
</table>
<form action="#" method="post">
<button type="submit" name="insert">Add Employee</button>
</form>
</body>
</html>
View\employee\view.php
<!DOCTYPE html>
<html>
<head>
<title>View Employee</title>
</head>
<body>
<h1>View Employee</h1>
<p><strong>Employee ID:</strong> <?php echo $employee['empid']; ?></p>
<p><strong>Name:</strong> <?php echo $employee['name']; ?></p>
<p><strong>Department:</strong> <?php echo $employee['department']; ?></p>
<a href="index.php?page=employee&action=edit&id=<?php echo $employee['empid'];
?>">Edit</a>
<a href="index.php?page=employee&action=delete&id=<?php echo $employee['empid'];
?>">Delete</a>
</body>
</html>
View\employee\Add.php
<?php
use lab11_php_ma026\Controller\EmployeeController;
<head>
<title>Add Employee</title>
</head>
<body>
10 MA026
<h1>Add Employee</h1>
<form action="" method="post">
<input type="hidden" name="empid">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br>
<label for="department">Department:</label>
<input type="text" id="department" name="department" required><br>
<button type="submit" name="Insert">Insert</button>
</form>
</body>
</html>
View\employee\edit.php
<?php
use lab11_php_ma026\Controller\EmployeeController;
if (isset($_POST['UpdateData'])) {
EmployeeController::Update($empid, $_POST['name'],
$_POST['department']);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit Employee</title>
</head>
<body>
<h1>Edit Employee</h1>
<form action="" method="post">
<input type="hidden" name="empid" value="<?php echo $empid; ?>">
<label for="name">Name:</label>
<input type="text" id="name" name="name" value="<?php echo $name; ?>" required><br>
<label for="department">Department:</label>
<input type="text" id="department" name="department" value="<?php echo $department;
?>" required><br>
<button type="submit" name="UpdateData">UpdateData</button>
</form>
</body>
</html>
View\leave\list.php
<?php
11 MA026
use lab11_php_ma026\Controllers\LeaveController;
if (isset($_POST['approve'])) {
LeaveController::approveLeave($_POST['leave_id']);
}
if (isset($_POST['disapprove']))
LeaveController::disapproveLeave($_POST['leave_id']);
if (isset($_POST['request_leave']))
header("location:request.php");
?>
<!DOCTYPE html>
<html>
<head>
<title>Leave List</title>
</head>
<body>
<h1>Leave List</h1>
<table>
<thead>
<tr>
<th>Leave ID</th>
<th>Employee ID</th>
<th>Leave Date</th>
<th>Leave Reason</th>
<th>Leave Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($leaves as $leave) : ?>
<tr>
<td><?php echo $leave['leave_id']; ?></td>
<td><?php echo $leave['empid']; ?></td>
<td><?php echo $leave['leave_date']; ?></td>
<td><?php echo $leave['leave_reason']; ?></td>
<td><?php echo $leave['leave_status']; ?></td>
<td>
<table>
<tr>
<td>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="hidden" name="leave_id" value="<?php echo $leave['leave_id'];
?>">
<button type="submit" name="approve">Approve</button>
</form>
</td>
<td>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="hidden" name="leave_id" value="<?php echo $leave['leave_id'];
?>">
12 MA026
<button type="submit" name="disapprove">Disapprove</button>
</form>
</td>
</tr>
</table>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<button type="submit" name="request_leave">Request Leave</button>
</form>
</body>
</html>
View\leave\request.php
<!DOCTYPE html>
<html>
<head>
<title>Request Leave</title>
<!-- Include Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
<h1 class="text-center">Request Leave</h1>
<form action="index.php?page=leave&action=submit_request" method="post">
<!-- Leave request form fields -->
<div class="form-group">
<label for="leave_date">Leave Date:</label>
<input type="date" class="form-control" id="leave_date" name="leave_date" required>
</div>
<div class="form-group">
<label for="leave_reason">Leave Reason:</label>
<textarea class="form-control" id="leave_reason" name="leave_reason" rows="4"
required></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit Request</button>
</form>
</div>
</html>
13 MA026
Output:
Add Employee
List Employee
14 MA026
View Employee
Edit Employee
15 MA026
Delete Employee
After delete
Leave List
Approved Leave
16 MA026
Disapprove Leave
17 MA026