Screen to enter values

1

I have this code and would like to create a user screen to enter values and they are inserted into their respective arrays, CREDIT (array a) and DEBIT (array b) and create a run button to run the code and print the response. Could you give a help? I do not understand ajax, javascript ... I'm sorry, I imagine it's easy, but I can not.

Follow the code below.

<?php

$a = [];
$b = [];

$iguais = [];
$diferentes = [];
foreach($a as $item){
    $i = array_search($item, $b);
    if($i !== false){
        $iguais[] = '('. $item .', '. $b[$i] .')';
        unset($b[$i]);
    }else{
        $diferentes[] = $item;
    }
}

$d = [$b];
$e = [$diferentes];

$common = array_intersect( $diferentes, $b,$a); 

$diff2 = array_diff( $b, $common );
$diff3 = array_diff( $diferentes, $common );

$diferentes += $b;

echo "IGUAIS - ";
print_r($iguais);

echo "DEBITO - ";
print_r($diff3);

echo "SOMA(a) = ".array_sum($diff3)."\n";

echo "CREDITO - ";
print_r($diff2);

echo "SOMA(a) = ".array_sum($diff2)."\n";

?>

Thank you.

    
asked by anonymous 07.04.2016 / 21:14

2 answers

1

So I understand you want to go putting data in the array and execute your code, I used session variables to store the arrays even when the page is updated (they are only deleted when the browser is closed or the session is destroyed). I felt the need to put 3 actions into the system:

  • Add: just add the value of the input in your array.
  • Execute: Run function code.
  • Terminate: Destroys the session and resets the arrays.

Of course the code can be improved, but I did not see the need to change it any more. Although I could not understand the purpose of it.

 <!-- FORMULARIO DE ADICIONAR -->
<form action="#" method="post">
    <p><input name="number" placeholder="Crédito" type="text"></p>
    <p><input name="number" placeholder="Débito" type="text"></p>
    <p><input type="submit" value="ADICIONAR"></p>
</form>
<!-- FORMULARIO DE EXECUTAR -->
<form action="#" method="post">
    <p><input name="executa" type="hidden"></p>
    <p><input type="submit" value="EXECUTAR"></p>
</form>
<!-- FORMULARIO DE ENCERRAR -->
<form action="#" method="post">
    <p><input name="encerrar" type="hidden"></p>
    <p><input type="submit" value="ENCERRAR"></p>
</form>

<?php
/**
 * Created by PhpStorm.
 * User: Leonardo Vilarinho
 * Date: 07/04/2016
 * Time: 16:31
 */
session_start();


// quando formulario de executar foi enviado
if(isset($_POST['executa']))
{
    // executa função de calcular
    calcula($_SESSION['credito'], $_SESSION['debito']);
}

// quando formulario de encerrar foi enviado
if(isset($_POST['encerrar']))
{
    // destroi arrays e a sessão
    unset($_SESSION);
    session_destroy();
}

// quando formulario de adicionar foi enviado com o credito
if(isset($_POST['credito']))
{
    // adiciona valor do input no array
    array_push($_SESSION['credito'], $_POST['credito']);
}
else
{
    // quando for a primeira vez que entrou na pagina cria um array
    $_SESSION['credito'] = array();
}

// quando formulario de adicionar foi enviado com o debito
if(isset($_POST['debito']))
{
    // adiciona valor do input no array
    array_push($_SESSION['debito'], $_POST['debito']);
}
else
{
    // quando for a primeira vez que entrou na pagina cria um array
    $_SESSION['debito'] = array();
}

// exibe os arrays
var_dump($_SESSION);

// tranforme ie função para ficar légivel
function calcula($credito, $debito)
{
    $iguais = array();
    $diferentes = array();
    foreach($credito as $item){
        $i = array_search($item, $debito);
        if($i !== false){
            $iguais[] = '('. $item .', '. $debito[$i] .')';
            unset($debito[$i]);
        }else{
            $diferentes[] = $item;
        }
    }

    $common = array_intersect( $diferentes, $debito,$credito);

    $diff2 = array_diff( $debito, $common );
    $diff3 = array_diff( $diferentes, $common );

    $diferentes += $debito;

    echo "IGUAIS - ";
    print_r($iguais);

    echo "DEBITO - ";
    print_r($diff3);

    echo "SOMA(credito) = ".array_sum($diff3)."\n";

    echo "CREDITO - ";
    print_r($diff2);

    echo "SOMA(credito) = ".array_sum($diff2)."\n";

}

?>
    
07.04.2016 / 21:54
0

In your html, the inputs must be in a form tag to work. What we need to focus on in html so that it works, is the form's "action" and "method" properties, the "name" property of the text input tags (credit and debit), and the "type" property of the input RUN must be set to "submit".

That is, the HTML form part would look like this:

<form action="endereco/do/seu/script.php" method="post">
   <label>DEBITO: </label><input type="text" name="debito" />
   <label>CREDITO: </label><input type="text" name="credito" />
   <input type="submit" value="EXECUTAR" />
</form>

And you can look up these values in your supposed "script.php" by changing the first 4 lines of code to:

<?php
$a = [];
$b = [];
$a[] = $_POST['credito'];
$b[] = $_POST['debito'];
    
07.04.2016 / 21:53