john7911 Posted June 30, 2019 Share Posted June 30, 2019 Hi ? I wrote a PHP code but I think i can simplify more specially in the switch part , as you can see in the code. can you please give me some advise. <form method="post" action="<?=$_SERVER['PHP_SELF'];?>"> <fieldset> X: <input type="text" name="x" value="" /> <br/> Y: <input type="text" name="y" value="" /> <br/> <select name="diametre"> <option value=" "> </option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="6">6</option> <option value="8">8</option> <option value="10">10</option> <option value="12">12</option> <option value="14">14</option> <option value="16">16</option> <option value="18">18</option> <option value="20">20</option> <option value="22">22</option> <option value="24">24</option> <option value="26">26</option> </select> <input type="submit" value = "Calculer" /> </fieldset> </form> <?php if (isset($_POST["x"]) && isset($_POST["y"]) && isset($_POST["diametre"]) ){ $x = $_POST["x"]; $y = $_POST["y"]; $diametre = $_POST["diametre"]; } switch ($diametre) { case "2": $rayon = $diametre * 38.1; $dc = $x/2; $ad = ($y/2)-$rayon; $ac = sqrt(pow($ad,2) + pow($dc,2)); $ec = sqrt(pow($ac,2) - pow($rayon,2)); $LongueurBayonette = $ec*2; $alpha = asin($dc/$ac); $alpha = $alpha*180/M_PI; $beta = acos($rayon/$ac); $beta = $beta*180/M_PI; $angle = 180-$alpha-$beta; echo "X = " . $x . "mm" . "<br/>"; echo "Y = " . $y . "mm" . "<br/>"; echo "Longueur = " . number_format($LongueurBayonette,1) . " mm" . "<br/>"; echo "ꞵ = " . number_format($angle,1) . "°" . "<br/>"; echo "Rayon = " . $rayon . " mm" . "<br/>"; echo "⌀ = " . $diametre . '"' . "<br/>"; break; case "3": $rayon = $diametre * 38.1; $dc = $x/2; $ad = ($y/2)-$rayon; $ac = sqrt(pow($ad,2) + pow($dc,2)); $ec = sqrt(pow($ac,2) - pow($rayon,2)); $LongueurBayonette = $ec*2; $alpha = asin($dc/$ac); $alpha = $alpha*180/M_PI; $beta = acos($rayon/$ac); $beta = $beta*180/M_PI; $angle = 180-$alpha-$beta; echo "X = " . $x . "mm" . "<br/>"; echo "Y = " . $y . "mm" . "<br/>"; echo "Longueur = " . number_format($LongueurBayonette,1) . "mm" . "<br/>"; echo "ꞵ = " . number_format($angle,1) . "°" . "<br/>"; echo "Rayon = " . $rayon . "mm" . "<br/>"; echo "⌀ = " . $diametre . '"' . "<br/>"; break; case "4": $rayon = $diametre * 38.1; $dc = $x/2; $ad = ($y/2)-$rayon; $ac = sqrt(pow($ad,2) + pow($dc,2)); $ec = sqrt(pow($ac,2) - pow($rayon,2)); $LongueurBayonette = $ec*2; $alpha = asin($dc/$ac); $alpha = $alpha*180/M_PI; $beta = acos($rayon/$ac); $beta = $beta*180/M_PI; $angle = 180-$alpha-$beta; echo "X = " . $x . "mm" . "<br/>"; echo "Y = " . $y . "mm" . "<br/>"; echo "Longueur = " . number_format($LongueurBayonette,1) . "mm" . "<br/>"; echo "ꞵ = " . number_format($angle,1) . "°" . "<br/>"; echo "Rayon = " . $rayon . "mm" . "<br/>"; echo "⌀ = " . $diametre . '"' . "<br/>"; break; case "6": $rayon = $diametre * 38.1; $dc = $x/2; $ad = ($y/2)-$rayon; $ac = sqrt(pow($ad,2) + pow($dc,2)); $ec = sqrt(pow($ac,2) - pow($rayon,2)); $LongueurBayonette = $ec*2; $alpha = asin($dc/$ac); $alpha = $alpha*180/M_PI; $beta = acos($rayon/$ac); $beta = $beta*180/M_PI; $angle = 180-$alpha-$beta; echo "X = " . $x . "mm" . "<br/>"; echo "Y = " . $y . "mm" . "<br/>"; echo "Longueur = " . number_format($LongueurBayonette,1) . "mm" . "<br/>"; echo "ꞵ = " . number_format($angle,1) . "°" . "<br/>"; echo "Rayon = " . $rayon . "mm" . "<br/>"; echo "⌀ = " . $diametre . '"' . "<br/>"; break; case "8": $rayon = $diametre * 38.1; $dc = $x/2; $ad = ($y/2)-$rayon; $ac = sqrt(pow($ad,2) + pow($dc,2)); $ec = sqrt(pow($ac,2) - pow($rayon,2)); $LongueurBayonette = $ec*2; $alpha = asin($dc/$ac); $alpha = $alpha*180/M_PI; $beta = acos($rayon/$ac); $beta = $beta*180/M_PI; $angle = 180-$alpha-$beta; echo "X = " . $x . "mm" . "<br/>"; echo "Y = " . $y . "mm" . "<br/>"; echo "Longueur = " . number_format($LongueurBayonette,1) . "mm" . "<br/>"; echo "ꞵ = " . number_format($angle,1) . "°" . "<br/>"; echo "Rayon = " . $rayon . "mm" . "<br/>"; echo "⌀ = " . $diametre . '"' . "<br/>"; break; case "10": $rayon = $diametre * 38.1; $dc = $x/2; $ad = ($y/2)-$rayon; $ac = sqrt(pow($ad,2) + pow($dc,2)); $ec = sqrt(pow($ac,2) - pow($rayon,2)); $LongueurBayonette = $ec*2; $alpha = asin($dc/$ac); $alpha = $alpha*180/M_PI; $beta = acos($rayon/$ac); $beta = $beta*180/M_PI; $angle = 180-$alpha-$beta; echo "X = " . $x . "mm" . "<br/>"; echo "Y = " . $y . "mm" . "<br/>"; echo "Longueur = " . number_format($LongueurBayonette,1) . "mm" . "<br/>"; echo "ꞵ = " . number_format($angle,1) . "°" . "<br/>"; echo "Rayon = " . $rayon . "mm" . "<br/>"; echo "⌀ = " . $diametre . '"' . "<br/>"; break; case "12": $rayon = $diametre * 38.1; $dc = $x/2; $ad = ($y/2)-$rayon; $ac = sqrt(pow($ad,2) + pow($dc,2)); $ec = sqrt(pow($ac,2) - pow($rayon,2)); $LongueurBayonette = $ec*2; $alpha = asin($dc/$ac); $alpha = $alpha*180/M_PI; $beta = acos($rayon/$ac); $beta = $beta*180/M_PI; $angle = 180-$alpha-$beta; echo "X = " . $x . "mm" . "<br/>"; echo "Y = " . $y . "mm" . "<br/>"; echo "Longueur = " . number_format($LongueurBayonette,1) . "mm" . "<br/>"; echo "ꞵ = " . number_format($angle,1) . "°" . "<br/>"; echo "Rayon = " . $rayon . "mm" . "<br/>"; echo "⌀ = " . $diametre . '"' . "<br/>"; break; case "14": $rayon = $diametre * 38.1; $dc = $x/2; $ad = ($y/2)-$rayon; $ac = sqrt(pow($ad,2) + pow($dc,2)); $ec = sqrt(pow($ac,2) - pow($rayon,2)); $LongueurBayonette = $ec*2; $alpha = asin($dc/$ac); $alpha = $alpha*180/M_PI; $beta = acos($rayon/$ac); $beta = $beta*180/M_PI; $angle = 180-$alpha-$beta; echo "X = " . $x . "mm" . "<br/>"; echo "Y = " . $y . "mm" . "<br/>"; echo "Longueur = " . number_format($LongueurBayonette,1) . "mm" . "<br/>"; echo "ꞵ = " . number_format($angle,1) . "°" . "<br/>"; echo "Rayon = " . $rayon . "mm" . "<br/>"; echo "⌀ = " . $diametre . '"' . "<br/>"; break; case "16": $rayon = $diametre * 38.1; $dc = $x/2; $ad = ($y/2)-$rayon; $ac = sqrt(pow($ad,2) + pow($dc,2)); $ec = sqrt(pow($ac,2) - pow($rayon,2)); $LongueurBayonette = $ec*2; $alpha = asin($dc/$ac); $alpha = $alpha*180/M_PI; $beta = acos($rayon/$ac); $beta = $beta*180/M_PI; $angle = 180-$alpha-$beta; echo "X = " . $x . "mm" . "<br/>"; echo "Y = " . $y . "mm" . "<br/>"; echo "Longueur = " . number_format($LongueurBayonette,1) . "mm" . "<br/>"; echo "ꞵ = " . number_format($angle,1) . "°" . "<br/>"; echo "Rayon = " . $rayon . "mm" . "<br/>"; echo "⌀ = " . $diametre . '"' . "<br/>"; break; case "18": $rayon = $diametre * 38.1; $dc = $x/2; $ad = ($y/2)-$rayon; $ac = sqrt(pow($ad,2) + pow($dc,2)); $ec = sqrt(pow($ac,2) - pow($rayon,2)); $LongueurBayonette = $ec*2; $alpha = asin($dc/$ac); $alpha = $alpha*180/M_PI; $beta = acos($rayon/$ac); $beta = $beta*180/M_PI; $angle = 180-$alpha-$beta; echo "X = " . $x . "mm" . "<br/>"; echo "Y = " . $y . "mm" . "<br/>"; echo "Longueur = " . number_format($LongueurBayonette,1) . "mm" . "<br/>"; echo "ꞵ = " . number_format($angle,1) . "°" . "<br/>"; echo "Rayon = " . $rayon . "mm" . "<br/>"; echo "⌀ = " . $diametre . '"' . "<br/>"; break; case "20": $rayon = $diametre * 38.1; $dc = $x/2; $ad = ($y/2)-$rayon; $ac = sqrt(pow($ad,2) + pow($dc,2)); $ec = sqrt(pow($ac,2) - pow($rayon,2)); $LongueurBayonette = $ec*2; $alpha = asin($dc/$ac); $alpha = $alpha*180/M_PI; $beta = acos($rayon/$ac); $beta = $beta*180/M_PI; $angle = 180-$alpha-$beta; echo "X = " . $x . "mm" . "<br/>"; echo "Y = " . $y . "mm" . "<br/>"; echo "Longueur = " . number_format($LongueurBayonette,1) . "mm" . "<br/>"; echo "ꞵ = " . number_format($angle,1) . "°" . "<br/>"; echo "Rayon = " . $rayon . "mm" . "<br/>"; echo "⌀ = " . $diametre . '"' . "<br/>"; break; case "22": $rayon = $diametre * 38.1; $dc = $x/2; $ad = ($y/2)-$rayon; $ac = sqrt(pow($ad,2) + pow($dc,2)); $ec = sqrt(pow($ac,2) - pow($rayon,2)); $LongueurBayonette = $ec*2; $alpha = asin($dc/$ac); $alpha = $alpha*180/M_PI; $beta = acos($rayon/$ac); $beta = $beta*180/M_PI; $angle = 180-$alpha-$beta; echo "X = " . $x . "mm" . "<br/>"; echo "Y = " . $y . "mm" . "<br/>"; echo "Longueur = " . number_format($LongueurBayonette,1) . "mm" . "<br/>"; echo "ꞵ = " . number_format($angle,1) . "°" . "<br/>"; echo "Rayon = " . $rayon . "mm" . "<br/>"; echo "⌀ = " . $diametre . '"' . "<br/>"; break; case "24": $rayon = $diametre * 38.1; $dc = $x/2; $ad = ($y/2)-$rayon; $ac = sqrt(pow($ad,2) + pow($dc,2)); $ec = sqrt(pow($ac,2) - pow($rayon,2)); $LongueurBayonette = $ec*2; $alpha = asin($dc/$ac); $alpha = $alpha*180/M_PI; $beta = acos($rayon/$ac); $beta = $beta*180/M_PI; $angle = 180-$alpha-$beta; echo "X = " . $x . "mm" . "<br/>"; echo "Y = " . $y . "mm" . "<br/>"; echo "Longueur = " . number_format($LongueurBayonette,1) . "mm" . "<br/>"; echo "ꞵ = " . number_format($angle,1) . "°" . "<br/>"; echo "Rayon = " . $rayon . "mm" . "<br/>"; echo "⌀ = " . $diametre . '"' . "<br/>"; break; case "26": $rayon = $diametre * 38.1; $dc = $x/2; $ad = ($y/2)-$rayon; $ac = sqrt(pow($ad,2) + pow($dc,2)); $ec = sqrt(pow($ac,2) - pow($rayon,2)); $LongueurBayonette = $ec*2; $alpha = asin($dc/$ac); $alpha = $alpha*180/M_PI; $beta = acos($rayon/$ac); $beta = $beta*180/M_PI; $angle = 180-$alpha-$beta; echo "X = " . $x . "mm" . "<br/>"; echo "Y = " . $y . "mm" . "<br/>"; echo "Longueur = " . number_format($LongueurBayonette,1) . "mm" . "<br/>"; echo "ꞵ = " . number_format($angle,1) . "°" . "<br/>"; echo "Rayon = " . $rayon . "mm" . "<br/>"; echo "⌀ = " . $diametre . '"' . "<br/>"; break; default: echo " "; } ?> The result is this page: http://cadtuts.eu/bayonette-test.php Thank you ? Quote Link to comment Share on other sites More sharing options...
Barand Posted June 30, 2019 Share Posted June 30, 2019 (edited) The code in each switch is identical so all it achieves is to ensure the calculation uses only the defined list of diameter options. Just use an array of the valid values to verify the values. You can use the same array to generate the option list <?php $diam_vals = [2,3,4,6,8,10,12,14,16,18,20,22,24,26]; $results = ''; if ($_SERVER['REQUEST_METHOD']=='POST') { $x = $_POST['x'] ?? 0; $y = $_POST['y'] ?? 0; $diametre = $_POST['diametre'] ?? 0; if ($x > 0 && $y > 0 && in_array($diametre, $diam_vals)) { $rayon = $diametre * 38.1; $dc = $x/2; $ad = ($y/2)-$rayon; $ac = sqrt(pow($ad,2) + pow($dc,2)); $ec = sqrt(pow($ac,2) - pow($rayon,2)); $LongueurBayonette = $ec*2; $alpha = asin($dc/$ac); $alpha = $alpha*180/M_PI; $beta = acos($rayon/$ac); $beta = $beta*180/M_PI; $angle = 180-$alpha-$beta; $results .= "X = " . $x . "mm" . "<br/>"; $results .= "Y = " . $y . "mm" . "<br/>"; $results .= "Longueur = " . number_format($LongueurBayonette,1) . " mm" . "<br/>"; $results .= "β = " . number_format($angle,1) . "°" . "<br/>"; $results .= "Rayon = " . $rayon . " mm" . "<br/>"; $results .= "φ = " . $diametre . '"' . "<br/>"; } else { $results = 'Inputs are not valid'; } } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Simplified Example</title> </head> <body> <form method="post" action=""> <fieldset> X: <input type="text" name="x" value="" /> <br/> Y: <input type="text" name="y" value="" /> <br/> Diametre: <select name="diametre"> <option value="0"> </option> <?php foreach ($diam_vals as $d) { echo "<option value='$d'>$d</option>\n" ; } ?> </select> <input type="submit" value = "Calculer" /> </fieldset> </form> <br> <?=$results?> Just curious - do you have a diagram of how those values relate to one another. It metions "rayon" and "bayonnette" so my guess is that it is some kind of laser rifle with attached bayonet (but I could be wrong) ? Edited June 30, 2019 by Barand 3 Quote Link to comment Share on other sites More sharing options...
john7911 Posted June 30, 2019 Author Share Posted June 30, 2019 Thank you very much Barand for your help ? I will print the code and study it because i'm not a programmer. Quote Just curious - do you have a diagram of how those values relate to one another. It metions "rayon" and "bayonnette" so my guess is that it is some kind of laser rifle with attached bayonet (but I could be wrong) ? hahaha no it's not a laser rifle , it's to help pipefitter and piping designer to calculate the length of pipe they need and the angle. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted July 1, 2019 Share Posted July 1, 2019 Side note: you'll want to avoid outputting the raw value of $_SERVER['PHP_SELF']. Having code like the following opens your page up to XSS (cross-site scripting) attacks: <form method="post" action="<?=$_SERVER['PHP_SELF'];?>"> In this particular case, you can leave the action attribute blank, as Barand's post showed. <form method="post" action=""> If you think that's weird, you could also hard-code the page name. <form method="post" action="bayonette-test.php"> Quote Link to comment Share on other sites More sharing options...
benanamen Posted July 1, 2019 Share Posted July 1, 2019 In HTML5, the "action" is not necessary at all when submitting to the same page. You can simple do like so... <form method="post"> Quote Link to comment Share on other sites More sharing options...
Barand Posted July 1, 2019 Share Posted July 1, 2019 (edited) @john7911 Just for completeness... Where you have several case values which call the same the code you can do something like this instead of repeating the code block for each value <?php $diametre = $_GET['diametre'] ?? 0; switch ($diametre) { case 2: case 3: case 4: case 6: case 8: case 10: case 12: case 14: case 16: case 18: case 20: case 22: case 24: case 26: echo "do something"; break; default: echo "do something else"; } ?> Edited July 1, 2019 by Barand Quote Link to comment Share on other sites More sharing options...
john7911 Posted July 2, 2019 Author Share Posted July 2, 2019 cyberRobot, benanamen, BarandThank you very much for your help and advises. i will try in some test file. ? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.