0
i am new in the programming area and am creating an Ecommerce, until I came across the following situation when submitting my form via ajax.
Items sold on the site have parameters, for example:
The user can buy Project 01, but within Project 01, he can choose among some items, such as an electrical project, a structural project, among other projects still belonging to Project 01.
I am creating a session to store the parameters of each selected project.
But I’m not sure how I could store the data of a project inside an array that automatically stores the chosen items of its respective project.
This is my request where to send mu ajax to create my session.
<?php
include('../config.php');
//Declaração de sessões
if (isset($_POST['nome']) && isset($_POST['codigo']) && isset($_POST['id']) && isset($_POST['valor'])) {
$conteudo = [$_POST['nome'],(int)$_POST['codigo'],(int)$_POST['id'],$_POST['valor']];
$idProjeto = (int)$_POST['id'];
if (isset($_SESSION['pack']) == false) {
$_SESSION['pack'] = [];
}
if (isset($_SESSION['pack'][$idProjeto]) == false) {
$_SESSION['pack'][$idProjeto];
$_SESSION['pack'][$idProjeto][1] = $conteudo;
}else{
$_SESSION['pack'][$idProjeto][] = $conteudo;
}
echo json_encode($_SESSION['pack']);
}
?>
In my shopping cart I am rescuing my session this way.
<ol>
<!-- Aqui são os nome dos itens que foram selecionados no projeto pelo usuário -->
<?php
$retornoProjeto = $_SESSION['pack'];
foreach ($retornoProjeto as $key2 => $retornoProjeto[]) {
foreach ($retornoProjeto[$key2] as $key3 => $retorno2) {
if ($retornoProjeto[$key2][$key3][1] === $$_SESSION['carrinho']) {
?>
<li><?php echo $retornoProjeto[$key2][$key3][0]; ?></li>
<hr>
<?php
}
}
}
?>
<ol>
<!-- Aqui é o valor dos itens que foram selecionados no projeto pelo usuário -->
<?php
$retornoProjeto = $_SESSION['pack'];
foreach ($retornoProjeto as $key2 => $retornoProjeto[]) {
foreach ($retornoProjeto[$key2] as $key3 => $retorno2) {
if ($retornoProjeto[$key2][$key3][1] === $_SESSION['carrinho']) {
?>
<li>R$ <?php echo $retornoProjeto[$key2][$key3][3]; ?></li>
<hr>
<?php
}
}
}
?>
</ol>
Each Project sold has 7 optional items. If you can help me please would be very grateful!!! I really stuck on this part, first site hehe Thanks in advance!!