Find sum of randomly generated array












0















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).



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









share|improve this question

























  • You keep overwriting $randomArray within the loop

    – B001ᛦ
    Nov 15 '18 at 14:52






  • 1





    $SERVER != $_SERVER - you might want to turn on some error messages

    – iainn
    Nov 15 '18 at 14:52











  • Possible duplicate of How do I get PHP errors to display?

    – iainn
    Nov 15 '18 at 14:53
















0















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).



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









share|improve this question

























  • You keep overwriting $randomArray within the loop

    – B001ᛦ
    Nov 15 '18 at 14:52






  • 1





    $SERVER != $_SERVER - you might want to turn on some error messages

    – iainn
    Nov 15 '18 at 14:52











  • Possible duplicate of How do I get PHP errors to display?

    – iainn
    Nov 15 '18 at 14:53














0












0








0








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).



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









share|improve this question
















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).



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






php






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 15:07









Erik Kalkoken

13.2k32552




13.2k32552










asked Nov 15 '18 at 14:49









Josh SmithJosh Smith

97




97













  • You keep overwriting $randomArray within the loop

    – B001ᛦ
    Nov 15 '18 at 14:52






  • 1





    $SERVER != $_SERVER - you might want to turn on some error messages

    – iainn
    Nov 15 '18 at 14:52











  • Possible duplicate of How do I get PHP errors to display?

    – iainn
    Nov 15 '18 at 14:53



















  • You keep overwriting $randomArray within the loop

    – B001ᛦ
    Nov 15 '18 at 14:52






  • 1





    $SERVER != $_SERVER - you might want to turn on some error messages

    – iainn
    Nov 15 '18 at 14:52











  • Possible duplicate of How do I get PHP errors to display?

    – iainn
    Nov 15 '18 at 14:53

















You keep overwriting $randomArray within the loop

– B001ᛦ
Nov 15 '18 at 14:52





You keep overwriting $randomArray within the loop

– B001ᛦ
Nov 15 '18 at 14:52




1




1





$SERVER != $_SERVER - you might want to turn on some error messages

– iainn
Nov 15 '18 at 14:52





$SERVER != $_SERVER - you might want to turn on some error messages

– iainn
Nov 15 '18 at 14:52













Possible duplicate of How do I get PHP errors to display?

– iainn
Nov 15 '18 at 14:53





Possible duplicate of How do I get PHP errors to display?

– iainn
Nov 15 '18 at 14:53












1 Answer
1






active

oldest

votes


















0














You had a couple of issues in your code:




  • typo: $_SERVER, not $SERVER

  • The result output need to be inside the if ... == POST brackets, or they will be called with the input

  • In your loop of assigning the random values, you kept overwriting the $randomArray


  • array_sum() needs to be called on the $randomArray directly, not on an
    array of the random array.



Here is a corrected version of your code:



<!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 = $randomRange;
}

$total = array_sum($randomArray);

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;
}

function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}

?>
</body>
</html>





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322054%2ffind-sum-of-randomly-generated-array%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You had a couple of issues in your code:




    • typo: $_SERVER, not $SERVER

    • The result output need to be inside the if ... == POST brackets, or they will be called with the input

    • In your loop of assigning the random values, you kept overwriting the $randomArray


    • array_sum() needs to be called on the $randomArray directly, not on an
      array of the random array.



    Here is a corrected version of your code:



    <!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 = $randomRange;
    }

    $total = array_sum($randomArray);

    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;
    }

    function test_input($data)
    {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
    }

    ?>
    </body>
    </html>





    share|improve this answer




























      0














      You had a couple of issues in your code:




      • typo: $_SERVER, not $SERVER

      • The result output need to be inside the if ... == POST brackets, or they will be called with the input

      • In your loop of assigning the random values, you kept overwriting the $randomArray


      • array_sum() needs to be called on the $randomArray directly, not on an
        array of the random array.



      Here is a corrected version of your code:



      <!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 = $randomRange;
      }

      $total = array_sum($randomArray);

      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;
      }

      function test_input($data)
      {
      $data = trim($data);
      $data = stripslashes($data);
      $data = htmlspecialchars($data);
      return $data;
      }

      ?>
      </body>
      </html>





      share|improve this answer


























        0












        0








        0







        You had a couple of issues in your code:




        • typo: $_SERVER, not $SERVER

        • The result output need to be inside the if ... == POST brackets, or they will be called with the input

        • In your loop of assigning the random values, you kept overwriting the $randomArray


        • array_sum() needs to be called on the $randomArray directly, not on an
          array of the random array.



        Here is a corrected version of your code:



        <!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 = $randomRange;
        }

        $total = array_sum($randomArray);

        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;
        }

        function test_input($data)
        {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
        }

        ?>
        </body>
        </html>





        share|improve this answer













        You had a couple of issues in your code:




        • typo: $_SERVER, not $SERVER

        • The result output need to be inside the if ... == POST brackets, or they will be called with the input

        • In your loop of assigning the random values, you kept overwriting the $randomArray


        • array_sum() needs to be called on the $randomArray directly, not on an
          array of the random array.



        Here is a corrected version of your code:



        <!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 = $randomRange;
        }

        $total = array_sum($randomArray);

        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;
        }

        function test_input($data)
        {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
        }

        ?>
        </body>
        </html>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 15:02









        Erik KalkokenErik Kalkoken

        13.2k32552




        13.2k32552
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322054%2ffind-sum-of-randomly-generated-array%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            List item for chat from Array inside array React Native

            Thiostrepton

            Caerphilly