Download code _ PHP and SQL-Based Train Scheduler App Using OOP
Train Scheduler App (PHP & MySQL)
Introduction
The Train Scheduler App is a simple web application developed using PHP OOP and MySQL Database. It helps train stations provide passengers with a platform to monitor upcoming or current train schedules. The app has a clean and user-friendly interface, designed with the Bootstrap v5 Framework. It includes useful features to make schedule management easy for station staff.
Features and Functionalities
- Add New Schedule ā Allows station staff to add train schedules.
- List All Schedules ā Displays all upcoming and current train schedules.
- Edit Schedule ā Enables staff to update train details.
- Delete Schedule ā Allows removal of outdated schedules.
- Real-Time Schedule Updates ā Automatically updates the estimated arrival time of trains.
How the Train Scheduler App Works
The Train Scheduler App is simple to use. It helps passengers monitor how long they need to wait for their train at a station. The schedules are managed by station staff, who can add train details such as arrival time, next destination, and travel duration.
Technologies Used
This application is developed using:
- HTML
- CSS
- JavaScript
- Bootstrap Framework
- PHP
- MySQL Database
How to Install and Run
Requirements
- Install a local web server like XAMPP.
- Download the Train Scheduler App source code.
Installation Steps
- Open the XAMPP Control Panel and start Apache and MySQL.
- Extract the downloaded source code file.
- Copy the extracted folder and paste it into the “htdocs” directory inside XAMPP.
- Open a web browser and visit:
- The database and tables will be created automatically on the first run.
No login or registration is required to use the app. You are free to modify the source code as needed to fit your project requirements.
index PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
<?php session_start(); require_once "Database.php"; $db = new Database(); if($_SERVER['REQUEST_METHOD'] == "POST"){ extract($_POST); if(!empty($_POST['id'])){ $update_data = $db->update($_POST); if($update_data){ $_SESSION['message'] = "Schedule has been updated successfully."; echo "<script>location.replace('./')</script>"; exit; } }else{ $insert_data = $db->insert($_POST); if($insert_data){ $_SESSION['message'] = "New Schedule has been added successfully."; echo "<script>location.replace('./')</script>"; exit; } } } if(isset($_GET['action'])){ switch ($_GET['action']){ case 'edit': $edit_data = $db->single_fetch($_GET['id']); // echo "<pre>"; // print_r($edit_data); // echo "</pre>"; break; case 'delete': $delete_data = $db->delete($_GET['id']); if($delete_data){ $_SESSION['message'] = "Schedule has been deleted successfully."; echo "<script>location.replace('./')</script>"; exit; } break; } } if(isset($_SESSION['message'])){ $message = $_SESSION['message']; unset($_SESSION['message']); } function format_num($num, $decimal= 0){ $num_ex = explode(".", $num); if($decimal > 0){ return number_format($num_ex[0], 2); }else{ $dec = isset($num_ex[1]) ? $num_ex[1] :0; $dec = abs($dec) > 0 ? $dec : ""; return number_format($num_ex[0], strlen($dec)); } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Train Scheduler App</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.6.1.min.js" integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ=" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-A3rJD856KowSb7dwlZdYEkO39Gagi7vIsF0jrRAoQmDKKtQBHUuLZ9AsSv4jD4Xa" crossorigin="anonymous"></script> </head> <body class=""> <nav class="navbar navbar-dark navbar-expand-lg bg-dark bg-gradient"> <div class="container"> <a class="navbar-brand" href="./">Train Scheduler App</a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav"> <li class="nav-item"> <a class="nav-link active" aria-current="page" href="./">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="real_time.php" target="_blank">Real-Time</a> </li> </ul> </div> </div> </nav> <div class="container my-5"> <div class="row"> <div class="col-lg-4 col-md-5 col-sm-12"> <div class="card shadow rounded-0"> <div class="card-header rounded-0"> <div class="card-title"><b><?= isset($edit_data['id']) ? "Edit" : '' ?> Schedule Form</b></div> </div> <div class="card-body"> <div class="container-fluid"> <form action="" id="sched-form" method="POST"> <input type="hidden" name="id" value="<?= isset($edit_data['id']) ? $edit_data['id'] : '' ?>"> <div class="mb-3"> <label for="code" class="control-label">Train Code</label> <input type="text" class="form-control rounded-0" id="code" name="code" value="<?= isset($edit_data['code']) ? $edit_data['code'] : '' ?>" required="required"> </div> <div class="mb-3"> <label for="name" class="control-label">Train Name</label> <input type="text" class="form-control rounded-0" id="name" name="name" value="<?= isset($edit_data['name']) ? $edit_data['name'] : '' ?>" required="required"> </div> <div class="mb-3"> <label for="destination" class="control-label">Destination</label> <textarea rows="3" class="form-control rounded-0" id="destination" name="destination" required="required"><?= isset($edit_data['destination']) ? $edit_data['destination'] : '' ?></textarea> </div> <div class="mb-3"> <label for="duration" class="control-label">Travel Duration (mins)</label> <input type="text" class="form-control rounded-0" id="duration" name="duration" value="<?= isset($edit_data['duration']) ? $edit_data['duration'] : '' ?>" required="required"> </div> <div class="mb-3"> <label for="eta" class="control-label">Arrival to Station Duration (mins)</label> <input type="text" class="form-control rounded-0" id="eta" name="eta" value="<?= isset($edit_data['eta']) ? $edit_data['eta'] : '' ?>" required="required"> </div> <?php if(isset($edit_data['status'])): ?> <div class="mb-3"> <label for="status" class="control-label">Status</label> <select name="status" id="status" class="form-select rounded-0" required="required"> <option value="0" <?= $edit_data['status'] == 0 ? 'selected' : '' ?>>Pending</option> <option value="1" <?= $edit_data['status'] == 1 ? 'selected' : '' ?>>Arrived to The Station</option> <option value="2" <?= $edit_data['status'] == 2 ? 'selected' : '' ?>>Departed from the station</option> </select> </div> <?php endif; ?> </form> </div> </div> <div class="card-footer py-3"> <div class="d-grid gap-2"> <button class="btn btn-primary btn-sm rounded-0" form="sched-form">Save Schedule</button> <?php if(isset($edit_data['id'])): ?> <button class="btn btn-light border btn-sm rounded-0" type="button" onclick="location.href = './'">Cancel</button> <?php endif; ?> </div> </div> </div> </div> <div class="col-lg-8 col-md-7 col-sm-12"> <?php if(isset($message)): ?> <div class="alert alert-success rounded-0 mb-2"> <div><?= $message ?></div> </div> <?php unset($message) ?> <?php endif; ?> <div class="card rounded-0 shadow"> <div class="card-header rounded-0"> <div class="card-title"><b>Schedule List</b></div> </div> <div class="card-body rounded-0"> <div class="container-fluid"> <table class="table table-striped table-bordered table-hover"> <thead> <tr> <th class="text-center">Added</th> <th class="text-center">Train</th> <th class="text-center">Destination</th> <th class="text-center">Duration</th> <th class="text-center">ETA</th> <th class="text-center">Action</th> </tr> </thead> <tbody> <?php $data = $db->get_results("SELECT * FROM `schedule_list` order by `status` asc, abs(unix_timestamp(`date_added`)) desc"); ?> <?php if($data->num_rows > 0): ?> <?php while($row = $data->fetch_assoc()): ?> <tr> <td class="p-1"><?= date("M d, Y g:i A", strtotime($row['date_added'])) ?></td> <td class="p-1"> <div class="lh-1"> <div><?= $row['name'] ?></div> <div><small><?= $row['code'] ?></small></div> </div> </td> <td class="p-1"><?= $row['destination'] ?></td> <td class="p-1 text-center"><?= format_num($row['duration']) ?> min(s)</td> <td class="p-1 text-center"> <?php if($row['status'] ==0): ?> <?= format_num($row['eta']) ?> min(s) <?php elseif($row['status'] == 1): ?> <small class="text-success">Arrived</small> <?php else: ?> <small class="text-muted">Departed</small> <?php endif; ?> </td> <td class="text-center"> <div class="dropdown"> <button class="btn btn-sm btn-light border border-dark dropdown-toggle rounded-0" type="button" data-bs-toggle="dropdown" aria-expanded="false"> Action </button> <ul class="dropdown-menu"> <li><a class="dropdown-item" href="./?action=edit&id=<?= $row['id'] ?>">Edit</a></li> <li><a class="dropdown-item" href="./?action=delete&id=<?= $row['id'] ?>" onclick="if(confirm('Are you sure to delete this schedule?') == false) event.preventDefault();">Delete</a></li> </ul> </div> </td> </tr> <?php endwhile; ?> <?php endif; ?> </tbody> </table> </div> </div> </div> </div> </div> </div> </body> </html> |
Database PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
<?php Class Database{ private $dbname; private $tblname; private $host; private $username; private $password; private $conn; function __construct(){ // Database Name $this->dbname = 'tsa_db'; // Table Name $this->tblname = 'schedule_list'; // Host Name $this->host = 'localhost'; // DB Username $this->username = 'root'; // Database Name $this->password = ''; $this->conn = new mysqli($this->host, $this->username, $this->password); // Creating the sample DB $db_sql = " CREATE DATABASE IF NOT EXISTS {$this->dbname}"; $this->conn->query($db_sql); if(!$this->conn->error){ if($this->conn->affected_rows >0){ $this->conn->close(); // Open Databse Connection $this->conn = new mysqli($this->host, $this->username, $this->password, $this->dbname); // Creating a sample table $tbl_sql = "CREATE TABLE IF NOT EXISTS `{$this->tblname}` ( `id` int(30) NOT NULL PRIMARY KEY AUTO_INCREMENT, `code` varchar(250) NOT NULL, `name` varchar(250) NOT NULL, `destination` varchar(250) NOT NULL, `duration` float(12,2) NOT NULL Default 0, `eta` float(12,2) NOT NULL Default 0, `status` tinyint(2) NOT NULL Default 0, `date_added` DATETIME NOT NULL Default CURRENT_TIMESTAMP)"; $this->conn->query($tbl_sql); if($this->conn->error){ die("Creating DB Table Failed. Error: ". $this->conn->error); } }else{ $this->conn->close(); // Open Databse Connection $this->conn = new mysqli($this->host, $this->username, $this->password, $this->dbname); } }else{ die("Creating DB Failed. Error: ". $this->conn->error); } } // Fetching Data From Database public function get_results($query= ""){ if(empty($query)){ // Returning Error if query String is Empty return json_encode(["error"=>"Query is empty"]); }else{ $query = $this->conn->query($query); if($this->conn->error){ return json_encode($this->conn); }else{ return $query; } } } public function sanitize_data($string=""){ if(!is_numeric($string)){ $string = addslashes($this->conn->real_escape_string($string)); } return $string; } public function insert($data=[]){ if(!is_array($data)){ die("The given data is not an Array. Data Given". $data); }else if(is_array($data) && count($data) <= 0){ die("The given data is null."); }else{ $idata = ""; $allowed_insert = ["code", "name","destination", "duration", "eta", "status", "date_added"]; foreach($data as $k => $v){ if(!in_array($k, $allowed_insert)) continue; $v = $this->sanitize_data($v); if(!empty($idata)) $idata .= ", "; $idata .="`{$k}` = '{$v}'"; } $insert = $this->conn->query("INSERT INTO `{$this->tblname}` set {$idata}"); if($insert){ return $this->conn->insert_id; }else{ return false; } } } public function update($data=[]){ if(!is_array($data)){ die("The given data is not an Array. Data Given". $data); }else if(is_array($data) && count($data) <= 0){ die("The given data is null."); }else{ $idata = ""; $allowed_insert = ["code", "name","destination", "duration", "eta", "status", "date_added"]; foreach($data as $k => $v){ if(!in_array($k, $allowed_insert)) continue; $v = $this->sanitize_data($v); if(!empty($idata)) $idata .= ", "; $idata .="`{$k}` = '{$v}'"; } $id = $this->sanitize_data($data['id']); $update = $this->conn->query("update`{$this->tblname}` set {$idata} where id = '{$id}'"); if($update){ return true; }else{ return false; } } } public function single_fetch($id = ""){ if(empty($id)){ die("The given id is empty."); }else{ $id =$this->sanitize_data($id); $get = $this->conn->query("SELECT * FROM `{$this->tblname}` where id = '{$id}'"); if(!$this->conn->error){ if($get->num_rows > 0) return $get->fetch_assoc(); else return []; }else{ die("An error occurred. Error: ". $this->conn->error); } } } public function delete($id = ""){ if(empty($id)){ die("The given id is empty."); }else{ $id =$this->sanitize_data($id); $delete = $this->conn->query("DELETE FROM `{$this->tblname}` where id = '{$id}'"); if($this->conn->error){ die("An error occurred. Error: ". $this->conn->error); } if($delete){ return true; }else{ return false; } } } // closing db connection function __destruct(){ $this->conn->close(); } } ?> |