From 8c989afa297d86c21fc9ad449b92584896b7df02 Mon Sep 17 00:00:00 2001 From: Jack-Benny Persson Date: Tue, 17 Jun 2014 19:03:10 +0200 Subject: [PATCH] Added some code for browser usage --- README.md | 14 +++++++--- interest.php | 78 +++++++++++++++++++++++++++++++++++----------------- 2 files changed, 63 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 50facb9..e07c8bd 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,9 @@ It's nothing fancy at all, but maybe it will be useful to someone. I've made it while learning PHP. ## Usage ## +The script can be run both from a browser and from the terminal. + +### CLI usage ### First argument to the script is the amount of money on your savings account, the second argument is the interest (without the percent sign) and the third (optional) argument is the number of years. For example if you have 50.000 in @@ -12,13 +15,16 @@ money you will have after 8 years: php interest.php 50000 3 8 -Or if you've made the script executable you can run it as follow: - - ./interest 50000 3 8 - If you leave out the number of years the script will calulate the savings after one year. +### Browser usage ### +Just put the script on a webserver with PHP and run access it from your browser. +Then fill in the form and hit 'Calculate' and the script will calculate your +savings for you. + +Please note that the script does NOT do any form of data validation! + ## Copying ## Copyright 2014 Jack-Benny Persson (jack-benny@cyberinfo.se) diff --git a/interest.php b/interest.php index 9e3b0e9..f600575 100755 --- a/interest.php +++ b/interest.php @@ -1,4 +1,3 @@ -#!/usr/bin/php \n"; } -// Sanity check -if (!isset($argv[1]) || !isset($argv[2])) +// If we are running from the terminal +if (!array_key_exists("HTTP_USER_AGENT", $_SERVER)) { - print "Please enter amount and interest!\n"; - Usage(); - exit(1); + // Sanity check + if (!isset($argv[1]) || !isset($argv[2])) + { + print "Please enter amount and interest!\n"; + CLI_Usage(); + exit(1); + } + + // Declare some variables + $money = $argv[1]; + $inter = $argv[2]; + + // If the user entered years + if (isset($argv[3])) + { + $years = $argv[3]; + $mySavings = InteOnInte($money, $inter, $years); + printf ("After %d years with %.2f%% interest on %.2f you will have " . + "%.2f\n", $years, $inter, $money, $mySavings); + } + + // If the user did not enter years + else + { + $mySavings = InteOnInte($money, $inter); + printf ("After 1 year with %.2f%% interest on %.2f you will have " . + "%.2f\n", $inter, $money, $mySavings); + } } -// Declare some variables -$money = $argv[1]; -$inter = $argv[2]; - -// If the user entered years -if (isset($argv[3])) -{ - $years = $argv[3]; - $mySavings = InteOnInte($money, $inter, $years); - printf ("After %d years with %.2f%% interest on %.2f you will have %.2f\n", - $years, $inter, $money, $mySavings); -} - -// If the user did not enter years +// If we are running from a browser else { - $mySavings = InteOnInte($money, $inter); - printf ("After 1 year with %.2f%% interest on %.2f you will have %.2f\n", - $inter, $money, $mySavings); + print ("\n" . + "\nIntereset On Interest\n" . + "

Interest On Interest calculator

\n" . + "
\n" . + "Money:
\n" . + "Interest:
\n" . + "Years:
\n" . + "\n" . + "
\n
\n"); + + if (array_key_exists("money", $_GET) && array_key_exists("interest", $_GET) + && array_key_exists("years", $_GET)) + { + print ("After $_GET[years] years with $_GET[interest]% interest on " . + "$_GET[money] you will have "); + $savings = InteOnInte($_GET['money'], $_GET['interest'], $_GET['years']); + printf ("%.2f\n", $savings); + } + + print "\n\n"; + } - ?>