Guest User

Untitled

a guest
Aug 6th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.53 KB | None | 0 0
  1. <?php
  2. //db connection
  3.  
  4.  
  5. $username = 'root';
  6. $servername = 'localhost';
  7. $password = '';
  8. $dbport = '3306';
  9. $database = 'alligator_db';
  10.  
  11. // Reads the variables sent via POST from our gateway
  12. $sessionId = $_POST["sessionId"];
  13. $serviceCode = $_POST["serviceCode"];
  14. $phoneNumber = $_POST["phoneNumber"];
  15. $text = $_POST["text"];
  16.  
  17. //set default level to zero
  18.  
  19. //Definition of all functions to be used
  20. function printText($response){
  21. echo $response;
  22. }
  23. //First menu to be displayed on querying SSD CODE
  24. function initialMenu(){
  25. $menu = "CON Choose your county\n";
  26. $menu .= "1. Turkana\n";
  27. $menu .= "2. Wajir \n";
  28. $menu .= "3. Isiolo \n";
  29. printText($menu);
  30. }
  31.  
  32. //when user selects any county
  33. function secMenu(){
  34. $menu = "CON Choose what you want to view\n";
  35. $menu .= "1. Weather Updates\n";
  36. $menu .= "2. Farming Information\n";
  37. $menu .= "3. Ground Water\n";
  38. printText($menu);
  39. }
  40.  
  41. if($text == ""){
  42. //function call to initial menu
  43. initialMenu();
  44. }
  45.  
  46. else if($text =="1") {
  47. secMenu();
  48. }
  49. //shows a choice of the first menu code below shows second menu
  50.  
  51.  
  52. else if( $text=="1*1") {
  53. //$disp = "SELECT rainfall FROM weather_tbl WHERE county_id='1'\n";
  54. //$disp = "SELECT temperature FROM weather_tbl WHERE county_id='1'\n";
  55.  
  56. try{
  57. //create connection
  58. $db = new mysqli($servername, $username, $password, $database, $dbport);
  59. //checks connection if there is error ends ussd connection
  60. if ($db->connect_error){
  61. ('Content-type:text/plain');
  62. //log error to ussdcall and end call
  63. $filename = "F:/aligator/log.txt";
  64. file_put_contents($filename, "no db connection");
  65. die("End a database error was encountered. Contact admin!");
  66. }
  67. $disp = "SELECT humidity,rainfall,temperature FROM weather_tbl WHERE county_id='1'\n";
  68.  
  69. $result = $db->query($disp);
  70. if($result->num_rows > 0){
  71. //output data of each row
  72. while ($row = $result->fetch_assoc()){
  73. $rainfall = $row["rainfall"];
  74. $humidity = $row["humidity"];
  75. $temperature = $row["temperature"];
  76. }
  77.  
  78.  
  79.  
  80. $response = "END weather today is\n";
  81. $response .= "rainfall ".$rainfall ."\n";
  82. $response .= "humidity ".$humidity ."\n";
  83. $response .= "temperature ".$temperature ."\n";
  84. }
  85. else{
  86. $response = "0 results";
  87. }
  88. //$menu = "END Thankyou for using our services weather today is ".$menu."\n";
  89. printText($response);
  90. }
  91. catch(Exception $e) {
  92. echo 'Message: ' .$e->getMessage();
  93.  
  94.  
  95. $filename = "F:/aligator/log.txt";
  96. file_put_contents($filename, $e->getMessage());
  97.  
  98.  
  99. //printText($response)
  100. //$menu = "END Thankyou for using our services\n";
  101. //printText($menu)
  102.  
  103. }
  104. }
  105.  
  106. else if ( $text == "1*2" ) {
  107. try{
  108. //create connection
  109. $db = new mysqli($servername, $username, $password, $database, $dbport);
  110. //checks connection if there is error ends ussd connection
  111. if ($db->connect_error){
  112. ('Content-type:text/plain');
  113. //log error to ussdcall and end call
  114. echo 'connection failed';
  115. $filename = "F:/aligator/log.txt";
  116. file_put_contents($filename, "no db connection");
  117. die("End a database error was encountered. Contact admin!");
  118. }
  119. $disp = "SELECT message FROM farming_info_tbl WHERE message_id='1'\n";
  120.  
  121. $result = $db->query($disp);
  122. if ($result->num_rows > 0) {
  123. // output data of each row
  124. while($row = $result->fetch_assoc()) {
  125. $message = $row["message"];
  126.  
  127. $response = "END Our analysis today\n";
  128. $response .= "message ".$message ."\n";
  129. }
  130. } else {
  131. echo "0 results";
  132. }
  133.  
  134. $db->close();
  135. }
  136. catch(Exception $e) {
  137. echo 'Message: ' .$e->getMessage();
  138.  
  139.  
  140. $filename = "F:/aligator/log.txt";
  141. file_put_contents($filename, $e->getMessage());
  142.  
  143.  
  144. }
  145.  
  146. }
  147.  
  148.  
  149.  
  150.  
  151.  
  152. if($text =="2"){
  153.  
  154. secMenu();
  155. }
  156.  
  157. else if($text == "2*1") {
  158. //$disp = "SELECT rainfall FROM weather_tbl WHERE county_id='1'\n";
  159. //$disp = "SELECT temperature FROM weather_tbl WHERE county_id='1'\n";
  160.  
  161. try{
  162. //create connection
  163. $db = new mysqli($servername, $username, $password, $database, $dbport);
  164. //checks connection if there is error ends ussd connection
  165. if ($db->connect_error){
  166. ('Content-type:text/plain');
  167. //log error to ussdcall and end call
  168. $filename = "F:/aligator/log.txt";
  169. file_put_contents($filename, "no db connection");
  170. die("End a database error was encountered. Contact admin!");
  171. }
  172. $disp = "SELECT humidity,rainfall,temperature FROM weather_tbl WHERE county_id='2'\n";
  173.  
  174. $result = $db->query($disp);
  175. if($result->num_rows > 0){
  176. //output data of each row
  177. while ($row = $result->fetch_assoc()){
  178. $rainfall = $row["rainfall"];
  179. $humidity = $row["humidity"];
  180. $temperature = $row["temperature"];
  181. }
  182.  
  183.  
  184.  
  185. $response = "END weather today is\n";
  186. $response .= "rainfall ".$rainfall ."\n";
  187. $response .= "humidity ".$humidity ."\n";
  188. $response .= "temperature ".$temperature ."\n";
  189. }
  190. else{
  191. $response = "0 results";
  192. }
  193. //$menu = "END Thankyou for using our services weather today is ".$menu."\n";
  194. printText($response);
  195. }
  196. catch(Exception $e) {
  197. echo 'Message: ' .$e->getMessage();
  198.  
  199.  
  200. $filename = "F:/aligator/log.txt";
  201. file_put_contents($filename, $e->getMessage());
  202.  
  203.  
  204. //printText($response)
  205. //$menu = "END Thankyou for using our services\n";
  206. //printText($menu)
  207.  
  208. }
  209. }
  210.  
  211.  
  212. if($text=="3"){
  213.  
  214. secMenu();
  215. }
  216.  
  217. else if($text == "3*1") {
  218. //$disp = "SELECT rainfall FROM weather_tbl WHERE county_id='1'\n";
  219. //$disp = "SELECT temperature FROM weather_tbl WHERE county_id='1'\n";
  220.  
  221. try{
  222. //create connection
  223. $db = new mysqli($servername, $username, $password, $database, $dbport);
  224. //checks connection if there is error ends ussd connection
  225. if ($db->connect_error){
  226. ('Content-type:text/plain');
  227. //log error to ussdcall and end call
  228. $filename = "F:/aligator/log.txt";
  229. file_put_contents($filename, "no db connection");
  230. die("End a database error was encountered. Contact admin!");
  231. }
  232. $disp = "SELECT humidity,rainfall,temperature FROM weather_tbl WHERE county_id='3'\n";
  233.  
  234. $result = $db->query($disp);
  235. if($result->num_rows > 0){
  236. //output data of each row
  237. while ($row = $result->fetch_assoc()){
  238. $rainfall = $row["rainfall"];
  239. $humidity = $row["humidity"];
  240. $temperature = $row["temperature"];
  241. }
  242.  
  243.  
  244.  
  245. $response = "END weather today is\n";
  246. $response .= "rainfall ".$rainfall ."\n";
  247. $response .= "humidity ".$humidity ."\n";
  248. $response .= "temperature ".$temperature ."\n";
  249. }
  250. else{
  251. $response = "0 results";
  252. }
  253. //$menu = "END Thankyou for using our services weather today is ".$menu."\n";
  254. printText($response);
  255. }
  256. catch(Exception $e) {
  257. echo 'Message: ' .$e->getMessage();
  258.  
  259.  
  260. $filename = "F:/aligator/log.txt";
  261. file_put_contents($filename, $e->getMessage());
  262.  
  263.  
  264. //printText($response)
  265. //$menu = "END Thankyou for using our services\n";
  266. //printText($menu)
  267.  
  268. }
  269. }
  270.  
  271.  
  272.  
  273. header('Content-type: text/plain');
  274. $response = $text;
  275. printText($response);
  276.  
  277.  
  278. // DONE!!!
  279. ?>
Add Comment
Please, Sign In to add comment