Good morning,
The script has three parts, all of them saved as php files except css.
simple_maths_calc.php (html)
simple_maths_operations.css
in_calc_functions.php
I am obviously aware of the problem of user input being outputted to the browser - tried to insert some simple script but it was filtered by one of the functions so hence i did not try to make use of html (obviously, i am not trying to say it is unnecessary )
btw, i am not excited when it comes to html design of the matter, so the look of it is just plain i would say.
The reason why it does not let enter a number longer then 10 digits is that it would stretch the ,result_box, way too much…
Thanks for having a look.
simple_maths_calc.php (html):
[php]
Maths operations
Simple maths Calculator
<div class="input_fields_box">
<label for="number1">Number 1</label>
<input type="text" name="number_1" id="number1">
<label for="number2">Number 2</label>
<input type="text" name="number_2" id="number2">
<div class="buttons_box">
<input type="submit" value="Addition" name="submit">
<br><br>
<input type="submit" value="Substraction" name="submit">
<br><br>
<input type="submit" value="Multiplication" name="submit">
<br><br>
<input type="submit" value="Division" name="submit">
<br><br>
<input type="submit" value="Larger" name="submit">
<br><br>
<input type="submit" value="Smaller" name="submit">
</div>
<div class="results_box">
<!--the following script takes inputs and offers/displays results-->
<?php require_once 'in_calc_functions.php'; ?>
</div>
[/php]
simple_maths_operations.css (CSS):
[php]
.body_container_frame {
height: 350px;
width: 400px;
position: fixed;
top: 25%;
left: 50%;
transform: translate(-50%, -50%);
background-color: LemonChiffon;
/display: table;/
}
/################################################################/
/###############laying out header and inut fields###############/
.table_header_inputs {
display: table;
width: 100%;
height: 15%;
}
.header_box {
display: table-row;
background-color: Wheat;
height: 20%;
width: 100%;
text-align: center;
font-style: oblique;
font-size: 150%;
/vertical-align: middle;//does not do anything…/
}
.input_fields_box {
display: table-row;
background-color: LemonChiffon;
height: 80%;
width: 100%;
}
//
/aligning input labels as well as inputs/
.input_fields_box label {
float: left;
clear: left;
width: 30%;
text-align: center;
margin-top: 2%;
font-style: oblique;
}
.input_fields_box input {
float: right;
margin-right:15%;
margin-top: 1%;
margin-bottom: 1%;
border-radius: 2px;
}
/################################################################/
/###############laying out buttons and result box###############/
.table_buttons_resultsbox {
display: table;
width: 100%;
}
.buttons_box {
display: table-cell;
background-color: LemonChiffon;
width: 25%;
padding-left: 2%;
}
.results_box {
display: table-cell;
background-color: MintCream;
width: 55%;
margin-right: auto;
padding-left: 5%;
vertical-align: middle;
/forming a shape/
border-radius: 20px;
border: 2px solid #666;
}
//
/aligning buttonss an formatting results box/
.buttons_box input {
width: 80%;
border-radius: 5px;
}
[/php]
in_calc_functions.php:
[php]
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
#setting the following variables as to get rid of ,,Notice: Undefined variable,, message
$nr_1 = "";
$nr_2 = "";
#path to the file to which the result is written in to.
$filename = '/var/www/html/web_dev/Study/phphelp/numbers/output.txt';
$errors = check_numbers($_POST['number_1'], $_POST['number_2']);
if (!empty($errors))
{
foreach ($errors as $key => $value) {
echo $value . "
";
}
}
elseif (empty($errors)) {
switch ($_POST['submit'])
{
case 'Addition':
$result[$_POST['submit']] = calcAddition($nr_1, $nr_2, $filename);
break;
case 'Substraction':
$result[$_POST['submit']] = calcSubstraction($nr_1, $nr_2, $filename);
break;
case 'Multiplication':
$result[$_POST['submit']] = calcMultiplication($nr_1, $nr_2, $filename);
break;
case 'Division';
$result[$_POST['submit']] = calcDivision($nr_1, $nr_2, $filename);
break;
case 'Larger';
$result[$_POST['submit']] = calcMax($nr_1, $nr_2, $filename);
break;
case 'Smaller':
$result[$_POST['submit']] = calcMin($nr_1, $nr_2, $filename);
break;
}
##the below is a basic formatting of output
echo "
" .
" Number 1 => " . $_POST['number_1'] .
"
" .
" Number 2 => " . $_POST['number_2'] .
"
" . strtolower(key($result)) . " : " .
" " . $result[(key($result))] . "
";
echo "
";
}
}
#defining functions
function calcAddition($nr_1, $nr_2, $filename)
{
$data = $_POST['number_1'] + $_POST['number_2'];
file_put_contents($filename, $data);
return $data;
}
function calcSubstraction($nr_1, $nr_2, $filename)
{
$data = $_POST['number_1'] - $_POST['number_2'];
file_put_contents($filename, $data);
return $data;
}
function calcMultiplication($nr_1, $nr_2, $filename)
{
$data = $_POST['number_1'] * $_POST['number_2'];
file_put_contents($filename, $data);
return $data;
}
function calcDivision($nr_1, $nr_2, $filename)
{
$data = $_POST['number_1'] / $_POST['number_2'];
file_put_contents($filename, $data);
return $data;
}
function calcMax($nr_1, $nr_2, $filename)
{
$data = max($_POST['number_1'], $_POST['number_2']);
file_put_contents($filename, $data);
return $data;
}
function calcMin($nr_1, $nr_2, $filename)
{
$data = min($_POST['number_1'], $_POST['number_2']);
file_put_contents($filename, $data);
return $data;
}
function check_numbers()
{
$errors_out = array();
if (strlen($_POST['number_1']) > 10 ) {
$errors_out['nr1_length']= "Number 1 too large.";
}
if (strlen($_POST['number_2']) > 10 ) {
$errors_out['nr2_length']= "Number 2 too large.";
}
if ($_POST['number_1'] < 0 || $_POST['number_2'] < 0 )
{
$errors_out['negative']= "Negative number detected.";
}
if(($_POST['submit'] == 'Division') && ($_POST['number_2'] == '0'))
{
$errors_out['zero_division']= "Division by zero detected !!.";
}
if (!is_numeric($_POST['number_1']))
{
$errors_out['number 1'] = "Number 1 is not a number!";
}
if (!is_numeric($_POST['number_2']))
{
$errors_out['number 2'] = "Number 2 is not a number!";
}
return $errors_out;
}
?>
[/php]