Advertisement
Guest User

Untitled

a guest
Aug 24th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. <?php
  2. $errors = []; //To store errors
  3. $result = array(); //Pass back the data to `form.php`
  4.  
  5.  
  6. //Overall outline of the question
  7. // peopleOnBus = people on the bus
  8. // numberOfPeopleGettingOn = people who want to get on the bus
  9. // busMaxCapacity = Total capacity
  10.  
  11. $peopleOnBus = 0;
  12. // extract the data from the form
  13. $busMaxCapacity = $_POST['busMaxCapacity'];
  14. $numberOfPeopleGettingOn = $_POST['numberOfPeopleGettingOn'];
  15. $numberOfPeopleGettingOff = $_POST['numberOfPeopleGettingOff'];
  16.  
  17. //Process
  18. $peopleCurrentlyOnBus = $peopleOnBus + $numberOfPeopleGettingOn - $numberOfPeopleGettingOff;
  19.  
  20. if($numberOfPeopleGettingOff > $busMaxCapacity)
  21. {
  22. $errors['invalid'] = 'The values inserted are not valid. Go back to the previous page!';
  23. }
  24.  
  25. else if ($numberOfPeopleGettingOff > $peopleOnBus)
  26. {
  27. $errors['invalid'] = "The values inserted are not valid. Go back to the previous page!";
  28. }
  29.  
  30. else
  31. {
  32. //$result['capacity'] = "The bus has a capacity of $busMaxCapacity";
  33.  
  34. // Question 1: Capacity is less than total people
  35. if ($peopleCurrentlyOnBus > $busMaxCapacity)
  36. {
  37. $difference = $peopleCurrentlyOnBus - $busMaxCapacity;
  38.  
  39. $gotOn = $numberOfPeopleGettingOn - $difference;
  40. $result['comment'] = "$gotOn got on the bus, but $difference could not get on";
  41. $result['freeSeats'] = $busMaxCapacity - $gotOn;
  42. $result['takenSeat'] = $peopleCurrentlyOnBus;
  43. }
  44.  
  45. //Question 2: Spare seats
  46. else if ($peopleCurrentlyOnBus < $busMaxCapacity)
  47. {
  48. $spareSeats = $busMaxCapacity - $peopleCurrentlyOnBus;
  49. //echo "$peopleCurrentlyOnBus people got on the bus, and there were $spareSeats spare seats";
  50. }
  51.  
  52. // Question 3: Total capacity = total people
  53. else
  54. {
  55. //echo "$numberOfPeopleGettingOn people tried to get on the bus";
  56. //echo "<br>";
  57. //echo "Everyone could get on the bus yay";
  58. }
  59.  
  60. }
  61.  
  62. header("content-type:application/json");
  63. echo json_encode($result);
  64.  
  65.  
  66. // present the result
  67.  
  68.  
  69. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement