I am trying to store data from a form in a PHP array, I send the data by the "POST" method but I need it to store more data or products with their respective positions, my code is as follows:
<?php
$sProducto = $_POST["producto"];
$sCantidadExistencia = $_POST["cantidad"];
$sPrecio = $_POST["precioventa"];
$sCantidadProducto = $_POST["cantidad"];
$total = $sCantidadProducto * $sPrecio;
$i = 0;
for ($i=0; $i<50; $i++)
{
$factura = array($sProducto, $sCantidadExistencia, $sPrecio, $sCantidadProducto, $total);
}
print_r($factura);
?>
And the result it gives me is the following: Array ( [0] => Pasta INA [1] => 2 [2] => 11.00 [3] => 2 [4] => 22 )
and if I try to save another product, it changes it and does not save another, example of a new product entered: Array ( [0] => Cepillo de Dientes [1] => 2 [2] => 5.00 [3] => 2 [4] => 10 )
The form must be sent several times and must remember previous products, in addition, I tried to use the cycle for
to do what I indicate, remember the products.
I would appreciate the support, greetings!
I can think of two ways to do it server-side:
Research the pros and cons of each of them. The simplest, not necessarily the best, is 2.
Then in the final code (which you mentioned in a comment) consider "cleaning" the invoice stored in the session, because if the user starts a new purchase process (I guess that's what it is) the data from the previous process will be remain present on the new invoice.
Dude adding [] to the end of your variable turns it into an array accessible via its index.
The way to have persistence of the information is through cookies, this can be achieved in the following way.
First I check if the cookie was created, if it exists, I get the information and convert it into an array (the cookie only stores String) and I assign it to the variable that will store the following data
or you can also use sessions they work the same as cookies.