Skip to content

Instantly share code, notes, and snippets.

@Oluwarufus
Last active September 28, 2022 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Oluwarufus/95d53d5601f36c5907d6a06db221fcb6 to your computer and use it in GitHub Desktop.
Save Oluwarufus/95d53d5601f36c5907d6a06db221fcb6 to your computer and use it in GitHub Desktop.
Enistic Task
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Enistic Dev Task</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 mx-auto">
<div class="card p-5 bg-light">
<h2 class="header-title text-center">Enistic Dev Task</h2>
<p class="text-center">by Rufus Oyemade</p>
<?php
/**
*
*/
class EnisticTask {
public $db_host;
public $db_user;
public $db_password;
public $db_name;
function __construct($db_host, $db_user, $db_password, $db_name) {
try {
$this->connection = new mysqli($db_host, $db_user, $db_password, $db_name);
$this->db_table = 'prices';
} catch (Exception $e) {
echo $this->errorAlert("Failed to connect to MySQL, please check the config");
die('Stopped.');
}
}
public function init() {
echo $this->createTable();
echo $this->seedDatabase();
echo $this->renderData();
}
private function createTable() {
$sql = "SHOW TABLES LIKE '" . $this->db_table . "'";
$result = $this->connection->query($sql);
if ($result->num_rows !== 1) {
$sql = "CREATE TABLE " . $this->db_table . " (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
description VARCHAR(255) NOT NULL,
price DECIMAL(6,4) NOT NULL
)";
try {
$this->connection->query($sql);
return $this->successAlert("Table created successfully");
} catch (Exception $e) {
return $this->errorAlert("Error creating table: " . $this->connection->error);
}
} else {
return $this->errorAlert("Table already exists");
}
}
private function seedDatabase() {
$sql = "SELECT * FROM " . $this->db_table;
$result = $this->connection->query($sql);
if ($result->num_rows === 0) {
$sql = "INSERT INTO " . $this->db_table . " (description, price)
VALUES ('Apples', 27), ('Oranges', 83), ('Bananas', 12)";
try {
$this->connection->query($sql);
return $this->successAlert("Table seeded successfully");
} catch (Exception $e) {
return $this->errorAlert("Error seeding table with sample data: " . $this->connection->error);
}
} else {
return $this->errorAlert("Table already seeded.");
}
}
private function renderData() {
$sql = "SELECT * FROM " . $this->db_table;
$result = $this->connection->query($sql);
echo "<table class='table'>
<thead>
<tr>
<th scope='col'>Description</th>
<th scope='col'>Price</th>
</tr>
</thead>
<tbody>";
if ($result->num_rows > 0) {
while ($item = $result->fetch_assoc()) {
echo "<tr>
<td>" . $item["description"] . "</td>
<td>" . $item["price"] . "</td>
</tr>";
}
} else {
echo "<tr><td>0 results</td></td>";
}
echo "</tbody></table";
}
private function successAlert($message) {
echo "<div class='alert alert-success' role='alert'> $message </div>";
}
private function errorAlert($message) {
echo "<div class='alert alert-warning' role='alert'> $message </div>";
}
function __destruct() {
$this->connection->close();
}
}
$run = new EnisticTask('localhost', 'root', 'pass', 'enistic');
$run->init();
?>
</div>
</div>
</div>
</div>
</body>
</html>
@Oluwarufus
Copy link
Author

Oluwarufus commented Sep 28, 2022

Of course, this is not a live project, but if it were, I will do the following (to mention a few):

  • I won't mix up PHP and HTML like this. Instead, I'd use a proper MVC framework like Laravel that will help with writing quality code, testing it, and separating of concerns. For example, the rendering can be via a VueJS SPA.
  • I won't hardcode the DB credentials as in the code, this will be hidden and secured in an environment (.env) file
  • For the rendering of data, I'd use backend pagination and/or datatable. This part may involve Javascript or a library like jQuery and will be helpful when the data grows.
  • Prepare my SQL statements esp. user inputs to prevent SQL injection.

Thank you for the consideration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment