Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $errors = []; //To store errors
- $result = array(); //Pass back the data to `form.php`
- //Overall outline of the question
- // peopleOnBus = people on the bus
- // numberOfPeopleGettingOn = people who want to get on the bus
- // busMaxCapacity = Total capacity
- $peopleOnBus = 0;
- // extract the data from the form
- $busMaxCapacity = $_POST['busMaxCapacity'];
- $numberOfPeopleGettingOn = $_POST['numberOfPeopleGettingOn'];
- $numberOfPeopleGettingOff = $_POST['numberOfPeopleGettingOff'];
- //Process
- $peopleCurrentlyOnBus = $peopleOnBus + $numberOfPeopleGettingOn - $numberOfPeopleGettingOff;
- if($numberOfPeopleGettingOff > $busMaxCapacity)
- {
- $errors['invalid'] = 'The values inserted are not valid. Go back to the previous page!';
- }
- else if ($numberOfPeopleGettingOff > $peopleOnBus)
- {
- $errors['invalid'] = "The values inserted are not valid. Go back to the previous page!";
- }
- else
- {
- //$result['capacity'] = "The bus has a capacity of $busMaxCapacity";
- // Question 1: Capacity is less than total people
- if ($peopleCurrentlyOnBus > $busMaxCapacity)
- {
- $difference = $peopleCurrentlyOnBus - $busMaxCapacity;
- $gotOn = $numberOfPeopleGettingOn - $difference;
- $result['comment'] = "$gotOn got on the bus, but $difference could not get on";
- $result['freeSeats'] = $busMaxCapacity - $gotOn;
- $result['takenSeat'] = $peopleCurrentlyOnBus;
- }
- //Question 2: Spare seats
- else if ($peopleCurrentlyOnBus < $busMaxCapacity)
- {
- $spareSeats = $busMaxCapacity - $peopleCurrentlyOnBus;
- //echo "$peopleCurrentlyOnBus people got on the bus, and there were $spareSeats spare seats";
- }
- // Question 3: Total capacity = total people
- else
- {
- //echo "$numberOfPeopleGettingOn people tried to get on the bus";
- //echo "<br>";
- //echo "Everyone could get on the bus yay";
- }
- }
- header("content-type:application/json");
- echo json_encode($result);
- // present the result
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement