Displaying Database data using Class, Object, Constructor, Destructor in PHP MySql
<?php
class Employee
{
private $conn;
public function __construct()
{
$this->conn = new mysqli("localhost", "root", "", "database_ci");
if ($this->conn->connect_error) {
die("Connection Failed : " . $this->conn->connect_error);
}
}
public function __destruct()
{
$this->conn->close();
}
public function getAllEmployees()
{
$data = [];
$sql = "SELECT * FROM register";
$result = $this->conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}
return $data;
} else {
return $data;
}
}
public function getMaxSalaryEmployee()
{
$data = [];
$sql = "SELECT * FROM register ORDER BY salary DESC LIMIT 1";
$result = $this->conn->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
return $row;
} else {
return $data;
}
}
public function getMinSalaryEmployee()
{
$data = [];
$sql = "SELECT * FROM register ORDER BY salary ASC LIMIT 1";
$result = $this->conn->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
return $row;
} else {
return $data;
}
}
}
<?php
include("Constructor_Destructor.php");
$obj = new Employee();
$empList = $obj->getAllEmployees();
// echo "<pre>";
// print_r($empList);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>List of Employees</title>
</head>
<body>
<h1>List of Employees</h1>
<?php
if (count($empList) > 0) {
?>
<table border="2px">
<tr>
<th>id</th>
<th>name</th>
<th>email</th>
<th>phone</th>
<th>language</th>
<th>gender</th>
<th>qualification</th>
<th>image</th>
<th>added_on</th>
<th>updated_on</th>
<th>status</th>
</tr>
<?php
foreach ($empList as $emp) {
?>
<tr>
<td><?php echo $emp["id"]; ?></td>
<td><?php echo $emp["name"]; ?></td>
<td><?php echo $emp["email"]; ?></td>
<td><?php echo $emp["phone"]; ?></td>
<td><?php echo $emp["language"]; ?></td>
<td><?php echo $emp["gender"]; ?></td>
<td><?php echo $emp["qualification"]; ?></td>
<td><?php echo $emp["image"]; ?></td>
<td><?php echo $emp["added_on"]; ?></td>
<td><?php echo $emp["updated_on"]; ?></td>
<td><?php echo $emp["status"]; ?></td>
</tr>
<?php
}
?>
</table>
<?php } ?>
</body>
</html>
<?php
include("Constructor_Destructor.php");
$obj = new Employee();
$empList = $obj->getAllEmployees();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>List of Employees</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<?php
if (count($empList) > 0) {
foreach ($empList as $x) {
?>
<div class="col-md-3 bg-success" style="margin: 10px;">
<p><b>ID</b>: <?php echo $x["id"]; ?></p>
<p><b>NAME</b>: <?php echo $x["name"]; ?></p>
<p><b>EMAIL</b>: <?php echo $x["email"]; ?></p>
<p><b>PHONE</b>: <?php echo $x["phone"]; ?></p>
<p><b>LANGUAGE</b>: <?php echo $x["language"]; ?></p>
<p><b>GENDER</b>: <?php echo $x["gender"]; ?></p>
<p><b>QUALIFICATION</b>: <?php echo $x["qualification"]; ?></p>
<p><b>IMAGE</b>: <?php echo $x["image"]; ?></p>
<p><b>ADDED ON</b>: <?php echo $x["added_on"]; ?></p>
<p><b>UPDATED ON</b>: <?php echo $x["updated_on"]; ?></p>
<p><b>STATUS</b>: <?php echo $x["status"]; ?></p>
</div>
<?php
}
}
?>
</div>
</div>
</body>
</html>
Comments
Post a Comment