$form_arrival_city = filter_input(INPUT_POST, "arrival_city", FILTER_SANITIZE_STRING);
$form_arrival_date = filter_input(INPUT_POST, "arrival_date", FILTER_SANITIZE_STRING);
$form_date_now = filter_input(INPUT_POST, "date_now", FILTER_SANITIZE_STRING);
+$action = filter_input(INPUT_POST, "action", FILTER_SANITIZE_STRING);
$oDepartureDate = new DateTime($form_departure_date);
$oArrivalDate = new DateTime($form_arrival_date);
$input_failure = true;
}
-if ($oArrivalDate <= $oDepartureDate) {
+if (!($action === "return_flight") && $oArrivalDate <= $oDepartureDate) {
echo "Arrival date is before departure date. <br>";
$input_failure = true;
}
?>
-<h1> Rechercher un vol </h1>
+<h1> Rechercher un vol <?php if ($action === "return_flight") { echo "retour"; } ?> </h1>
<form action="index.php" id="search" method="post">
<input type="hidden" name="form" value="search" />
+ <?php if ($action === "return_flight") { echo "<input type=\"hidden\" name=\"action\" value=\"return_flight\" />"; } ?>
<input type="hidden" name="date_now" value="<?php echo $form_date_now; ?>" />
<label> Départ : Ville -> </label>
<select size="1" name="departure_city" required>
<optgroup label="Sélectionner une ville">
<?php
- $sql_pquery = "select distinct VilleD from VOLS";
+ $sql_pquery = "select distinct VilleD from VOLS order by VilleD";
global $connection;
$connection->prepare_query($sql_pquery);
$connection->run_prepared_query();
<select size="1" name="arrival_city" required>
<optgroup label="Sélectionner une ville">
<?php
- $sql_pquery = "select distinct VilleA from VOLS";
+ $sql_pquery = "select distinct VilleA from VOLS order by VilleA";
global $connection;
$connection->prepare_query($sql_pquery);
$connection->run_prepared_query();
</optgroup>
</select>
<label> Date -> </label>
- <input type="datetime-local" name="arrival_date" <?php if (isset($form_arrival_date)) echo "value=\"$form_arrival_date\"";?> />
- <input type="submit" value="Valider">
+ <input type="datetime-local" name="arrival_date" <?php if (isset($form_arrival_date)) { echo "value=\"$form_arrival_date\""; } ?> />
+ <input type="submit" value="Rechercher">
</form>
<br>
<?php
if (!$input_failure) {
- global $connection;
- $sql_pquery = "select VOLS.NumVol as NumVol, VilleD, DateD, VilleA, DateA, Classe, round(CoutVol*CoeffPrix, 2) as Prix from VOLS, DEFCLASSES
- where DEFCLASSES.NumVol = VOLS.NumVol and
- DateD >= ? and VilleD = ? and DateA <= ? and VilleA = ?
- order by DateD, NumVol, Prix";
- $connection->prepare_query($sql_pquery);
- $connection->prepared_query_bind_param("ssss", array($form_departure_date, $form_departure_city, $form_arrival_date, $form_arrival_city));
- $connection->run_prepared_query();
- $connection->get_pquery_result();
- $rows = $connection->get_result_array();
- $connection->close_prepared_query();
- //var_dump($rows);
- if (empty($rows)) {
- echo "Aucun vol ne correspond aux critères de recherche. <br>";
- } else {
- echo "<table id=\"search\">\n";
- echo " <tr>\n";
- echo " <th>Numéro de vol</th>\n";
- echo " <th>Ville de départ</th>\n";
- echo " <th>Date de départ</th>\n";
- echo " <th>Ville d'arrivée</th>\n";
- echo " <th>Date d'arrivée</th>\n";
- echo " <th>Classe</th>\n";
- echo " <th>Prix d'une place</th>\n";
- echo " <th>Réserver</th>\n";
- echo " </tr>\n";
- foreach ($rows as $row) {
- echo " <tr>\n";
- echo " <td>" . $row['NumVol'] . "</td>\n";
- echo " <td>" . $row['VilleD'] . "</td>\n";
- echo " <td>" . $row['DateD'] . "</td>\n";
- echo " <td>" . $row['VilleA'] . "</td>\n";
- echo " <td>" . $row['DateA'] . "</td>\n";
- echo " <td>" . $row['Classe'] . "</td>\n";
- echo " <td>" . $row['Prix'] . "€</td>\n";
- echo " <td>
+ global $connection;
+ $sql_pquery = "select VOLS.NumVol as NumVol, VilleD, DateD, VilleA, DateA, DEFCLASSES.Classe, round(CoutVol*CoeffPrix, 2) as Prix, CapAv
+ from VOLS join DEFCLASSES on DEFCLASSES.NumVol = VOLS.NumVol
+ join AVIONS on AVIONS.NumAv = VOLS.NumAv
+ where DateD >= ? and VilleD = ? and DateA <= ? and VilleA = ?
+ order by DateD, NumVol, Prix";
+ $connection->prepare_query($sql_pquery);
+ $connection->prepared_query_bind_param("ssss", array($form_departure_date, $form_departure_city, $form_arrival_date, $form_arrival_city));
+ $connection->run_prepared_query();
+ $connection->get_pquery_result();
+ $flights = $connection->get_result_array();
+ $connection->close_prepared_query();
+ //var_dump($flights);
+ if (empty($flights)) {
+ echo "Aucun vol ne correspond aux critères de recherche. <br>";
+ } else {
+ if ($action === "return_flight" && isset($_SESSION['return_flight_nb_place'])) {
+ $nb_places = $_SESSION['return_flight_nb_place'];
+ unset($_SESSION['return_flight_nb_place']);
+ } else {
+ $nb_places = 1;
+ }
+ echo "<table id=\"search\">\n";
+ echo " <tr>\n";
+ echo " <th>Numéro de vol</th>\n";
+ echo " <th>Ville de départ</th>\n";
+ echo " <th>Date de départ</th>\n";
+ echo " <th>Ville d'arrivée</th>\n";
+ echo " <th>Date d'arrivée</th>\n";
+ echo " <th>Classe</th>\n";
+ echo " <th>Prix d'une place</th>\n";
+ echo " <th>Réserver</th>\n";
+ echo " </tr>\n";
+ foreach ($flights as $flight) {
+ $sql_pquery = "select sum(NbPlaces) as BookedPlaces from RESERVATIONS where NumVol = ?";
+ $connection->prepare_query($sql_pquery);
+ $connection->prepared_query_bind_param("s", array($flight['NumVol']));
+ $connection->run_prepared_query();
+ $connection->get_pquery_result();
+ $rows = $connection->get_result_array();
+ $connection->close_prepared_query();
+ $booked_places = $rows[0][0];
+ if (is_null($booked_places)) {
+ $booked_places = 0;
+ }
+ if (isset($flight['CapAv']) && isset($booked_places)) {
+ $free_places = $flight['CapAv'] - $booked_places;
+ } else {
+ $free_places = "Inconnu";
+ }
+ echo " <tr>\n";
+ echo " <td>" . $flight['NumVol'] . "</td>\n";
+ echo " <td>" . $flight['VilleD'] . "</td>\n";
+ echo " <td>" . $flight['DateD'] . "</td>\n";
+ echo " <td>" . $flight['VilleA'] . "</td>\n";
+ echo " <td>" . $flight['DateA'] . "</td>\n";
+ echo " <td>" . $flight['Classe'] . "</td>\n";
+ echo " <td>" . $flight['Prix'] . "€</td>\n";
+ echo " <td>
+ Nombre de places libres : " . $free_places . "
<form action=\"index.php\" id=\"booking\" method=\"post\">
<input type=\"hidden\" name=\"form\" value=\"booking\" />
- <input type=\"hidden\" name=\"flight_id\" value=\"" . $row['NumVol'] . "\" />
- <input type=\"hidden\" name=\"class_name\" value=\"" . $row['Classe'] . "\" />
- <input type=\"hidden\" name=\"place_price\" value=\"" . $row['Prix'] . "\" />
+ <input type=\"hidden\" name=\"flight_id\" value=\"" . $flight['NumVol'] . "\" />
+ <input type=\"hidden\" name=\"class_name\" value=\"" . $flight['Classe'] . "\" />
+ <input type=\"hidden\" name=\"place_price\" value=\"" . $flight['Prix'] . "\" />
<label> Place(s) : </label>
- <input type=\"number\" name=\"nb_place\" min=\"1\" max=\"9\" value=\"1\" required/>
- <label> Vol retour : </label>
- <input type=\"checkbox\" name=\"return_flight\" checked required/>
- <input type=\"submit\" value=\"Réserver\">
- </form>
- </td>\n";
- echo " </tr>\n";
- }
- echo "</table>";
- }
-}
+ <input type=\"number\" name=\"nb_place\" min=\"1\" max=\"9\" value=\"" . $nb_places . "\" required/>\n";
+ if ($action !== "return_flight") {
+ echo " <label> Vol retour : </label>\n";
+ echo " <input type=\"checkbox\" name=\"return_flight\" checked/>\n";
+ }
+ if ($free_places !== 0 || $free_places === "Inconnu") {
+ echo " <input type=\"submit\" value=\"Réserver\">\n";
+ }
+ echo " </form>
+ </td>\n";
+ echo " </tr>\n";
+ }
+ echo "</table>\n";
+ }
+ }
?>