Location: PHPKode > scripts > GF PHP Calculator > gf-php-calculator/calculator.php
<?php

//////////////////////////////////////////////////////////////////////////
//			GF Creations PHP Calculator V3			//
//	-------------------------------------------------------------	//
//									//
//	Author: 	Paul Goddard					//
//	Website: 	http://www.gfcreations.com			//
//	Support: 	http://www.gfcreations.com/contact.php		//
//									//
//	-------------------------------------------------------------	//
//									//
//	This is the new and improved version of the PHP Calculator	//
//	bought to you by GF Creations.Com				//
//	The code has been completely re-written to improve usability	//
//	and design. It gives the webmaster complete control over	//
//	their very own interactive tool.				//
//									//
//	If you have any suggestions for the script, find any bugs,	//
//	have a complaint, or just a general comment - please use the	//
//	support link found above.					//
//									//
//									//
//////////////////////////////////////////////////////////////////////////

?>

<!-- Page Header Start -->

<html>

<head>
	<title>PHP Calculator (Powered by GF Creations)</title>
</head>

<body>

<!-- Page Header End -->

<?php

/* 	PLEASE DO NOT EDIT THE FOLLOWING UNLESS YOU HAVE KNOWLEDGE OF PHP CODING	*/
/*	DOING SO COULD CAUSE THE SCRIPT TO FAIL						*/


	/* Grab Calculator Functions */
	require_once("calc_functions.php");
	require_once("calc_settings.php");

	/* Define variables */
	$result = "";
	$message = "";
	$msg_type = "";


	/* Generate Calculator Styles */
?>
	<style>

		input.calc_input{
			text-size: <?php echo($Calculator['Input_Text_Size']);?>px;
			color: <?php echo($Calculator['Input_Text_Colour']);?>;
			background-color: <?php echo($Calculator['Input_BG_Colour']);?>;
		}

		select.calc_dropdown{
			text-size: <?php echo($Calculator['Dropdown_Text_Size']);?>px;
			color: <?php echo($Calculator['Dropdown_Text_Colour']);?>;
			background-color: <?php echo($Calculator['Dropdown_BG_Colour']);?>;
		}

		span#calc_msg{
			text-size: <?php echo($Calculator['Message_Text_Size']);?>px;
			color: <?php echo($Calculator['Message_Text_Colour']);?>;
			background-color: <?php echo($Calculator['Message_BG_Colour']);?>;

			border: <?php echo($Calculator['Message_Border_Size'].'px '.$Calculator['Message_Border_Style'].' '.$Calculator['Message_Border_Colour']);?> ;

			padding: <?php echo($Calculator['Message_Padding'].'px'); ?>;
		}
	</style>	
