Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active May 26, 2023 05:29
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 code-boxx/1b5bf34f587ec5a46c2bdad08e415ce0 to your computer and use it in GitHub Desktop.
Save code-boxx/1b5bf34f587ec5a46c2bdad08e415ce0 to your computer and use it in GitHub Desktop.
PHP MYSQL Timetable

PHP MYSQL TIMETABLE

https://code-boxx.com/simple-timetable-php-mysql/

NOTES

  1. Create a database and import 1-database.sql.
  2. Change the database settings in 2-lib-timetable.php to your own.
  3. Access 3a-timetable.php in the browser.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

-- (A) EVENTS TABLE
CREATE TABLE `events` (
`event_id` bigint(20) NOT NULL,
`event_txt` varchar(255) NOT NULL,
`event_row` varchar(255) NOT NULL,
`event_col` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `events`
ADD PRIMARY KEY (`event_id`);
ALTER TABLE `events`
MODIFY `event_id` bigint(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
-- (B) DUMMY DATA
INSERT INTO `events` (`event_id`, `event_txt`, `event_row`, `event_col`) VALUES
(1, 'Walk the doge.', '2', '2/3'),
(2, 'Feed the doge.', '4', '3/4'),
(3, 'Play with doge.', '6', '2');
<?php
class TimeTable {
// (A) CONSTRUCTOR - CONNECT TO DATABASE
private $pdo = null;
private $stmt = null;
public $error = "";
function __construct () {
$this->pdo = new PDO(
"mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET,
DB_USER, DB_PASSWORD, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
}
// (B) DESTRUCTOR - CLOSE DATABASE CONNECTION
function __destruct () {
if ($this->stmt!==null) { $this->stmt = null; }
if ($this->pdo!==null) { $this->pdo = null; }
}
// (C) HELPER FUNCTION - EXECUTE SQL QUERY
function query ($sql, $data=null) : void {
$this->stmt = $this->pdo->prepare($sql);
$this->stmt->execute($data);
}
// (D) GET ALL EVENTS
function get () {
$this->query("SELECT * FROM `events`");
return $this->stmt->fetchAll();
}
// (E) SAVE EVENT
function save ($txt, $row, $col, $id=null) {
// (E1) NEW EVENT
if ($id==null) {
$sql = "INSERT INTO `events` (`event_txt`, `event_row`, `event_col`) VALUES (?, ?, ?)";
$data = [$txt, $row, $col];
}
// (E2) EDIT EVENT
else {
$sql = "UPDATE `events` SET `event_txt`=?, `event_row`=?, `event_col`=? WHERE `event_id`=?";
$data = [$txt, $row, $col, $id];
}
$this->query($sql, $data);
return true;
}
// (F) DELETE EVENT
function del ($id) {
$this->query("DELETE FROM `events` WHERE `event_id`=?", [$id]);
return true;
}
}
// (G) DATABASE SETTINGS - CHANGE TO YOUR OWN!
define("DB_HOST", "localhost");
define("DB_NAME", "test");
define("DB_CHARSET", "utf8mb4");
define("DB_USER", "root");
define("DB_PASSWORD", "");
// (H) NEW TIMETABLE OBJECT
$_TT = new TimeTable();
<!DOCTYPE html>
<html>
<head>
<title>Time Table With CSS Grid</title>
<meta charset="utf-8">
<script src="3b-timetable.js"></script>
<link rel="stylesheet" href="3c-timetable.css">
</head>
<body>
<!-- (A) GENERATE TIMETABLE HERE -->
<div id="demo"></div>
<!-- (B) ATTACH TIMETABLE -->
<script>
timetable({
// (B1) REQUIRED
target: document.getElementById("demo"),
x: ["Morning", "Afternoon", "Night"],
y: ["Mon", "Tue", "Wed", "Thur", "Fri"],
// (B2) LOAD PHP LIBRARY & OUTPUT ENTRIES
data: [<?php
require "2-lib-timetable.php";
$entries = $_TT->get();
$len = count($entries);
$last = $len - 1;
if ($len>0) { for ($i=0; $i<$len; $i++) {
$entry = $entries[$i];
printf("{txt:'%s',row:'%s',col:'%s',color:'#696a07',bg:'#fffac0'}%s",
$entry["event_txt"], $entry["event_row"], $entry["event_col"],
$i!=$last ? "," : ""
);
}}
?>],
// (B3) OPTIONAL
gridX: "100px repeat(3, 1fr)",
gridY : "50px"
});
</script>
</body>
</html>
function timetable (instance) {
// (A) CSS
instance.target.classList.add("timetable");
if (instance.gridX == undefined) {
instance.gridX = `repeat(${instance.x.length+1}, 1fr)`;
}
instance.target.style.gridTemplateColumns = instance.gridX;
if (instance.gridY) {
instance.target.style.gridAutoRows = instance.gridY;
}
// (B) GENERATE CELLS HELPER FUNCTION
let celler = (data, css) => {
let cell = document.createElement("div");
cell.className = css;
if (typeof data == "string") { cell.innerHTML = data; }
else {
cell.innerHTML = data.txt;
cell.style = `grid-column:${data.col};grid-row:${data.row};color:${data.color};background:${data.bg}`;
if (instance.gridY) { cell.style.height = instance.gridY; }
if (data.click) { cell.onclick = data.click; }
}
instance.target.appendChild(cell);
};
// (C) FIRST ROW - EMPTY CELL | HEADER FOR X
celler("&nbsp;", "cell head");
for (let i of instance.x) { celler(i, "cell head"); }
// (D) FOLLOWING ROWS - HEADER FOR Y | EMPTY CELLS
for (let i=0; i<instance.y.length; i++) {
celler(instance.y[i], "cell head");
for (let j=0; j<instance.x.length; j++) { celler("&nbsp;", "cell"); }
}
// (E) ENTRIES
for (let i of instance.data) { celler(i, "cell entry"); }
}
/* (A) WHOLE PAGE */
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
/* (B) TIMETABLE WRAPPER */
.timetable {
display: grid;
position: relative;
}
/* (C) BLANK & HEADER CELLS */
.cell {
display: flex;
align-items: center;
padding: 10px;
border: 1px solid #efefef;
}
.head {
font-weight: 700;
justify-content: center;
color: #fff;
background: #343da3;
}
/* (D) ENTRY CELLS */
.entry {
position: absolute;
right: 0; left: 0; top: 0;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment