Really need help Please! PHP and INC-PHP

Hello all! Thank you for taking the time to read this. I am in need of some direction as to how to fix my PHP and INC_PHP files. This is what is needed: a. Accept the 2 numbers and the selection.
b. If the selection is Addition, call a calcAddition function that will add these 2 numbers and return the result.
c. If the selection is Subtraction, call a calcSubtraction function that will subtract these 2 numbers and return the result.
d. If the selection is Multiplication, call a calcMultiplication function that will multiply these 2 numbers and return the result.
e. If the selection is Division, call a calcDivision function that will divide these 2 numbers and return the result.
f. If the selection is Find Maximum, call a calcMax function that will return the larger number.
g. If the selection is Find Minimum, call a calcMin function that will return the smaller number.
h. When the answer is returned, display the answer on the screen AND write the answer to an output file called output.txt .
i. Validated the input – only perform the above calculations if the first number and the second number are positive numbers (no negative numbers allowed). If there is a negative number, display an error message and a RETURN TO FORM link so user can re-enter numbers.
j. Also, if doing division, make sure the second variable is not 0, because as we all know, In the words of my 8th grade math teacher, “thou shalt not divide by zero”.

  1. Write a inc-calc-functions.php file that will contain the 6 functions described above and must be named as follows:

calcAddition – accept the 2 numbers as parameters, add them, and return the answer.
calcSubtraction – accept the 2 numbers as parameters, subtract them, and return the answer.
calcMulitiplication – accept the 2 numbers as parameters, multiply them, and return the answer.
calcDivision – accept the 2 numbers as parameters, divide them, and return the answer.
calcMax – accept the 2 numbers as parameters and return the larger number.
calcMin – accept the 2 numbers as parameters and return the smaller number.

In the HTML, the user will input 2 numbers and select which math operation to use, such as addition and so forth. I have completed the HTML portion, but the PHP is just giving me a headache. Can anyone please help? Thank you and God Bless.

What (code) do you have currently and where are you having issue?

The inc-calc-functions.php will have to capture $_POST from your html code and feed the relevant data to relevant function.

Quite interesting assignment, i may well spend some time tomorrow to do it out of my curiosity.

Hi folks,

Wondering that if i post my solution to this, could some of (more experienced) you please advise on the quality of my code.
Sort of like, the code is junk, not that bad, good.What to be more careful about etc. - general advice.
(I am not a programmer, only do my reading about & studied it as one of my modules.)

Obviously, not posting the code right now as not sure if it would violate forum rules or good practice…

Thanks.

Post your code in php tags it is exactly what we want to see.

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]

Looks fine to me. I see some areas that could improve, but that is just the style and how you focused, but overall it works fine.

Thanks astonecipher.
Happy that the code meets some standard.
I just have to push myself practicing harder now.

Sponsor our Newsletter | Privacy Policy | Terms of Service