Pair and odd in php

Asked

Viewed 125 times

-2

I have to make a php code to show the sum of even numbers, odd and total sum, and also the amount of pairs and odd. But when I click the button it changes the number, and does not store nor sum.EX: When I type 10, it increases the pair amount, but when I put another number, for example 5, the number of pairs goes to 0 and the odd number increases. Does anyone know how to solve? Follow the code below:

     <form action="at2.php" method="post">
    <label for="num">Digite um número </A></label><br>
    <input type='number' name='num'><br>
    <button type="submit" name="btn_enviar"> CALCULE </button><br><br>            
         
  
  <br><br>
  
  <?php
 if(isset($_POST['btn_enviar'])){
 $arr = $_POST;
 array_pop($arr);
 $pares = array();
 $impares = array();

foreach ($arr as $value) {
  if(fmod($value,2) == 0){
    $pares[] = $value;
  }else{
    $impares[] = $value;
  }
}
$psum = array_sum($pares);
$isum = array_sum($impares);
echo "Quantidade Pares " . count($pares) . "<br>";
echo "Quantidade Impares " . count($impares) . "<br>";
echo "Soma  Pares: " . $psum . "<br>";
echo "Soma  Impares: " . $isum . "<br>";
echo "Soma Total: " . ($psum + $isum) . "<br>";
 }

  ?>
  
  • 1

    The protocol is stateless, which means he doesn’t retain the status of one requisition for another. If you need your program’s input to be an HTML form, you will have to persist these values elsewhere than in the application’s memory. You can persist on file (even through sessions if you prefer) or on an external service such as a database.

  • I think your approach should be to send all the numbers in the request to be calculated once. But if you want to send the numbers individually, you need to save in some way the data you will need for future calculations or, you can have a javascript in the front end that will accumulate the numbers to send at once to the back end (and also show a preview of the counts if you want)

1 answer

0

Every time you press the 'Calculate' button you start the process from scratch and restart the variables:

In order for a variable to remain the same in several different requests, it should use session variables, it would look something like this:

<form action="at2.php" method="post">
<label for="num">Digite um número </A></label><br>
<input type='number' name='num'><br>
<button type="submit" name="btn_enviar"> CALCULE </button><br><br>            
     

 <br><br>

 <?php
   session_start();

   if(isset($_POST['btn_enviar'])){
   $arr = $_POST;
    array_pop($arr);
    //  $pares = array();
    //  $impares = array();

    foreach ($arr as $value) {
     if(fmod($value,2) == 0){
       $_SESSION['pares'][] = $value;
     }else{
       $_SESSION['impares'][] = $value;
     }
    }
   $psum = array_sum($_SESSION['pares']);
   $isum = array_sum($_SESSION['impares']);
   echo "Quantidade Pares " . count($_SESSION['pares']) . "<br>";
   echo "Quantidade Impares " . count($_SESSION['impares']) . "<br>";
   echo "Soma  Pares: " . $psum . "<br>";
   echo "Soma  Impares: " . $isum . "<br>";
   echo "Soma Total: " . ($psum + $isum) . "<br>";
  • Oops, it worked buddy. Thank you so much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.