Redeem a while loop echo input

Asked

Viewed 82 times

0

I’d like to redeem the value of one input with echo of loop of a while and request UPDATE redeeming that amount.

<form method="POST" name="form" action="<?php echo $_SERVER['PHP_SELF'];?>">
                        <?php while ($linhasMotoristas = mysqli_fetch_array($consultaMotoristas)) {
                                $motoristasStatus = $linhasMotoristas['status'];
                                $motoristasId = $linhasMotoristas['idmotoristas'];
                                $motoristasNome = $linhasMotoristas['nome'];
                                $motoristasCpf = $linhasMotoristas['cpf'];
                                $motoristasGanhos = $linhasMotoristas['ganhos']
                            ?>
                                <tr>
                                    <td class="s">
                                        <?php 
                                    if($motoristasStatus == 1){
                                        echo "<div style='color: green;'>●</div>";
                                    }else{
                                        echo "<div style='color: red;'>●</div>";
                                    }
                                    ?>
                                    </td>
                                    <td class="m"><?php echo $motoristasId;?></td>
                                    <td class="n"><?php echo $motoristasNome;?></td>
                                    <td class="c"><input type="hidden" name="td_1" value="<?php echo $motoristasId;?>"><?php echo $motoristasCpf;?>
                                    </td>
                                    <td class="g"><?php echo round($motoristasGanhos, 2);?></td>
                                    <td>
                                    <?php
                                        if (isset($_POST['submit'])?$_POST['submit'] : 0) {
                                        $cpfMotorista = $_POST['td_1'];
                                        $valorBruto = $_POST['valorBruto'];
                                        $porcentagemBase = 5/100;
                                        $EncargosBase = 25/100;

                                        $vezesPorcentagem = $valorBruto*$porcentagemBase;
                                        $menosEncargosBase = $vezesPorcentagem-$EncargosBase;
                                        $resultadoLiquido = $menosEncargosBase*$porcentagemBase;
                                        $resultadoGeral = $resultadoLiquido;

                                        $inserirBanco = "UPDATE getmoney.motoristas SET ganhos = COALESCE(ganhos, 0) + '$resultadoGeral' WHERE idmotoristas = '$cpfMotorista'";
                                        $execInserirBanco = mysqli_query($conexao,$inserirBanco);
                                    }
                                    ?>
                                        <input type="text" name="valorBruto">
                                        <input type="submit" name="submit" value="Calcular">
                                        <?php echo $cpfMotorista;?>
                                        <?php echo round($resultadoGeral,2); ?>
                                    </td>
                                </tr>
                        <?php } ?>
                    </form>

So resgata o ID 25

In this picture, as you can see, it only rescues the ID 25 and I wanted you, according to the line, to rescue the ID user and made the UPDATE of the sum in the field input summed...

  • I didn’t understand anything. Clarify your doubt.

  • <td class="c"><input type="Hidden" name="td_1" value="<? php echo $motoristasId;? >"><? php echo $motoristasCpf;? > </td>

  • Would you like to redeem this loop value in "value="<? php echo $motoristasId;? >" and when using calculate when you can see in the image, update only in the requested ID

  • I don’t understand your code, for me it doesn’t make sense what it does. Inside while rescues form values and update. Your query returns how many different records?

  • If the answer solved your problem mark it as accepted, see https://i.stack.Imgur.com/jx7Ts.png and why https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-accepta reply/1079#1079

1 answer

0


1 - put brackets on names of inputs so that they can be sent in array form to the receiver.

<input type="hidden" name="td_1[]" value="<?php echo $motoristasId; ?>">

<input type="text" name="valorBruto[]">

2 - Remove the Ubmit input from inside the while, place it in a line after the while closure

<tr><td colspan="6"><input type="submit" name="submit"></td></tr>

3 - When submitting the form recover the arrays

    $valorBruto = $_POST['valorBruto'];

    $motoristasId = $_POST['td_1'];

4 - Use a for loop to iterate

    $result = count($valorBruto);
    for ($i = 0; $i < $result; $i++) {

5 - Use a condition for only values não nulos

    $result = count($valorBruto);
    for ($i = 0; $i < $result; $i++) {
    
        if ($valorBruto[$i]!=""){

6 - Inside that if do what must be done :-)

7 - Separate the part of the form from the part that will process the form data.

Complete code

<?php

    $conexao = ...........

if (isset($_POST['submit'])?$_POST['submit'] : 0) {

$valorBruto = $_POST['valorBruto'];

$motoristasId = $_POST['td_1'];

    $result = count($valorBruto);
    for ($i = 0; $i < $result; $i++) {
    
        if ($valorBruto[$i]!=""){
            $cpfMotorista = $motoristasId[$i];
            $porcentagemBase = 5/100;
            $encargosBase = 25/100;
            $vezesPorcentagem = $valorBruto[$i]*$porcentagemBase;
            $menosEncargosBase = $vezesPorcentagem-$encargosBase;
            $resultadoLiquido = $menosEncargosBase*$porcentagemBase;
            $resultadoGeral = $resultadoLiquido;
            
            $inserirBanco = "UPDATE ...................
            $execInserirBanco = mysqli_query($conexao,$inserirBanco);
        }
    
    }

}
?>

<form method="POST" name="form" action="<?php echo $_SERVER['PHP_SELF'];?>">

<?php 

$consultaMotoristas = mysqli_query($conexao,"SELECT...................

while ($linhasMotoristas = mysqli_fetch_array($consultaMotoristas)) {
    $motoristasStatus = $linhasMotoristas['status'];
    $motoristasId = $linhasMotoristas['idmotoristas'];
    $motoristasNome = $linhasMotoristas['nome'];
    $motoristasCpf = $linhasMotoristas['cpf'];
    $motoristasGanhos = $linhasMotoristas['ganhos']
?>
    <tr>
    <td class="s">
        <?php 
        if($motoristasStatus == 1){
        echo "<div style='color: green;'>●</div>";
        }else{echo "<div style='color: red;'>●</div>";
        }
        ?>
    </td>
    <td class="m"><?php echo $motoristasId;?></td>
    <td class="n"><?php echo $motoristasNome;?></td>
    
    <td class="c"><input type="hidden" name="td_1[]" value="<?php echo $motoristasId; ?>">
    </td>
    <td class="g"><?php echo round($motoristasGanhos, 2);?></td>
    <td>                                        
    <input type="text" name="valorBruto[]">
    
    </td>
    </tr>
<?php } ?>

<tr><td colspan="6"><input type="submit" name="submit"></td></tr>
</form>

Browser other questions tagged

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