PHP Advice Needed

Employment Law Clinic

Free Member
Aug 10, 2009
3,557
1,667
London
employmentlawclinic.com
I'm trying to put a simple calculation function on a page. I've used PHP calculator code I found here, and modified this to do the calculation I need: First Number² x Second Number.

What I'm struggling with is deleting of an unnecessary bit of text - "Operation" with a drop-down list (limited to one option) - that I don't want to include on the page - there won't be an option to choose anything other than the necessary calculation.

If I take out the code that puts this on the page
Code:
Operation:
<select name="oper">

<option value="multiply">Multiplication</option>
</select>
the calculation won't run.


The full code is included on http://employmentlawclinic.com/attendance-and-performance/test-page-2/

Code:
<?php

class calc {
var $number1;
var $number2;
function multiply($number1,$number2)
{
$result =$number1 * $number1 * $number2;


if ($result > 649)
echo "Your worker's Bradford Factor is $result, and requires urgent attention. Call us now on 020 3397 297 for assistance &amp; advice on managing your sick leave.<br><br>";

elseif ($result > 399)
echo "Your worker's Bradford Factor is $result, and you providing you have given earlier warnings, you should be considering a final written warning at this stage. Call us now on 020 3397 297 for assistance &amp; advice on managing your sick leave.<br><br>";

elseif ($result > 124)
echo "Your worker's Bradford Factor is $result, and you should be considering issuing formal written warnings. Call us now on 020 3397 297 for assistance advice on managing your sick leave. <br><br>";



elseif ($result > 49)
echo "Your worker's Bradford Factor is $result, and you should be considering issuing formal written warnings. Call us now on 020 3397 297 for assistance &amp; advice on managing your sick leave.<br><br>";



elseif ($result > 0)
echo "Your worker's Bradford Factor is $result; everyone gets sick on occasion, but there doesn't appear to be much that should concern you with this absence. Call us now on 020 3397 297 for assistance &amp; advice on managing your sick leave<br><br>";






}

}
//Creating object of class
$calc = new calc();
?>




<form name="calc" action="" method="POST">
<p>How many times has the worker being off sick?</p> <input type="text" name="value1"><br>
<p>And how many sick days have they had in total?</p> <input type="text" name="value2"><br>

Operation:
<select name="oper">

<option value="multiply">Multiplication</option>
</select>
<br>



<input type="submit" value="Calculate Your Worker's Bradford Factor" name="submit">
</form>
<br>
<br>

<?php
if($_POST['submit']){
$number1 = $_POST['value1'];
$number2 = $_POST['value2'];
$oper = $_POST['oper'];


if(!eregi("[0-9]", $number1)){
echo "<font color='red'>The total occasions of sick leave must be a number</font>";

}
if(!eregi("[0-9]", $number2)){
echo "<font color='red'>The total sick days must be a number</font>";

}

if($oper == "multiply"){
$calc->multiply($number1,$number2);
}
}
?>
<?php
if($_POST['submit1']){
$number = $_POST['value'];
$operat = $_POST['operat'];
if(!$number){
echo "<font color='red'>Please enter number in calculator 2</font>";

}

}
?>

I don't really understand PHP, so is anyone handy on it that can tell me how to tidy up this code?

Thanks,


Karl Limpert
 
Here we go:

PHP:
<?php

class calc {
var $number1;
var $number2;
function multiply($number1,$number2)
{
$result =$number1 * $number1 * $number2;


if ($result > 649)
echo "Your worker's Bradford Factor is $result, and requires urgent attention. Call us now on 020 3397 297 begin_of_the_skype_highlighting              020 3397 297      end_of_the_skype_highlighting for assistance &amp; advice on managing your sick leave.<br><br>";

elseif ($result > 399)
echo "Your worker's Bradford Factor is $result, and you providing you have given earlier warnings, you should be considering a final written warning at this stage. Call us now on 020 3397 297 begin_of_the_skype_highlighting              020 3397 297      end_of_the_skype_highlighting for assistance &amp; advice on managing your sick leave.<br><br>";

elseif ($result > 124)
echo "Your worker's Bradford Factor is $result, and you should be considering issuing formal written warnings. Call us now on 020 3397 297 begin_of_the_skype_highlighting              020 3397 297      end_of_the_skype_highlighting for assistance advice on managing your sick leave. <br><br>";



elseif ($result > 49)
echo "Your worker's Bradford Factor is $result, and you should be considering issuing formal written warnings. Call us now on 020 3397 297 begin_of_the_skype_highlighting              020 3397 297      end_of_the_skype_highlighting for assistance &amp; advice on managing your sick leave.<br><br>";



elseif ($result > 0)
echo "Your worker's Bradford Factor is $result; everyone gets sick on occasion, but there doesn't appear to be much that should concern you with this absence. Call us now on 020 3397 297 begin_of_the_skype_highlighting              020 3397 297      end_of_the_skype_highlighting for assistance &amp; advice on managing your sick leave<br><br>";






}

}
//Creating object of class
$calc = new calc();
?>




