Skip to content

Instantly share code, notes, and snippets.

@espirado
Created August 6, 2018 13:01
Show Gist options
  • Save espirado/7c34d11d182200d9d880be55e5af160f to your computer and use it in GitHub Desktop.
Save espirado/7c34d11d182200d9d880be55e5af160f to your computer and use it in GitHub Desktop.
php for ml for weather and farming information
<?php
//db connection
$username = 'root';
$servername = 'localhost';
$password = '';
$dbport = '3306';
$database = 'alligator_db';
// Reads the variables sent via POST from our gateway
$sessionId = $_POST["sessionId"];
$serviceCode = $_POST["serviceCode"];
$phoneNumber = $_POST["phoneNumber"];
$text = $_POST["text"];
//set default level to zero
//Definition of all functions to be used
function printText($response){
echo $response;
}
//First menu to be displayed on querying SSD CODE
function initialMenu(){
$menu = "CON Choose your county\n";
$menu .= "1. Turkana\n";
$menu .= "2. Wajir \n";
$menu .= "3. Isiolo \n";
printText($menu);
}
//when user selects any county
function secMenu(){
$menu = "CON Choose what you want to view\n";
$menu .= "1. Weather Updates\n";
$menu .= "2. Farming Information\n";
$menu .= "3. Ground Water\n";
printText($menu);
}
if($text == ""){
//function call to initial menu
initialMenu();
}
else if($text =="1") {
secMenu();
}
//shows a choice of the first menu code below shows second menu
else if( $text=="1*1") {
//$disp = "SELECT rainfall FROM weather_tbl WHERE county_id='1'\n";
//$disp = "SELECT temperature FROM weather_tbl WHERE county_id='1'\n";
try{
//create connection
$db = new mysqli($servername, $username, $password, $database, $dbport);
//checks connection if there is error ends ussd connection
if ($db->connect_error){
('Content-type:text/plain');
//log error to ussdcall and end call
$filename = "F:/aligator/log.txt";
file_put_contents($filename, "no db connection");
die("End a database error was encountered. Contact admin!");
}
$disp = "SELECT humidity,rainfall,temperature FROM weather_tbl WHERE county_id='1'\n";
$result = $db->query($disp);
if($result->num_rows > 0){
//output data of each row
while ($row = $result->fetch_assoc()){
$rainfall = $row["rainfall"];
$humidity = $row["humidity"];
$temperature = $row["temperature"];
}
$response = "END weather today is\n";
$response .= "rainfall ".$rainfall ."\n";
$response .= "humidity ".$humidity ."\n";
$response .= "temperature ".$temperature ."\n";
}
else{
$response = "0 results";
}
//$menu = "END Thankyou for using our services weather today is ".$menu."\n";
printText($response);
}
catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
$filename = "F:/aligator/log.txt";
file_put_contents($filename, $e->getMessage());
//printText($response)
//$menu = "END Thankyou for using our services\n";
//printText($menu)
}
}
else if ( $text == "1*2" ) {
try{
//create connection
$db = new mysqli($servername, $username, $password, $database, $dbport);
//checks connection if there is error ends ussd connection
if ($db->connect_error){
('Content-type:text/plain');
//log error to ussdcall and end call
echo 'connection failed';
$filename = "F:/aligator/log.txt";
file_put_contents($filename, "no db connection");
die("End a database error was encountered. Contact admin!");
}
$disp = "SELECT message FROM farming_info_tbl WHERE message_id='1'\n";
$result = $db->query($disp);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$message = $row["message"];
$response = "END Our analysis today\n";
$response .= "message ".$message ."\n";
}
} else {
echo "0 results";
}
$db->close();
}
catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
$filename = "F:/aligator/log.txt";
file_put_contents($filename, $e->getMessage());
}
}
if($text =="2"){
secMenu();
}
else if($text == "2*1") {
//$disp = "SELECT rainfall FROM weather_tbl WHERE county_id='1'\n";
//$disp = "SELECT temperature FROM weather_tbl WHERE county_id='1'\n";
try{
//create connection
$db = new mysqli($servername, $username, $password, $database, $dbport);
//checks connection if there is error ends ussd connection
if ($db->connect_error){
('Content-type:text/plain');
//log error to ussdcall and end call
$filename = "F:/aligator/log.txt";
file_put_contents($filename, "no db connection");
die("End a database error was encountered. Contact admin!");
}
$disp = "SELECT humidity,rainfall,temperature FROM weather_tbl WHERE county_id='2'\n";
$result = $db->query($disp);
if($result->num_rows > 0){
//output data of each row
while ($row = $result->fetch_assoc()){
$rainfall = $row["rainfall"];
$humidity = $row["humidity"];
$temperature = $row["temperature"];
}
$response = "END weather today is\n";
$response .= "rainfall ".$rainfall ."\n";
$response .= "humidity ".$humidity ."\n";
$response .= "temperature ".$temperature ."\n";
}
else{
$response = "0 results";
}
//$menu = "END Thankyou for using our services weather today is ".$menu."\n";
printText($response);
}
catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
$filename = "F:/aligator/log.txt";
file_put_contents($filename, $e->getMessage());
//printText($response)
//$menu = "END Thankyou for using our services\n";
//printText($menu)
}
}
if($text=="3"){
secMenu();
}
else if($text == "3*1") {
//$disp = "SELECT rainfall FROM weather_tbl WHERE county_id='1'\n";
//$disp = "SELECT temperature FROM weather_tbl WHERE county_id='1'\n";
try{
//create connection
$db = new mysqli($servername, $username, $password, $database, $dbport);
//checks connection if there is error ends ussd connection
if ($db->connect_error){
('Content-type:text/plain');
//log error to ussdcall and end call
$filename = "F:/aligator/log.txt";
file_put_contents($filename, "no db connection");
die("End a database error was encountered. Contact admin!");
}
$disp = "SELECT humidity,rainfall,temperature FROM weather_tbl WHERE county_id='3'\n";
$result = $db->query($disp);
if($result->num_rows > 0){
//output data of each row
while ($row = $result->fetch_assoc()){
$rainfall = $row["rainfall"];
$humidity = $row["humidity"];
$temperature = $row["temperature"];
}
$response = "END weather today is\n";
$response .= "rainfall ".$rainfall ."\n";
$response .= "humidity ".$humidity ."\n";
$response .= "temperature ".$temperature ."\n";
}
else{
$response = "0 results";
}
//$menu = "END Thankyou for using our services weather today is ".$menu."\n";
printText($response);
}
catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
$filename = "F:/aligator/log.txt";
file_put_contents($filename, $e->getMessage());
//printText($response)
//$menu = "END Thankyou for using our services\n";
//printText($menu)
}
}
header('Content-type: text/plain');
$response = $text;
printText($response);
// DONE!!!
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment