Well, without knowing more information it's hard to see what you want.
How to save multiple values in a variable?
Using an array (array) or an object.
$array = array('a', 'b', 'c'); //Array
//Objecto
$obj = new StdClass();
$obj->foo = 'bar';
$obj->baz = 'bazinga';
I'm creating a multi-product system, (...)
My question is: how to store this value in a variable, since it will have several products, and clicking on them will have to save the value and add them at the end ...
It seems to be wanting to make a shopping cart.
So it shows, if there is no javascript behind, your link will cause a redirect of the page. To avoid this you must use AJAX (Asynchronous Javascript and XML) in PT -> "Asynchronous Javascript and XML"
Example Code
A relatively simple way to make a shopping cart on a single page can be as follows:
cart.php
<?php
class Producto {
public $id;
public $nome;
public $preco;
public $quantidade = 0;
public function __construct($id, $preco, $nome, $quantidade = 0) {
$this->id = $id;
$this->preco = $preco;
$this->nome = $nome;
$this->quantidade = $quantidade;
}
public function adicionar($quantidade = 1) {
$this->quantidade = $this->quantidade + $quantidade;
}
public function remover($quantidade = 1) {
$this->quantidade = $this->quantidade - $quantidade;
if ($this->quantidade < 0)
$this->quantidade = 0;
}
}
class Carrinho implements IteratorAggregate {
public $productosNoCarrinho = array();
public function __construct ($listaDeProductos){
$this->productosNoCarrinho = $listaDeProductos;
}
public function adicionar($id, $quantidade = 1) {
if (!isset($this->productosNoCarrinho[$id]))
return false;
$this->productosNoCarrinho[$id]->adicionar($quantidade);
return true;
}
public function remover($id, $quantidade = 1) {
if (!isset($this->productosNoCarrinho[$id]))
return false;
$this->productosNoCarrinho[$id]->remover($quantidade);
return true;
}
public function total() {
$total = 0;
foreach ($this->productosNoCarrinho as $prod) {
$total += $prod->preco * $prod->quantidade;
}
return $total;
}
public function getIterator() {
return new ArrayIterator($this->productosNoCarrinho);
}
public function mostrar() {
$html = "<table><tr><td>#</td><td>Producto</td><td>Preco</td></tr>";
foreach ($this->productosNoCarrinho as $producto) {
if ($producto->quantidade > 0) {
$html .= "<tr><td>{$producto->quantidade}</td>".
"<td>{$producto->nome}</td>".
"<td>".$producto->preco * $producto->quantidade."</td></tr>";
}
}
$html .= "<tr><td></td><td></td><td>".$this->total()."</td></tr>";
$html .= "</table>";
return $html;
}
public function __toString() {
return $this->mostrar();
}
}
$listaDeProductos = array(
'A1' => new Producto('A1', 499, 'iPad air'),
'A2' => new Producto('A2', 600, 'iPhone 5S'),
'A3' => new Producto('A3', 1200, 'MacBook pro'),
'A4' => new Producto('A4', 1100, 'iMac')
);
session_start();
if(!isset($_SESSION['carrinho'])) {
$_SESSION['carrinho'] = new Carrinho($listaDeProductos);
}
$carrinho = $_SESSION['carrinho'];
if (isset($_GET['action'])) {
if ($_GET['action'] == 'add' && isset($_GET['id'])) {
$carrinho->adicionar($_GET['id']);
} else if ($_GET['action'] == 'remove' && isset($_GET['id'])) {
$carrinho->remover($_GET['id']);
}
//print $carrinho->mostrar();
}
?>
<!DOCTYPE HTML>
<head>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<body>
<div id="lista-de-productos">
<h1>Productos</h1>
<ul>
<?php foreach ($listaDeProductos as $prod) { ?>
<li class="producto"
data-id="<?=$prod->id?>"
data-preco="<?=$prod->preco?>"
data-nome="<?=$prod->nome?>"
>
<?=$prod->nome?> [<?=$prod->preco?>€]
<button class="adicionar">+</button>
<button class="remover">-</button>
</li>
<?php } ?>
</ul>
</div>
<hr>
<h1>Carrinho</h1>
<div id="carrinho">
<?=$carrinho?>
</div>
<script>
$(document).ready( function() {
var carrinhoID = $(this).attr("data-carrinhoID");
$(document).on("click", ".producto .adicionar", function() {
var prodID = $(this).parent().attr("data-id");
$("#carrinho").load("carrinho.php?action=add&id=" +prodID +" #carrinho", function(responseText, textStatus, XMLHttpRequest) {
//Sucesso
});
});
$(document).on("click", ".producto .remover", function() {
var prodID = $(this).parent().attr("data-id");
$("#carrinho").load("carrinho.php?action=remove&id=" +prodID +" #carrinho", function(responseText, textStatus, XMLHttpRequest) {
//Sucesso
});
});
});
</script>
</body>
</html>
Explaining the code ...
What this code does is create a Cart (object of the Cart class) in the session. The control of the cart (and the price and total to pay) is done by the server but the action of add and remove is done by the browser through AJAX.
In terms of logic, what happens is as follows:
The user enters the page link Browser sends a GET request to the server)
The Server receives the request and sees that in the headers, no Session Cookie has been passed.
The Server creates a session for the user and sends you a SetCookie in the headers
The Browser receives and saves the Cookie.
The user adds a product to the cart
The Browser sends an Asynchronous GET request (AJAX) to the server, to the same page, but with parameters in the URL ( link ) with the action to perform and the id of the producto.Also send the Cookie that received the first time the user accessed the page
The Server loads the cart saved in the session, reads the order and realizes that you must add +1 in the amount of product XX in the cart
The Server sends the whole page back
The Browser receives the request, but instead of displaying the entire page, it removes only the part that is inside the div id="cart"
10 ...
The HTTP VERB used is GET, but could be POST (incidentally, it made more sense). However, this is simpler to visualize.
ATTENTION: This code is for demonstration purposes only. SHOULD NOT BE USED IN PRODUCTION as it is INSECURE .