Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>P1 PHP Trains</title>
- </head>
- <form method="post">
- <?php
- $user = 'root';
- $pass = '';
- $dbName = 'pw';
- $host = '127.0.0.1';
- $db = new mysqli($host, $user, $pass, $dbName) or die("Can't connect");
- // Genereaza lista plecarilor on the fly
- $query = "SELECT * FROM TRAIN GROUP BY LocatiePlecare";
- $result = $db->query($query);
- echo 'Plecare: <select name="Plecare">';
- while ($rows = $result->fetch_assoc()) {
- echo '<option value="', $rows['LocatiePlecare'], '">', $rows['LocatiePlecare'], '</option>';
- }
- echo '</select> <br>';
- // Genereaza lista sosirilor on the fly
- $query = "SELECT * FROM TRAIN GROUP BY LocatieSosire";
- $result = $db->query($query);
- echo 'Sosire: <select name="Sosire">';
- while ($rows = $result->fetch_assoc()) {
- echo '<option value="', $rows['LocatieSosire'], '">', $rows['LocatieSosire'], '</option>';
- }
- echo '</select><br>';
- $dbh = null; //Closing DB
- ?>
- Doriti si drumurile cu schimbare de tren? <input type="checkbox" name="withStop" value="true"><br>
- <input type="submit" name="goCalculate" value="Calculate"/><br/><br/>
- </form>
- <?php
- if (isset($_POST['goCalculate'])) {
- $Sosire = $_POST['Sosire'];
- $Plecare = $_POST['Plecare'];
- $user = 'root';
- $pass = '';
- $dbName = 'pw';
- $host = '127.0.0.1';
- $db = new mysqli($host, $user, $pass, $dbName) or die("Can't connect");
- if (isset($_POST['withStop'])) {
- calculate($Sosire, $Plecare, $db);
- calculateWithStops($Sosire, $Plecare, $db);
- } else {
- calculate($Sosire, $Plecare, $db);
- }
- $db = null; //Closing DB
- }
- function calculateWithStops($Sosire, $Plecare, $db)
- {
- $query = 'SELECT T1.Numar AS T1Numar, T1.LocatiePlecare AS T1LocatiePlecare, T1.LocatieSosire AS T1LocatieSosire, T1.OraPlecare AS T1OraPlecare, T1.OraSosire AS T1OraSosire,T2.Numar AS T2Numar, T2.LocatiePlecare AS T2LocatiePlecare, T2.LocatieSosire AS T2LocatieSosire, T2.OraPlecare AS T2OraPlecare, T2.OraSosire AS T2OraSosire FROM train T1 INNER JOIN train T2 ON T1.LocatieSosire = T2.LocatiePlecare WHERE T1.LocatiePlecare = "' . $Plecare . '" AND T2.LocatieSosire = "' . $Sosire . '" AND T1.OraSosire < T2.OraPlecare';
- $result = $db->query($query);
- if (mysqli_num_rows($result) > 0) {
- while ($row = $result->fetch_assoc()) {
- echo $row['T1Numar'] . '. plecarea la ora: ' . $row['T1OraPlecare'] . ' din ' . $row['T1LocatiePlecare'] . ' cu schimbare la ' . $row['T1LocatieSosire'] . ' la ora: ' . $row['T1OraSosire'] . ' si ajunge la ' . $row['T2LocatieSosire'] . ' la ora ' . $row['T2OraSosire'] . '<br/>';
- }
- } else {
- echo 'Nu exista trenuri cu legatura pe aceasta ruta! <br/>';
- }
- }
- function calculate($Sosire, $Plecare, $db)
- {
- // Afiseaza toate trenurile directe
- $query = 'SELECT * FROM TRAIN WHERE LocatiePlecare = "' . $Plecare . '" AND LocatieSosire = "' . $Sosire . '"';
- $result = $db->query($query);
- if (mysqli_num_rows($result) > 0) {
- while ($row = $result->fetch_assoc()) {
- echo $row['Numar'] . '. plecarea la ora: ' . $row['OraPlecare'] . ' din ' . $row['LocatiePlecare'] . ' si soseste in ' . $row['LocatieSosire'] . ' la ora: ' . $row['OraSosire'] . '<br/>';
- }
- } else {
- echo 'Nu exista trenuri directe pe aceasta ruta! <br/>';
- }
- }
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement