Take values from a post form and forward to an html table

Asked

Viewed 116 times

0

I am layman in PHP and JSON, I have this code below that is working. It brings the values of a form, but all in the same PHP tag, I would like to separate and put in an organized table, for example: Name, Price, Quantity, Subtotal.

Follow the code, I hope you understand, I thank you very much for the strength!

<?php
    if(!isset($_POST["json_dados"])) die("Post não enviado.");

    $array_dados = json_decode($_POST["json_dados"]);


    $total = 0;

    foreach($array_dados as $obj)
    {
        echo 'Nome: '. $obj->nome . '<br>';
        echo 'Preço: '. $obj->preco . '<br>';   
        echo 'Quantidade: '. $obj->qtd . '<br>';        
        echo 'Subtotal: '. $obj->subtotal . '<br>'; 
        echo '<br><br>';

        $total = $total + $obj->subtotal;
    }

    echo 'Total: '.$total;
?>

<table width="95%"  border="1" align="center">
    <tr>
        <td width="26%"><div align="center">Nome</div></td>
        <td width="41%"><div align="center">Preco</div></td>
        <td width="33%"><div align="center">Quantidade</div></td>
        <td width="33%"><div align="center">Subtotal</div></td>
    </tr>
</table>

  • Puts php inside the table tag after tr of the header generating another tr inside the loop and td for each information. Then for every loop he gives you a new line.

  • I’ll test it here

  • I put nothing else after the

  • It was supposed to be table after that foreach($array_data as $obj){ more of the error

2 answers

1

So it must be:

<?php

//if(!isset($_POST["json_dados"])) die("Post não enviado.");


//$array_dados = json_decode($_POST["json_dados"]);

$array_dados = [
    ['nome' => "notebook", 'preco' => "999", 'qtd' => "12", 'subtotal' => "1281" ],
    ['nome' => "tv",       'preco' => "800", 'qtd' => "13", 'subtotal' => "343"  ],
    ['nome' => "teclado",  'preco' => "30",  'qtd' => "11", 'subtotal' => "50"   ],
];

$total = 0;

?>
  <table width="95%"  border="1" align="center">
    <tr>
      <td width="26%"><div align="center"><strong>Nome</strong></div></td>
      <td width="41%"><div align="center"><strong>Preco</strong></div></td>
      <td width="33%"><div align="center"><strong>Quantidade</strong></div></td>
      <td width="33%"><div align="center"><strong>Subtotal</strong></div></td>
    </tr>

    <?php foreach($array_dados as $dado): ?>
            <tr>
              <td width="26%"><div align="center"><?php echo $dado['nome'] ?></div></td>
              <td width="41%"><div align="center"><?php echo $dado['preco'] ?></div></td>
              <td width="33%"><div align="center"><?php echo $dado['qtd'] ?></div></td>
              <td width="33%"><div align="center"><?php echo $dado['subtotal'] ?></div></td>
            </tr>

            <?php $total += $dado['subtotal']; ?>

    <?php endforeach; ?>
  </table>

  <?php echo 'Total: '.$total; ?>

P.S: I declared a array in the PHP only to simulate your json.

I basically moved the foreach to the middle of the table, and for each element of it I put a row, with columns giving echo in that element of array.

The whole of the $dado['xxx'] for $dado->xxx, or else put your json in array associative putting $array_dados = json_decode($_POST["json_dados"], true);.

  • You need to increase the value of $total in your answer by adding the subtotals.

  • 1

    @Antonioalexandrealonsodesi thanks for the warning, I’ll tidy up

  • Sorry friend did not work out these values ['name' => "notebook", 'price' => "999", 'Qtd' => "12", 'subtotal' => "1281" ],

  • 1

    @Hemersonprestes was changing now just putting reminder that should change for example $dado['xxx'] for $dado->xxx or else use $array_dados = json_decode($_POST["json_dados"], true);

1


I scored +1 for lvcs, but put here tb another answer since your json has an array of objects and call them as array will give an error, because object property has to be called with arrow ( -> ).

<?php
if(!isset($_POST["json_dados"])) die("Post não enviado.");
$array_dados = json_decode($_POST["json_dados"]);

$total = 0;

// CABEÇALHO
echo '
  <table width="95%"  border="1" align="center">
    <tr>
      <td width="26%"><div align="center">Nome</div></td>
      <td width="41%"><div align="center">Preco</div></td>
      <td width="33%"><div align="center">Quantidade</div></td>
    <td width="33%"><div align="center">Subtotal</div></td>
    </tr>';

foreach($array_dados as $obj)
{

 echo '
    <tr>
      <td><div align="center">'. $obj->nome . '</div></td>
      <td><div align="center">'. $obj->preco . '</div></td>
      <td><div align="center">'. $obj->qtd . '</div></td>
      <td><div align="center">'. $obj->subtotal . '</div></td>
    </tr>';

    $total = $total + $obj->subtotal;
}

echo '
    <tr><td colspan="4"><div align="center">Total: '.$total.'</div></td></tr>
  </table>';

?>

Browser other questions tagged

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