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

0

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

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

<?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>
    
asked by anonymous 05.01.2017 / 00:49

2 answers

1

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

<?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>';

?>
    
05.01.2017 / 01:24
1

So you should give:

<?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 declare an array in PHP just to simulate your json .

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

You should change all $dado['xxx'] by $dado->xxx , or put your json in associative array by putting $array_dados = json_decode($_POST["json_dados"], true); .

    
05.01.2017 / 01:19