Jump to content

Simplify the PHP code


john7911

Recommended Posts

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 ?

 

 

Link to comment
Share on other sites

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 .= "&beta; = " . number_format($angle,1) . "°" . "<br/>";
        $results .= "Rayon = " . $rayon . " mm" . "<br/>";
        $results .= "&phi; = " . $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 by Barand
  • Like 3
Link to comment
Share on other sites

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. 

 

Link to comment
Share on other sites

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">

 

Link to comment
Share on other sites

@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 by Barand
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.