Lesson 18: USSD applications and databases

In our Lesson 15, we wrote some PHP code for the sample application. However, we did not talk anything about databases. This is because, this was beyond the scope of this tutorial. However, for those who may have any problem, I want to give you a clue. 

We will refer to the codes below


script ucuresults.php

<?php
//for africastalking
$phonenumber = $_GET['MSISDN'];
$sessionID = $_GET['sessionId'];
$servicecode = $_GET['serviceCode'];
$ussdString = $_GET['text'];

//create data fields
$regNo="";
$fName="";
$lName="";
$gender="";
$genderV="";
$pass="";
$acceptDeny="";

$username="";
$password="";
$year="";
$semester="";

//N/B: on going live we will change the GET[] method to POST[] (that is how africastalking do their stuff)
$level =0;

if($ussdString != ""){
$ussdString=  str_replace("#", "*", $ussdString);
$ussdString_explode = explode("*", $ussdString);
$level = count($ussdString_explode);
}
if ($level==0){
displaymenu();
}
function displaymenu(){
$ussd_text="CON Welcome to UCURESULTS. Please reply with; <br/>1. Register<br/>2. Login<br/>";
ussd_proceed($ussd_text);
}
function ussd_proceed ($ussd_text){
echo $ussd_text;
//exit(0);
}
if ($level>0){
switch ($ussdString_explode[0])
{
case 1:
register($ussdString_explode,$phonenumber);
break;
case 2:
login($ussdString_explode,$phonenumber);
break;
}
}


function register($details,$phone){

if (count($details)==1){
$ussd_text="CON <br/> Enter your registration number (Username)";
ussd_proceed($ussd_text);
}
else if (count($details)==2){
$ussd_text="CON <br/> Enter your first name";
ussd_proceed($ussd_text);
}
else if(count($details) == 3){
$ussd_text = "CON <br/> Enter your last name";
ussd_proceed($ussd_text);
}
else if(count($details) == 4){

$ussd_text = "CON <br/> Select gender  <br/>1. To select male <br/> 2. To select female <br/>";
ussd_proceed($ussd_text);
}else if(count($details) == 5){

$ussd_text = "CON <br/> set your password";
ussd_proceed($ussd_text);
}else if(count($details) == 6){
$ussd_text = "CON <br/>1. Accept registration <br/> 2. Cancel <br/>";
ussd_proceed($ussd_text);
}else if(count($details) == 7){
$regNo=$details[1];
$fName=$details[2];
$lName=$details[3];
$genderV=$details[4];
$pass=$details[5];
$acceptDeny=$details[6];

if($genderV=="1"){
$gender="Male";
}else if($genderV=="2"){
$gender="Female";
}
if($acceptDeny=="1"){
//=================Do your business logic here===========================
//Remember to put "END" at the start of each echo statement that comes here
echo "END <br> Details that will be pushed to the database. <br>Registration number: " . $regNo . "<br>" .
"Name: " . $fName. " " . $lName . "<br>" .
"Gender: " . $gender . "<br>" .
"Password (Encrypted): " . md5($pass) . "<br>";


}else{//Choice is cancel
$ussd_text = "END Your session is over";
ussd_proceed($ussd_text);
}


}
}

function login($details,$phone){
if (count($details)==1){
$ussd_text="CON <br/> Enter your Username (registration number)";
ussd_proceed($ussd_text);
}
else if (count($details)==2){
$ussd_text="CON <br/> Enter your password";
ussd_proceed($ussd_text);
}
else if(count($details) == 3){
$ussd_text = "CON <br/> Select your year of study <br/> 1. For year 1 <br/> 2. For year 2 <br/> 3. For year 3";
ussd_proceed($ussd_text);
}
else if(count($details) == 4){

$ussd_text = "CON <br/> Select semister  <br/>1. For semister 1 <br/> 2. For semester 2 <br/>";
ussd_proceed($ussd_text);

}else if(count($details) == 5){
$username=$details[1];
$password=$details[2];
$year=$details[3];
$semester=$details[4];
echo "END We are fetching your exam results using this information <br/>
Username: " . $username . "<br/>" .
"Password (Encrypted): " . md5($password) . "<br/>" .
"Year: " . $year. "<br/>" .
"Semester: " . $semester;
}
}
?> 

With the register option, you collect user’s data and when you are the 
//=================Do your business logic here===========================
You will need to connect to your database and do any business logic you like. You would store the data you collect from your users normally. 

Likewise, on the Login option, once you are at 

echo "END We are fetching your exam results using this information <br/>
Username: " . $username . "<br/>" .
"Password (Encrypted): " . md5($password) . "<br/>" .
"Year: " . $year. "<br/>" .
"Semester: " . $semester;

you will need to connect to your database, and authenticate your user using username and password, if it succeeds, you will then fetch the exam details of that user. 

You can always write your code in your own version but connect to database in the sections we indicated. 

N/B Do not be confused by the ‘END’ and ‘CON’, preapended to the output. These depends on the gateway you use. For example, from our example, the www.africastalking.com gateway uses CON to know that the session is on and that a user can still reply and END tells the gateway to end the session. 

Feel  free to leave a comment. 

17 Comments
    • Derdus Kenga
    • Derdus Kenga
    • Derdus Kenga

Add a Comment

Your email address will not be published. Required fields are marked *