Page 1 of 1

PHP Calculator

Posted: Mon May 23, 2005 10:07 am
by DarkArchon
I'm trying to make a calculator for a specific purpose, and so far I can't find any examples I can modify to work the way I want it to. Hopefully someone here can figure out what's wrong with it.

Here's the HTML form that goes with it.

Code: Select all

<html>
<head>
<title>Calculator</title>
</head>
<body>
<form name=&quote;calc&quote; method=POST action=&quote;calc.php&quote;>
Number of Ships: 
  <input name=&quote;ships&quote; type=&quote;text&quote; maxlength=&quote;3&quote;><p>
Number of Weapons Fired:  
  <input name=&quote;fired&quote; type=&quote;text&quote; maxlength=&quote;3&quote;><p> 
Type of Weapon: 
  <select name=&quote;weapon&quote;>
  <option value=&quote;25&quote;>Laser</option>
  <option value=&quote;15&quote;>Nuclear Missile</option>
 *There's a bunch of values in here that I edited out for length*
  <option value=&quote;100&quote;>Homing Black Hole</option>
  <option value=&quote;100&quote;>Distortion Blaster</option>
  </select>
  <input type=&quote;submit&quote; name=&quote;Submit&quote; value=&quote;Submit&quote;>
</form>
</body>
</html>
Here's the PHP code that I got so far.

Code: Select all

<html>
<head>
<title>
<?php
echo "</title>";
echo "</head>";
echo '<body>';

 // global variables
 $valid_form = "false";
 $ships = "";    // ships
 $fired = "";    // fired
 $weapon = "";   // weapon
 
 &fired * weapon / &ships = &type;
 echo &type;

?>


</body>
</html>
I'm kinda new to php, so that probably looks pretty messed up, but all I need is to get it working. Thanks for any help ya'll give me.

Posted: Mon May 23, 2005 11:43 am
by Sphen001
Hi,

Well, first of all, I wouldn't use globals.

Redoing your code:

Code: Select all

<?php
$type = ($_POST['fired'] * $_POST['weapon']) / $_POST['ships'];

echo $type;
?>
Just wondering, why are you using references? Doesn't make any sense to me.

Anyways, hope this helps.

Sphen001

Posted: Mon May 23, 2005 11:51 am
by Bennettman
Okay, first off echo and print (practically the same commands) are for outputting content, so it's not necessary to use them for static strings.

Code: Select all

<html>
<head>
<title></title> 
</head>
</body>
Secondly, in the code, always use $varname for variables. In the PHP code it seriously looks like you're resetting all the input, which makes no sense whatsoever.

Code: Select all

<?php

 // first load the POST input into regular variables
 $ships = $_POST['ships'];
 $fired = $_POST['fired'];
 $weapon = $_POST['weapon'];

 // check that $ships and $fired are under 1000, and that the weapon is 15/25/100
 if (
  ($ships > 999)
  or
  ($fired > 999)
  or
  ( ($weapon != 15) and ($weapon != 25) and ($weapon != 100) )
 )
 {
  print "Invalid input.";
 }

 // Equate $fired multiplied by $weapon divided by $ships into a var
 else
 {
  $result = ( ($fired * $weapon) / $ships );
  print $result;
 }
 
?>
</body>
</html>

Posted: Mon May 23, 2005 11:59 am
by DarkArchon
Sphen001: Like I said, I'm new to php, this doesn't make much since to me, so I don't expect it to make since to you :)

Bennettman: Thanks, I used your solution and it seems to work perfect.

BTW, is there any good books ya'll would suggest for learning more PHP? I love the launguage, but don't really have time for classes or anything.

Posted: Mon May 23, 2005 12:48 pm
by shiznatix