<form name="calc" action="" method="POST">
<p>How many times has the worker being off sick?</p> <input type="text" name="value1"><br>
<p>And how many sick days have they had in total?</p> <input type="text" name="value2"><br>


<br>



<input type="submit" value="Calculate Your Worker's Bradford Factor" name="submit">
</form>
<br>
<br>

<?php
if($_POST['submit']){
$number1 = $_POST['value1'];
$number2 = $_POST['value2'];



if(!eregi("[0-9]", $number1)){
echo "<font color='red'>The total occasions of sick leave must be a number</font>";

}
if(!eregi("[0-9]", $number2)){
echo "<font color='red'>The total sick days must be a number</font>";

}


$calc->multiply($number1,$number2);

}
?>
<?php
if($_POST['submit1']){
$number = $_POST['value'];
$operat = $_POST['operat'];
if(!$number){
echo "<font color='red'>Please enter number in calculator 2</font>";

}

}
?>

The code would not work because it was looking at

PHP:
if($oper == "multiply"){
$calc->multiply($number1,$number2);
}

You should be able to see above, that I just invoke it regardless :)
 
Upvote 0
ok this is a really over the top solution to what you want and can be done it just a couple of lines but effectively you want it to multipy all the time yeah?

I've only quickly looked at this so excuse me if I miss something but if you remove the $oper variable the line below won't call the $calc multiply function.



if($oper == "multiply"){ $calc->multiply($number1,$number2); }



So basically all you need to do is remove the if statement so it reads just:

$calc->multiply($number1,$number2);

This will call the function everytime so you may need to change some other code but try it and see what happens.
 
Last edited:
Upvote 0
all you need really is:

Code:
if($_POST['submit']){ 

$number1 = $_POST['value1']; $number2 = $_POST['value2'];

$result =$number1 * $number1 * $number2;   

if ($result > 649) echo "Your worker's Bradford Factor is $result, and requires urgent attention. Call us now on 020 3397 297 for assistance &amp; advice on managing your sick leave.<br><br>";  elseif ($result > 399) echo "Your worker's Bradford Factor is $result, and you providing you have given earlier warnings, you should be considering a final written warning at this stage. Call us now on 020 3397 297 for assistance &amp; advice on managing your sick leave.<br><br>";  elseif ($result > 124) echo "Your worker's Bradford Factor is $result, and you should be considering issuing formal written warnings. Call us now on 020 3397 297 for assistance advice on managing your sick leave. <br><br>";    elseif ($result > 49) echo "Your worker's Bradford Factor is $result, and you should be considering issuing formal written warnings. Call us now on 020 3397 297 for assistance &amp; advice on managing your sick leave.<br><br>";    elseif ($result > 0) echo "Your worker's Bradford Factor is $result; everyone gets sick on occasion, but there doesn't appear to be much that should concern you with this absence. Call us now on 020 3397 297 for assistance &amp; advice on managing your sick leave<br><br>";   }
 
Last edited:
Upvote 0
ok this is a really over the top solution to what you want and can be done it just a couple of lines but effectively you want it to multipy all the time yeah?
Yeah, I figured it might be OTT, but I’ve never learnt PHP and couldn’t find out how to write the necessary code, so anything that done the two multiplications was ok for me (I wanted to at least try to get something to work)...
all you need really is:

Code:
[/QUOTE]
...but I’ll try this instead, thanks.




Karl Limpert
 
Upvote 0
Yeah, I figured it might be OTT, but I’ve never learnt PHP and couldn’t find out how to write the necessary code, so anything that done the two multiplications was ok for me (I wanted to at least try to get something to work)...

...but I’ll try this instead, thanks.




Karl Limpert


obviously include the HTML form in that last one.
 
Upvote 0

Latest Articles