For security reasons, I recommend using a Framework own to create virtual stores, because they offer greater security, create stores in this way, is usually for people who already have knowledge in the area, and are familiar with security measures, and programming standards, or even for those who simply want to understand the dynamics of a virtual store.
For illustration, I will use a array
multidimensional for the database’s turn, and the respective articles in it. But first, let’s start a session, and create an immutable variable (in order to resist refeshes):
session_start();
if(!isset($_SESSION['cesto'])){
$_SESSION['cesto'] = array();
}
And then the array
with the products that will be used in the example:
// produtos
$produtos = array(
array(
'id' => 1,
'nome' => 'Lapis',
'preco' => 50
),
array(
'id' => 2,
'nome' => 'Marcador',
'preco' => 20
),
array(
'id' => 3,
'nome' => 'Borracha',
'preco' => 10
),
array(
'id' => 4,
'nome' => 'Mochila',
'preco' => 200
)
);
Then you’ll need to list them any way you want, but you’ll have to define a capture method GET
or POST
, of course for the POST
you’d have to use ajax in order to streamline the process, in this case I will use GET
to build a querystring
containing the id
of the product and the action desired.
// listar produtos
foreach($produtos as $produto){
print "<p>{$produto['nome']} ({$produto['preco']}) - <a href=\"?produto={$produto['id']}&a=adicionar\">adicionar</a></p>";
}
Already if you have the products, the listing of the products, variable that will keep them, now just missing the basket, let’s see:
// cesto
if(!empty($_SESSION['cesto'])){
$total = 0;
print "<strong>cesto (" . count($_SESSION['cesto']) . ")</strong><br>";
foreach($_SESSION['cesto'] as $item => $detalhes){
print $detalhes['nome'] . " x " . $detalhes['quantidade'] . "<br>";
$total += $detalhes['quantidade'] * $detalhes['preco'];
}
print "<strong>total: </strong>" . number_format($total, 2);
} else {
print "<strong>cesto vazio</strong>";
}
Now, to make the basket work, so we’re able to add the items into it:
// adicionar itens ao carrinho
if(isset($_GET['produto']) && isset($_GET['a'])){
if($_GET['a'] == 'adicionar'){
if(!empty($_SESSION['cesto'])){
foreach($_SESSION['cesto'] as $item => $produto){
if($item == $_GET['produto']){
$_SESSION['cesto'][$item]['quantidade'] = $produto['quantidade'] + 1;
break;
} else {
foreach($produtos as $produto){
if($produto['id'] == $_GET['produto']){
$_SESSION['cesto'][$produto['id']] = ['nome'=>$produto['nome'], 'preco'=>$produto['preco'], 'quantidade'=>1];
break;
}
}
}
}
} else {
foreach($produtos as $produto){
if($produto['id'] == $_GET['produto']){
$_SESSION['cesto'][$produto['id']] = ['nome'=>$produto['nome'], 'preco'=>$produto['preco'], 'quantidade'=>1];
}
}
}
}
}
EDIT :
To return the products through the database, you can do this way for example.
$produtos = array();
$stmt = $link->query("SELECT * FROM produtos");
while($linha = $stmt->fetch_assoc()){
array_push($produtos, $linha);
}
I recommend you read this tutorial here, there you can see better how to manipulate data in loops, and how to make connections with the database.
You can also see the full code here on Pastebin.
@Miguel is right (I think it refers to if and not foreach), corrected =)
– Guilherme Nascimento
ha, yes yes it was to if clear. Good cart
– Miguel
What if I want to cancel the inserted products ? I can create a stop button on Session ?
– Andre Junior
@Andrejunior to remove all just put on the removal page this
$_SESSION['carrinho'] = array();
, or when you complete the purchase, you will set an empty variable again– Guilherme Nascimento