Advertisement
ostyleo

Untitled

May 11th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.49 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>P1 PHP Trains</title>
  6. </head>
  7. <form method="post">
  8.     <?php
  9.     $user = 'root';
  10.     $pass = '';
  11.     $dbName = 'pw';
  12.     $host = '127.0.0.1';
  13.     $db = new mysqli($host, $user, $pass, $dbName) or die("Can't connect");
  14.  
  15.     // Genereaza lista plecarilor on the fly
  16.     $query = "SELECT * FROM TRAIN GROUP BY LocatiePlecare";
  17.     $result = $db->query($query);
  18.     echo 'Plecare: <select name="Plecare">';
  19.     while ($rows = $result->fetch_assoc()) {
  20.         echo '<option value="', $rows['LocatiePlecare'], '">', $rows['LocatiePlecare'], '</option>';
  21.     }
  22.     echo '</select> <br>';
  23.  
  24.     // Genereaza lista sosirilor on the fly
  25.     $query = "SELECT * FROM TRAIN GROUP BY LocatieSosire";
  26.     $result = $db->query($query);
  27.  
  28.     echo 'Sosire: <select name="Sosire">';
  29.     while ($rows = $result->fetch_assoc()) {
  30.         echo '<option value="', $rows['LocatieSosire'], '">', $rows['LocatieSosire'], '</option>';
  31.     }
  32.     echo '</select><br>';
  33.  
  34.  
  35.     $dbh = null; //Closing DB
  36.     ?>
  37.  
  38.     Doriti si drumurile cu schimbare de tren? <input type="checkbox" name="withStop" value="true"><br>
  39.     <input type="submit" name="goCalculate" value="Calculate"/><br/><br/>
  40. </form>
  41. <?php
  42.  
  43. if (isset($_POST['goCalculate'])) {
  44.     $Sosire = $_POST['Sosire'];
  45.     $Plecare = $_POST['Plecare'];
  46.     $user = 'root';
  47.     $pass = '';
  48.     $dbName = 'pw';
  49.     $host = '127.0.0.1';
  50.     $db = new mysqli($host, $user, $pass, $dbName) or die("Can't connect");
  51.  
  52.     if (isset($_POST['withStop'])) {
  53.         calculate($Sosire, $Plecare, $db);
  54.         calculateWithStops($Sosire, $Plecare, $db);
  55.     } else {
  56.         calculate($Sosire, $Plecare, $db);
  57.     }
  58.  
  59.     $db = null; //Closing DB
  60.  
  61. }
  62.  
  63. function calculateWithStops($Sosire, $Plecare, $db)
  64. {
  65.     $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';
  66.     $result = $db->query($query);
  67.     if (mysqli_num_rows($result) > 0) {
  68.         while ($row = $result->fetch_assoc()) {
  69.             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/>';
  70.         }
  71.     } else {
  72.         echo 'Nu exista trenuri cu legatura pe aceasta ruta! <br/>';
  73.  
  74.     }
  75. }
  76.  
  77. function calculate($Sosire, $Plecare, $db)
  78. {
  79.     // Afiseaza toate trenurile directe
  80.     $query = 'SELECT * FROM TRAIN WHERE LocatiePlecare = "' . $Plecare . '" AND LocatieSosire = "' . $Sosire . '"';
  81.     $result = $db->query($query);
  82.     if (mysqli_num_rows($result) > 0) {
  83.         while ($row = $result->fetch_assoc()) {
  84.             echo $row['Numar'] . '. plecarea la ora: ' . $row['OraPlecare'] . ' din ' . $row['LocatiePlecare'] . ' si soseste in ' . $row['LocatieSosire'] . ' la ora: ' . $row['OraSosire'] . '<br/>';
  85.         }
  86.     } else {
  87.         echo 'Nu exista trenuri directe pe aceasta ruta! <br/>';
  88.  
  89.     }
  90. }
  91.  
  92. ?>
  93.  
  94. </body>
  95. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement