Frage

Okay, ich habe diese Art von Pythagoras-Solver codiert und habe mich gefragt, wie ich es verbessern oder effizienter machen könnte?

<?php
    $sides = array('1' => 'Hypotenuse',
        '2' => 'Adjacent',
        '3' => 'Opposite');

    function createSideDropdown($name) {
        global $sides;
        $option = "<select name='".$name."'>";
            if(!empty($sides)) {
                foreach($sides as $id => $sideDesc) {
                    $option .= "<option value='".$id."'>".$sideDesc."</option>";
                }
            } else {
                die("Error fetching sides!");
            }
        $option .= "</select>";
        echo $option;
    }

    try {
        if(!empty($_POST['submit'])) {
            if(empty($_POST['val1']) || empty($_POST['val2'])) {
                throw new Exception("Please enter an integer in both boxes.");
            }
            if(!is_numeric($_POST['val1']) || !is_numeric($_POST['val2'])) {
                throw new Exception("One of the numbers you entered is not a valid integer.");
            }

            $val1 = $_POST['val1'];
            $val2 = $_POST['val2'];
            $val1numtype = $_POST['val1type'];
            $val2numtype = $_POST['val2type'];
            $val1nametype = $sides[$val1numtype];
            $val2nametype = $sides[$val2numtype];

                if($val1numtype == $val2numtype) {
                    throw new Exception("The two sides of the triangle must be different");
                }

                if($val1nametype == "Hypotenuse" || $val2nametype == "hypotenuse") {
                    // work out a small side
                    $bignum = max($val1, $val2);
                    $smallnum = min($val1, $val2);
                    $sqTotal = ($bignum * $bignum) - ($smallnum * $smallnum);
                    $total = sqrt($sqTotal);
                    echo $bignum."&sup2; - ".$smallnum."&sup2; = ".$sqTotal."<br />
                    &radic;".$sqTotal." = ".$total.$_POST['mes'];
                } else {
                    // work out the hypotenuse
                    $sq1 = $val1 * $val1;
                    $sq2 = $val2 * $val2;
                    $sqTotal = $sq1 + $sq2;
                    $total = sqrt($sqTotal);
                    echo $val1."&sup2; + ".$val2."&sup2; = ".$sqTotal."<br />
                    &radic;".$sqTotal." = ".$total.$_POST['mes'];
                }       
            echo "<br /><br />"; // Seperate the working out from the input
        }
    } catch(Exception $e) {
        echo $e->getMessage()."<br/><br/>";
    }


?>
<form method='POST' action='index.php'>
Value 1: <input type='text' name='val1' /> 
<?php createSideDropdown("val1type"); ?>
<br /><br />

Value 2: <input type='text' name='val2' /> 
<?php createSideDropdown("val2type"); ?>
<br />
<select name="mes">
<option name="mm">mm</option>
<option name="cm">cm</option>
<option name="m">m</option>
<option name="cm">km</option>
</select>
<br /> 

<input type="submit" name="submit" />
</form>
?>
War es hilfreich?

Lösung

Eine Sache, die Sie sicherlich tun können, ist:

HTML von selbst und PHP von selbst - trennen Sie das. Und dann würde ich den Rest weiterhin sehen.

Außerdem können Sie Ihre Ausnahmen mit JavaScript machen - ich meine, verwenden Sie JavaScript, um die Textfelder zu analysieren und die Fehler mit JavaScript zu schreiben. Anstatt immer das Formular einzureichen. - Trotzdem sollten Sie auch die Felder in PHP analysieren.

Dann machen Sie eine Klasse daraus

/**
 * This method does bla
 * @param Int a
 */

Verwenden Sie keine Globale - könnte mit Klassenattributen erfolgen.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top