2016-08-28 254 views
1

因此,我正在學習PHP,因此,正試圖創建一個程序來簡化分數(至少可以說是一個非常初學者的代碼)。它使用HTML表單,從中抽取分子和分母。但是,在提交表格時,功能將輸出的數字吐出。我的想法是,它正在傳遞每個函數中的'for'循環。任何幫助將不勝感激。爲什麼循環不循環?

<html> 
<head> 
<!-- CSS for echoed data --> 
    <style> 
     p { 
     text-size:12px; 
     text-color:black; 
     } 
    </style> 
</head> 
<body> 
<!-- Defines Form --> 
    <form method = "post" action = "<?php echo $_SERVER['PHP_SELF'];?>"> 
     Numerator: <br /> 
     <input type = "text" name = "numOne"><br /> 
     Denominator: <br /> 
     <input type = "text" name = "numTwo"><br /> 
     <input type = "submit" name = "Submit" value = "Reduce!"> 

    </form> 
<?php 

//Executes task upon form submission 
     if (isset($_POST['Submit'])) { 
      $n1 = $_POST['numOne']; 
      $n2 = $_POST['numTwo']; 
//Defines numerator simplification function 
      function simplifynum ($n1,$n2) { 
       $n1 = $_POST['numOne']; 
       $n2 = $_POST['numTwo']; 
       if ($n1 < $n2){ 
//Loops until numerator and denominator are reduced to simplest form      
        for ($x = 2;$x < $n2;$x++){ 
         $divedn1 = ($n1/$x); 
         $divedn2 = ($n2/$x); 
//If all divided values are purely digits (i.e. are whole numbers) and not equal to zero, sets new numerator and denominators variables to the divided value 
         if (ctype_digit($divedn1) && ctype_digit($divedn2) && ($divedn1 != 0) && ($divedn2 != 0)) { 
          $n1 = $divedn1; 
          $n2 = $divedn2; 
         } 
        } 
//returns numerator data 
        return $n1; 
       } 
      } 
//Defines denominator simplification function   
      function simplifyden ($n1,$n2) { 
       $n1 = $_POST['numOne']; 
       $n2 = $_POST['numTwo']; 
       if ($n1 < $n2){ 
//Loops until numerator and denominator are reduced to simplest form 
        for ($x = 2;$x < $n2;$x++){ 
         $divedn1 = ($n1/$x); 
         $divedn2 = ($n2/$x); 
//If all divided values are purely digits (i.e. are whole numbers) and not equal to zero, sets new numerator and denominators variables to the divided value 
         if (ctype_digit($divedn1) && ctype_digit($divedn2) && ($divedn1 != 0) && ($divedn2 != 0)) { 
          $n1 = $divedn1; 
          $n2 = $divedn2; 
         } 
        } 
//returns denominator data 
        return $n2; 
       } 
      } 






    ?> 
    <!-- Runs functions and reads respective values --> 
    <p><?php echo simplifynum($n1, $n2);?><br /> &#9473;<br /><?php echo simplifyden($n1, $n2);?></p> 
    <?php 
//Unsets form submission 
      unset($_POST['Submit']); 
     } 

    ?> 
</body> 

+0

爲什麼分數的每個組件的功能? –

+1

'ctype_digit'函數需要一個字符串。傳遞一個整數假定這是單個字符的ascii數字。 [手冊](http://php.net/manual/en/function.ctype-digit.php#refsect1-function.ctype-digit-notes)在「註釋」部分說明了這一點。在你的情況下,你可以使用[is_int](https://php.net/is_int)來檢查你是否有一個整數,或者你可以跳過除法和整數檢查使用模數運算符'%'返回部門的其餘部分。所以'25%5 = 0'因爲25可以被5和26%5 = 1'平分。當可以整除時,你會得到零。 –

回答

0

如果問題是簡化分數,因爲規則是,這兩個編號的元件和分母必須由同一個數字整除是正確的,你不需要兩個功能,也通過收集當你已經發送了諸如參數值的函數時,每個函數的post方法。

試試這個

<form method = "post" action = "<?php echo $_SERVER['PHP_SELF'];?>"> 
    Numerator: <br /> 
    <input type = "text" name = "numOne"><br /> 
    Denominator: <br /> 
    <input type = "text" name = "numTwo"><br /> 
    <input type = "submit" name = "Submit" value = "Reduce!"> 

</form> 

<?php 

    if (isset($_POST['Submit'])) { 
     $n1 = $_POST['numOne']; 
     $n2 = $_POST['numTwo']; 
     simplifynum($n1, $n2);//call the function 


} 
     function simplifynum ($n1,$n2) { 
      $menor = 0; 
      if ($n1 < $n2){ $menor= $n1;} 
      else {$menor=$n2;}//I check less variable to find the maximum number of digit in the divisor for and not have decimal results 

       for ($x = 2;$x < $menor;){ //Start the divisor digit two 
        if($n1 % $x == 0 && $n2 % $x==0) //Seeking the digit that is divisible for two numbers by modulo operation% to return the waste if zero is because it is divisible 
        { 
         $n1 = $n1/$x; //I check with the first number 
         $n2 = $n2/$x; //I check the second number 
        } 
        else 
        $x= $x+1;//if the two numbers not divisible increase as the variable in this case would be 3, the first iteration 
       } 

       echo $n1 . " /" . $n2 . "<br/>"; 
       echo "Result : " . ($n1/$n2); 
      } 

?> 
+0

如果您談到您對函數進行的更改(如使用模數),它可能會更有幫助。 –

+0

我以前沒有想過模數,解決了這個問題!謝謝!我想我還有一些東西要學習PHP哈哈。 – MeowyMorganstine