My code performs CRUD operations in PHP using OOP and dynamic queries. How can I completely separate HTML tables from this code?
Db.php
class Db {
Protected $conn;
public $table;
public $fields;
public $values;
public $sql;
public $array_keys;
public $array_values;
//put your code here
public function __construct() {
$servername = "localhost";
$username = "root";
$password = "abdullah123";
try {
$this->conn = new PDO("mysql:host=$servername;dbname=Pathalogy_Lab", $username, $password);
// set the PDO error mode to exception
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
}
public function tableData($fields, $table) {
unset($fields['Patient_data']);
unset($fields['logout']);
unset($fields['insert_Report']);
$this->array_keys = array_keys($fields);
$this->array_values = array_values($fields);
$this->table = $table;
$this->sqlQuery();
}
public function tableColums() {
return "`" . implode("`, `", $this->array_keys) . "`";
}
public function tableValues() {
return "'" . implode("', '", $this->array_values) . "'";
}
public function sqlQuery() {
$sql = "INSERT INTO `{$this->table}` (" . $this->tableColums() . ") VALUES (" . $this->tableValues() . ");";
$stmt = $this->conn->prepare($sql);
$stmt->execute();
if (Patient == $this->table) {
$this->addPatient();
} elseif (Reports == $this->table) {
$this->addReport();
}
}
public function addPatient() {
header('Location: ../FrontEndFiles/insert_report.php');
}
public function addReport() {
header('Location: ../FrontEndFiles/Admin_Index.php');
}
public function logout() {
unset($_SESSION["dbuser_name"], $_SESSION["dbpassword"], $_SESSION["dbid"]);
session_unset();
Session_destroy();
if (!isset($_SESSION['dbuser_name']) || !isset($_SESSION['Patient_password']) || !isset($_SESSION['Patient_ID'])) {
header('Location: ../FrontEndFiles/Index.php');
exit();
}
}
}
Admin.php
class Admin extends Db {
public $data_id;
public function listRecord() {
if (isset($_SESSION['dbuser_name']) && isset($_SESSION['dbpassword'])) {
$query = "select Patient_ID, Patient_name, Patient_Address, Patient_Phone from Patient where Role= 'u'";
$result = $this->conn->prepare($query);
$result->execute();
if ($row_count > 0) {
// output data of each row
$this->detailsHeredocs();
$nust = $result->fetchall();
foreach ($nust as $row1) {
echo"
<tr>
<td> $row1[Patient_name] </td>
<td> $row1[Patient_Address] </td>
<td><a href='../Admin/AdminData.php?report_id=$row1[Patient_ID]'>Details</a></td>
</tr>";
}
echo <<<HTML
</tbody>
</table>
</div>
<script>
$(document).ready(function(){
$('#myTable').DataTable();
});
</script>
</body>
</html>
HTML;
}
}
}
public function Details($id) {
$this->data_id = $id;
if (isset($_SESSION['dbuser_name']) && isset($_SESSION['dbpassword'])) {
$query = "select Report_type, Report_Id, Report_Reults,Patient_name, Patient_Address, "
. "Patient_Phone from Patient join Reports WHERE "
. "Reports.Patient_ID=$this->data_id and Patient.Patient_ID=$this->data_id";
$result = $this->conn->prepare($query);
$row = $result->execute();
$row_count = $result->rowCount();
if ($row_count > 0) {
// output data of each row
$this->patientListHeredocs();
$nust = $result->fetchAll();
foreach ($nust as $row1) {
echo"
<tr>
<td> $row1[Patient_name] </td>
<td> $row1[Patient_Address] </td>
<td> $row1[Patient_Phone] </td>
<td> $row1[Report_type] </td>
<td> $row1[Report_Reults] </td>
<td><a href='../Admin /AdminData.php?del_id=$row1[Report_Id]'>Delete</a></td>
</tr>";
}
echo <<<HTML
</tbody>
</table>
</div>
<script>
$(document).ready(function(){
$('#myTable').DataTable();
});
</script>
</body>
</html>
HTML;
}
}
}
public function Delete($del) {
if (isset($_SESSION['dbuser_name']) && isset($_SESSION['dbpassword'])) {
$this->data_id = $del;
$query = "DELETE FROM Reports Where Report_Id=$this->data_id";
$result = $this->conn->prepare($query);
$result->execute();
$affected_rows = $result->rowCount();
if ($affected_rows == 1) {
header('Location: ../FrontEndFiles/Admin_Index.php');
} else {
echo 'No data DELEted';
}
}
}
public function DeletePatient($param) {
if (isset($_SESSION['dbuser_name']) && isset($_SESSION['dbpassword'])) {
$this->data_id = $param;
$query = "Delete from Patient WHERE Patient_ID=$this->data_id";
$result = $this->conn->prepare($query);
$result->execute();
header('Location: ../Admin/Admin_Index.php');
}
}
public function PatientList() {
if (isset($_SESSION['dbuser_name']) && isset($_SESSION['dbpassword'])) {
$query = "select Patient_ID, Patient_name, Patient_Address, Patient_Phone from Patient where Role= 'u'";
$result = $this->conn->prepare($query);
$result->execute();
$row_count = $result->rowCount();
if ($row_count > 0) {
// output data of each row
ob_start();
include("../FrontEndFiles/navBar.html");
include("../FrontEndFiles/sideBar.html");
$include = ob_get_contents();
ob_end_clean();
echo <<<HTML
{$include}
<div class="col-md-8">
<h2> Data</h2>
<table id="myTable" class="display">
<thead>
<tr>
<th>Patient Name</th>
<th>Action</th>
</tr>
</thead>
<tbody>
HTML;
$nust = $result->fetchAll();
foreach ($nust as $row1) {
echo"
<tr>
<td> $row1[Patient_name] </td>
<td><a href='../Admin/AdminData.php?deleteData=$row1[Patient_ID]'>Delete</a></td>
</tr>";
}
echo <<<HTML
</tbody>
</table>
</div>
<script>
$(document).ready(function(){
$('#myTable').DataTable();
});
</script>
</body>
</html>
HTML;
}
}
}
public function loginAdmin($login_admin) {
if (!empty($login_admin[0]) || !empty($login_admin[1])) {
$query = "SELECT * FROM Patient WHERE Patient_name = '$login_admin[0]' and Patient_password = $login_admin[1] and Role = 'a'";
$stmt = $this->conn->query($query);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo $row['Patient_name'];
echo $row['Patient_password'];
echo $row['Patient_ID'];
if ($row) {
$_SESSION["dbuser_name"] = $row['Patient_name'];
$_SESSION["dbpassword"] = $row['Patient_password'];
$_SESSION["dbid"] = $row['Patient_ID'];
header('Location: ../FrontEndFiles/Admin_Index.php');
} else {
header('Location: ../FrontEndFiles/AdminLogin.php');
}
} else {
header('Location: ../FrontEndFiles/AdminLogin.php');
}
}
public function detailsHeredocs() {
ob_start();
include("../FrontEndFiles/navBar.html");
include("../FrontEndFiles/sideBar.html");
$include = ob_get_contents();
ob_end_clean();
echo <<<HTML
{$include}
<div class="col-md-8">
<h2>Report's Data</h2>
<table id="myTable" class="display">
<thead>
<tr>
<th>Patient Name</th>
<th>Patient Address</th>
<th>Action</th>
</tr>
</thead>
<tbody>
HTML;
}
public function patientListHeredocs() {
ob_start();
include("../FrontEndFiles/navBar.html");
include("../FrontEndFiles/sideBar.html");
$include = ob_get_contents();
ob_end_clean();
echo <<<HTML
{$include}
<div class="col-md-8">
<h2>Data</h2>
<table id="myTable" class="display">
<thead>
<tr>
<th>Patient Name</th>
<th>Patient Address</th>
<th>Patient Phone</th>
<th>Report Type</th>
<th>Report Results</th>
<th>Action</th>
</tr>
</thead>
<tbody>
HTML;
}
}
completely separate HTML tables from this code
? do you not want Tables rendered on your page? \$\endgroup\$