<?php

// Get the user's input from the USSD gateway (e.g., *123*1*1#)
$userInput = $_GET['input'];

// Define the menu options and their corresponding USSD codes
$mainMenu = [
    '1' => 'Buyer',
    '2' => 'Farmer',
    '3' => 'About FAMASO',
];

$buyerMenu = [
    '1' => 'View Orders',
    '2' => 'Place an Order',
    '3' => 'Contact Support',
    
    '00' => 'Home',
];

$farmerMenu = [
    '1' => 'View Farm Stats',
    '2' => 'Update Farm Info',
    '3' => 'Contact Support',
    '00' => 'Home',
];

$aboutMenu = [
    '1' => 'About Us',
    '00' => 'Home'
];

// Function to display a menu and its options
function displayMenu($menu)
{
    $response = "Welcome to Famaso.\n";
    foreach ($menu as $key => $option) {
        $response .= "$key. $option\n";
    }
    echo $response;
}

// Function to handle buyer-specific logic
function handleBuyerMenu($option)
{
    switch ($option) {
        case '1':
            echo "You selected View Orders. Here are your orders:\n";
            // Implement logic to display orders
            break;
        case '2':
            echo "You selected Place an Order. Please follow the instructions to place an order.\n";
            // Implement logic to place an order
            break;
        case '3':
            echo "You selected Contact Support. Please contact our support team at [email protected].\n";
            // Implement logic to contact support
            break;
        case '00':
            echo "Returning to the Home/Main Menu.\n";
            break;
        default:
            echo "Invalid option. Please try again.\n";
            break;
    }
}

// Function to handle farmer-specific logic
function handleFarmerMenu($option)
{
    switch ($option) {
        case '1':
            echo "You selected View Farm Stats. Here are your farm statistics:\n";
            // Logic to display farm stats
            function displayFarmStats()
{
    
           $farmName = "ABC Dairy Farm";
           $totalCows = 50;
           $todayMilkProduction = "500 liters";
           $thisMonthMilkProduction = "15000 liters";

           echo "Farm Name: $farmName\n";
           echo "Total Cows: $totalCows\n";
           echo "Milk Production (today): $todayMilkProduction\n";
           echo "Milk Production (this month): $thisMonthMilkProduction\n";
    // Add more farm statistics as needed
}
            break;
        case '2':
            echo "You selected Update Farm Info. Please update your farm information:\n";
            // Logic to update farm info
            function displayFarmInfo()
{
    
            $farmName = "ABC Dairy Farm";
            $ownerName = "John Doe";
            $totalCows = 50;
            $location = "Farmington, USA";
            $milkProduction = "500 liters/day";
            $farmSize = "100 acres";

            echo "Farm Name: $farmName\n";
            echo "Owner: $ownerName\n";
            echo "Total Cows: $totalCows\n";
            echo "Location: $location\n";
            echo "Daily Milk Production: $milkProduction\n";
            echo "Farm Size: $farmSize\n";
    
    
}
            break;

        case '3':
            echo "You selected Contact Support. Please contact our support team at .\n";
            // Logic for contact support
            $supportMobileNumber = '0726993619';
           echo "You selected Contact Support. Please contact our support team at $supportMobileNumber.\n";
   
     break;
            
     case '00':
            echo "Returning to the Home/Main Menu.\n";
            break;
        default:
            echo "Invalid option. Please try again.\n";
            break;
    }
}

function handleAboutMenu($option)

{    switch ($option) {
    case '1':
        echo "In this online platform buyers will be able to interact with farmers in easy way. They will be able to get their required products from a variety of farmers they prefer\n";

        break;
        case '00':
            echo "Returning to the Home/Main Menu.\n";
            break;
        default:
            echo "Invalid option. Please try again.\n";
            break;
    }



}


// Parse the user's input
$inputParts = explode('*', $userInput);

// Determine the menu level
$menuLevel = count($inputParts) - 1;

// Check the menu level and execute the appropriate logic
if ($menuLevel == 1) {
    // Main Menu
    if (isset($mainMenu[$inputParts[1]])) {
        displayMenu($menuLevel == 1 ? $mainMenu : []);
    } else {
        echo "Invalid option. Please try again.\n";
    }
} elseif ($menuLevel == 2) {
    // Buyer or Farmer Menu
    $selectedOption = $inputParts[2];
    if ($inputParts[1] == '1' && isset($buyerMenu[$selectedOption])) {
        handleBuyerMenu($selectedOption);
    } elseif ($inputParts[1] == '2' && isset($farmerMenu[$selectedOption])) {
        handleFarmerMenu($selectedOption);
    } else {
        echo "Invalid option. Please try again.\n";
    }
} else {
    echo "Error while running the USSD code.\n";
}

?> 

PHP Online Compiler

Write, Run & Share PHP code online using OneCompiler's PHP online compiler for free. It's one of the robust, feature-rich online compilers for PHP language, running on the latest version 7. Getting started with the OneCompiler's PHP compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as PHP and start coding.

Taking inputs (stdin)

OneCompiler's PHP online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample PHP program which takes name as input and prints hello message with your name.

<?php
	fscanf(STDIN, "%s\n", $name);           
    echo "Hello ".$name.".\n";
?>

About PHP

PHP(Hypertext Preprocessor) is widely used server sripting language by Rasmus Lerdorf in the year 1994.

Key features

  • Free
  • powerful tool for making dynamic and interactive web pages
  • can integrate with almost all popular databases like MySQL, PostgreSQL, Oracle, Sybase, Informix, Microsoft SQL Server etc.
  • C like Syntax and easy to learn.
  • Object oriented scripting language.
  • easily embeddable into HTML
  • Loosely typed language.

Syntax help

Variables

In PHP, there is no need to explicitly declare variables to reserve memory space. When you assign a value to a variable, declaration happens automatically. Variables are case-sensitive in PHP.

$variable_name = value;  

Loops

1. IF Family:

If, If-else, Nested-Ifs are used when you want to perform a certain set of operations based on conditional expressions.

If

if(conditional-expression){    
//code    
} 

If-else

if(conditional-expression){  
//code if condition is true  
} else {  
//code if condition is false  
} 

Nested-If-else

if(condition-expression1) {  
    //code if above condition is true  
} elseif(condition-expression2){  
    //code if above condition is true  
}  
elseif(condition-expression3) {  
    //code if above condition is true  
}  
...  
else {  
    //code if all the conditions are false  
}  

2. Switch:

Switch is used to execute one set of statement from multiple conditions.

switch(conditional-expression) {    
case value1:    
 // code if the above value is matched    
 break;  // optional  
case value2:    
 // code if the above value is matched    
 break;  // optional  
...    
    
default:     
 // code to be executed when all the above cases are not matched;    
} 
 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  // code  
} 

For-each:

// you can use any of the below syntax
foreach ($array as $element-value) {  
    //code  
}

foreach ($array as $key => $element-value) {   
    //code 
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while(condition) {  
 // code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {
  // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

How to define a Function

function function_name(parameters) {  
  //code
}

How to call a Function

function_name (parameters)