<?php
	/* Submit pressed for basic calculator */
	if($_POST['submit_basic']){

		if($Calculator['pi_value']){
			$num1 = htmlspecialchars(trim(stripslashes($_POST['first_number'])));
			$num2 = htmlspecialchars(trim(stripslashes($_POST['second_number'])));

			$num1 = str_replace("pi", $Calculator['pi_value'], $num1);
			$num2 = str_replace("pi", $Calculator['pi_value'], $num2);
		}
		else{
			$num1 = htmlspecialchars(trim(stripslashes($_POST['first_number'])));
			$num2 = htmlspecialchars(trim(stripslashes($_POST['second_number'])));
		}

		$action = $_POST['action'];


		if($num1 == ""){
			$result = false;

			if($Calculator['Error_Number1_Empty'] == ""){
				$message = "You must fill in both fields to complete the calculation.";
			}
			else{
				$message = $Calculator['Error_Number1_Empty'];
			}
			
			$msg_type = "error";
		}
		elseif(!is_numeric($num1)){
			$result = false;

			if($Calculator['Error_Must_Be_Number'] == ""){
				$message = "You must enter numeric values to complete the calculation.";
			}
			else{
				$message = $Calculator['Error_Must_Be_Number'];
			}
			
			$msg_type = "error";
		}
		elseif($num2 == ""){
			$result = false;

			if($Calculator['Error_Number2_Empty'] == ""){
				$message = "You must fill in both fields to complete the calculation.";
			}
			else{
				$message = $Calculator['Error_Number2_Empty'];
			}

			$msg_type = "error";
		}
		elseif(!is_numeric($num1)){
			$result = false;

			if($Calculator['Error_Must_Be_Number'] == ""){
				$message = "You must enter numeric values to complete the calculation.";
			}
			else{
				$message = $Calculator['Error_Must_Be_Number'];
			}
			
			$msg_type = "error";
		}
		elseif($action == ""){
			$result = false;

			if($Calculator['Error_Action_Blank'] == ""){
				$message = "There is no action specified.";
			}
			else{
				$message = $Calculator['Error_Action_Blank'];
			}

			$msg_type = "error";
		}
		else{
			if($action == "add"){
				$result = $num1 + $num2;

				if($Calculator['Result_Add'] == ""){
					$message = "After adding {NUM1} and {NUM2}, the result is: {RESULT}";
				}
				else{
					$message = $Calculator['Result_Add'];
				}
				
				$msg_type = "result";
			}
			elseif($action == "subtract"){
				$result = $num1 - $num2;

				if($Calculator['Result_Subtract'] == ""){
					$message = "After subtracting {NUM2} from {NUM1}, the result is: {RESULT}";
				}
				else{
					$message = $Calculator['Result_Subtract'];
				}
				
				$msg_type = "result";
			}
			elseif($action == "multiply"){
				$result = $num1 * $num2;

				if($Calculator['Result_Multiply'] == ""){
					$message = "After multiplying {NUM1} and {NUM2}, the result is: {RESULT}";
				}
				else{
					$message = $Calculator['Result_Multiply'];
				}
				
				$msg_type = "result";
			}
			elseif($action == "divide"){
				$result = $num1 / $num2;

				if($Calculator['Result_Divide'] == ""){
					$message = "After dividing {NUM1} into {NUM2}, the result is: {RESULT}";
				}
				else{
					$message = $Calculator['Result_Divide'];
				}
				
				$msg_type = "result";
			}
			else{
				$result = false;

				if($Calculator['Error_General'] == ""){
					$message = "An unknown error has occured whilst processing the calculation.";
				}
				else{
					$message = $Calculator['Error_General'];
				}

				$msg_type = "error";
			}
		}
	}

	/* Submit pressed for extra functions */
	if($_POST['submit_extra']){

		if($Calculator['pi_value']){
			$num3 = htmlspecialchars(trim(stripslashes($_POST['third_number'])));

			$num1 = str_replace("pi", $Calculator['pi_value'], $num3);
		}
		else{
			$num3 = htmlspecialchars(trim(stripslashes($_POST['third_number'])));
		}

		$action_x = $_POST['action_x'];


		if($num3 == ""){
			$result = false;

			if($Calculator['Error_Number3_Empty'] == ""){
				$message = "You must fill in the field to complete the calculation.";
			}
			else{
				$message = $Calculator['Error_Number3_Empty'];
			}

			$msg_type = "error";
		}
		elseif(!is_numeric($num3)){
			$result = false;

			if($Calculator['Error_Must_Be_Number'] == ""){
				$message = "You must enter numeric values to complete the calculation.";
			}
			else{
				$message = $Calculator['Error_Must_Be_Number'];
			}
			
			$msg_type = "error";
		}
		elseif($action_x == ""){
			$result = false;

			if($Calculator['Error_Action_Blank'] == ""){
				$message = "There is no action specified.";
			}
			else{
				$message = $Calculator['Error_Action_Blank'];
			}
			
			$msg_type = "error";
		}
		else{
			if($action_x == "square"){
				$result = square($num3);

				if($Calculator['Result_Square'] == ""){
					$message = "After squaring {NUM3}, the result is: {RESULT}";
				}
				else{
					$message = $Calculator['Result_Square'];
				}

				$msg_type = "result";
			}
			elseif($action_x == "cube"){
				$result = cube($num3);

				if($Calculator['Result_Cube'] == ""){
					$message = "After cubing {NUM3}, the result is: {RESULT}";
				}
				else{
					$message = $Calculator['Result_Cube'];
				}

				$msg_type = "result";
			}
			elseif($action_x == "squareroot"){
				$result = sqrt($num3);

				if($Calculator['Result_Cube'] == ""){
					$message = "After square rooting {NUM3}, the result is: {RESULT}";
				}
				else{
					$message = $Calculator['Result_Cube'];
				}

				$msg_type = "result";
			}
			else{
				$result = false;

				if($Calculator['Error_General'] == ""){
					$message = "An unknown error has occured whilst processing the calculation.";
				}
				else{
					$message = $Calculator['Error_General'];
				}

				$msg_type = "error";
			}
		}
	}

	
	/* Open The Form */
	echo(open_form('post','calculator.php','calc'));

	echo('<br /><br />');

	/* Text Box - First Number */
	echo(create_text_input('first_number','10',false,'class="calc_input"'));

	echo(' ');

	/* Dropdown Box - Action */

	if( ($Calculator['Display_Add'] != "") && ($Calculator['Display_Subtract'] != "") && ($Calculator['Display_Multiply'] != "") && ($Calculator['Display_Divide'] != "") ){
		$the_basic_values = $Calculator['Display_Add']."|".$Calculator['Display_Subtract']."|".$Calculator['Display_Multiply']."|".$Calculator['Display_Divide'];
	}
	else{
		$the_basic_values = "+|-|*|/";
	}

	echo(create_dropdown('action','add|subtract|multiply|divide',$the_basic_values,'class="calc_dropdown"'));

	echo(' ');

	/* Text Box - Second Number */
	echo(create_text_input('second_number','10',false,'class="calc_input"'));

	echo(' ');

	/* Submit - Basic Calculator */
	echo('<input type="submit" name="submit_basic" value="Equals" />');


	echo('<br /><br /><br /><br />');


	/* Text Box - Third Number */
	echo(create_text_input('third_number','10',false,'class="calc_input"'));

	echo(' ');

	/* Dropdown Box - Extra Actions */

	if( ($Calculator['Display_Square'] != "") && ($Calculator['Display_Cube'] != "") && ($Calculator['Display_SqRt'] != "") ){
		$the_xtra_values = $Calculator['Display_Square']."|".$Calculator['Display_Cube']."|".$Calculator['Display_SqRt'];
	}
	else{
		$the_xtra_values = "^2|^3|SqRt";
	}

	echo(create_dropdown('action_x','square|cube|squareroot',$the_xtra_values,'class="calc_dropdown"'));

	echo(' ');

	/* Submit - Extra Calculator */
	echo('<input type="submit" name="submit_extra" value="Equals" />');


	echo('</form>');

	/* Generate Error / Result Message */
	if($message != ""){
		echo(generate_message($msg_type,'id="calc_msg"',$message,$num1,$num2,$num3,$result));
	}


/* 	YOU MAY EDIT BELOW THIS LINE	*/

?>

<!-- Page Footer Start -->

</body>

</html>

<!-- Page Footer End -->
Return current item: GF PHP Calculator