Jump to content

Server-Side Scripting/Key-Value Databases/PHP

From Wikiversity

index.php

[edit | edit source]
<?php

// This program creates and displays a temperature database
// with options to insert, update, and delete records.
//
// Assumes Redis is installed locally. If you have Redis running
// separately (such as in a Docker container), replace localhost below
// with your Redis server's IP address.
//
// References:
//  https://en.wikibooks.org/wiki/PHP_Programming
//  https://www.php.net/manual/en/class.mongodb-driver-manager.php
//  https://redislabs.com/lp/php-redis/
//  https://github.com/phpredis/phpredis

$HOST = "localhost";
$DATABASE = 0;

$FORM = '
<h1>Temperature Data Entry</h1>
<p>Enter country and temperature. Enter again to update.</p>
<p>Enter country without temperature to delete.</p>
<form method="POST">
<p><label for="country">Country:</label>
<input type="text" id="country" name="country" required></p>
<p><label for="stop">Temperature:</label>
<input type="text" id="temperature" name="temperature"></p>
<p><input type="submit" name="submit" value="Submit"></p>
</form>
<hr>
';

main();

function main() {
    switch($_SERVER["REQUEST_METHOD"]) {
        case "GET":
            try {
                echo $GLOBALS["FORM"];
                echo get_data();
            } catch (Exception $exception) {
                echo $exception->getMessage();
            }
            break;
        case "POST":
            try {
                $country = $_POST["country"];
                $temperature = $_POST["temperature"];
                if (!country_exists($country)) {
                    insert_country($country, $temperature);
                } else if ($temperature != "") {
                    update_country($country, $temperature);
                }
                else {
                    delete_country($country);
                }

                echo $GLOBALS["FORM"];
                echo get_data();
            } catch (Exception $exception) {
                echo $exception->getMessage();
            }
            break;
        default:
            echo "Unexpected request method:" . $_SERVER["REQUEST_METHOD"];
            break;
    } 
}

function get_data() {
    $redis = new Redis();
    $redis->connect($GLOBALS["HOST"]);
    $countries = $redis->keys('*');
    sort($countries);

    $result = "<table><tr><th>Country</th>";
    $result = $result . "<th>Temperature</th></tr>";
    foreach ($countries as $country) {
        $temperature = $redis->get($country);
        $result = $result . "<tr><td>" . $country . "</td>";
        $result = $result . "<td>" . $temperature . "</td></tr>";
    }
    $result = $result . "</table>";
    return $result;
}

function country_exists($country) {
    $redis = new Redis();
    $redis->connect($GLOBALS["HOST"]);
    $result = $redis->exists($country);
    return !!$result;
}

function insert_country($country, $temperature) {
    $redis = new Redis();
    $redis->connect($GLOBALS["HOST"]);
    $redis->set($country, $temperature);
}

function update_country($country, $temperature) {
    $redis = new Redis();
    $redis->connect($GLOBALS["HOST"]);
    $redis->set($country, $temperature);
}

function delete_country($country) {
    $redis = new Redis();
    $redis->connect($GLOBALS["HOST"]);
    $redis->del($country);
}

?>

Try It

[edit | edit source]
  1. Use Docker/Redis to run a Redis server.
  2. Use Docker/PHP and copy and paste the code above to run as the PHP application. Be sure to modify the Redis host address to match the address received by your Redis server.