jeudi 15 novembre 2018

Find sum of randomly generated array

Right now I'm working on a PHP project where the user enters the lower limit and upper limit and the program will generate 5 numbers between that limit, print it out the randomly generated 5 numbers, and find and print the sum. For some reason my program isn't getting passed the input (lower and upper limits). Any help is appreciated, thanks all!

<!DOCTYPE html>
<html lang = "en">
   <head>
      <title>Sum of the Digits!</title>
   </head>
   <body>
      <h1>Find the sum of five digits!</h1>
      <p>Enter in the lower and upper limits of the numbers you would like to 
         generate. Press "Calculate" to calculate the sum of the genreated numbers!
      </p>
      <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
         Enter the lower limit: <input type = "number" name = "lowLim">
         Enter the upper limit: <input type = "number" name = "upLim">
         <input type = "submit">
      </form>
      <?php
         if($SERVER["REQUEST_METHOD"] == "POST"){

         $lowerLim = test_input($_POST["lowLim"]);
         $upperLim = test_input($_POST["upLim"]);
         $randomArray = array();
         $sumArray = array();
         $total = 0;
         $arrCounter = 0;
         var_dump ($lowerLim);



         for($i = 0; $i < 5; $i++){
         $randomRange = rand("$lowerLim","$upperLim");
         $randomArray = array($randomRange[i]);
         }

         $sumArray = array($randomArray);
         $total = array_sum($sumArray);
         }
         function test_input($data){
         $data = trim($data);
         $data = stripslashes($data);
         $data = htmlspecialchars($data);
         return $data;
         }




         echo "First number generated: ".$randomArray[0];
         echo "Second number generated: ".$randomArray[1];
         echo "Third number generated: ".$randomArray[2];
         echo "Fourth number generated: ".$randomArray[3];
         echo "Fifth number generated: ".$randomArray[4];



         echo "The sum of the generated digits is:".$total;


         ?>
   </body>
</html>




Aucun commentaire:

Enregistrer un commentaire