monkeypaw201 Posted July 10, 2008 Share Posted July 10, 2008 i have this code that works when i post velocity and time, distance and time, but not velocity and distance the time is posted in minutes, velocity in miles per hour (outputted in knots) and the distance is in miles... if you could find out whats wrong, much appreciated <?php if(isset($_GET['time']) | isset($_GET['velocity']) | isset($_GET['distance'])) { // Define Variables if($_GET['time'] != "") $tt = $_GET['time']; else $tt = "?"; if($_GET['velocity'] != "") $v = $_GET['velocity']; else $v = "?"; if($_GET['distance'] != "") $d = $_GET['distance']; else $d = "?"; $deg = $_GET['degrees']; //Convert Variables if($_GET['time'] != "") $t = $tt/60; else $t = $tt; // Calculate if($d == "?") $d = $v*$t; elseif($v == "?") $v = $d/$t; elseif($t == "?") $t = $d/$v; //Convert results $dn = round($d); $vn = round($v*0.868976242); if(isset($_GET['time'])) $tn = round($tt); else $tn = round($t*60); ?> Hello, <br><br> Given the information and selections you provided, the calculations have been made and the generated information is below. <br><br> Traveling at <b><?php echo $vn; ?> kts</b><br> Turning <b><?php echo $deg; ?></b>° degrees<br><br> Will take you <b><?php echo $tn; ?></b> minutes and <b><?php echo $dn; ?></b> miles <?php }else{ ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> </head> <body><FORM action="aircraft_turn.php" method="GET" enctype="multipart/form-data"> To calulate a number, enter the two you are not looking for (ie searching: time, provide: speed, distance) <br> <br> Speed: <INPUT maxlength="3" name="velocity" type="text"> mph<br> Time: <INPUT name="time" type="text"> minutes<br> Distance: <INPUT name="distance" type="text"> miles<br><br><INPUT type="submit" name="submit" value="Calculate!"> </FORM> </body> </html> <?php } ?> Link to comment https://forums.phpfreaks.com/topic/114101-solved-velocitydistancetime-works-only-for-certain-combinations/ Share on other sites More sharing options...
mbeals Posted July 10, 2008 Share Posted July 10, 2008 your code could use some optimizing but I believe your issue is inconsistent units. Your velocity is being entered in as miles per hour, but you are using time in seconds. Here are my suggestions: 1. Convert time into seconds as soon as you read it in. 2. Convert velocity into miles/second 3. Cast all three values as floats What is happening... When you divide distance by velocity, your velocity is too high by a factor of 3600 (60^2) giving you a relatively small number for time (in seconds). When you convert to minutes, this number is almost always going to be less then a minute, so it is rounded to zero. So the simple solution is to just divide your incoming velocity by 3600, but I would simplify your code too. Something like this: <?php if(isset($_GET['time']) | isset($_GET['velocity']) | isset($_GET['distance'])) { $time = ((float)$_GET['time'])*60; //time in seconds $velocity = ((float)$_GET['velocity'])/3600; //velocity in mi/s $distance = (float)$_GET['distance']; //distance in mi if( $time != '' && $velocity != '') { $distance = $time * $velocity; } if( $time != '' && $distance != '') { $velocity = $distance/$time; } if( $distance!= '' && $velocity != '') { $time = $distance/$velocity; } $deg = $_GET['degrees']; //Convert results $distance_mi = round($distance); $velocity_knotts = round($velocity*3600*0.868976242); //convert to mph then to knotts $time_minutes = round($time/60); //convert to minutes ?> Hello, <br><br> Given the information and selections you provided, the calculations have been made and the generated information is below. <br><br> Traveling at <b><?=$velocity_knotts; ?> kts</b><br> Turning <b><?=$deg; ?></b>° degrees<br><br> Will take you <b><?=$time_minutes; ?></b> minutes and <b><?=$distance_miles; ?></b> miles <?php }else{ ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> </head> <body><FORM action="test.php" method="GET" enctype="multipart/form-data"> To calulate a number, enter the two you are not looking for (ie searching: time, provide: speed, distance) <br> <br> Speed: <INPUT maxlength="3" name="velocity" type="text"> mph<br> Time: <INPUT name="time" type="text"> minutes<br> Distance: <INPUT name="distance" type="text"> miles<br><br><INPUT type="submit" name="submit" value="Calculate!"> </FORM> </body> </html> <?php } ?> casting might not be necessary, but I don't trust do math problems on variables with implied type. I've had results turn out orders of magnitude wrong because one variable 3 equations deep was automatically cast as an int instead of a float (rounding off the decimal). Those are not fun to debug. Link to comment https://forums.phpfreaks.com/topic/114101-solved-velocitydistancetime-works-only-for-certain-combinations/#findComment-586485 Share on other sites More sharing options...
monkeypaw201 Posted July 10, 2008 Author Share Posted July 10, 2008 Thanks a bunch! Very thorough answer! I thank you Link to comment https://forums.phpfreaks.com/topic/114101-solved-velocitydistancetime-works-only-for-certain-combinations/#findComment-586494 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.