PHP Using SESSION with Array

1
Good night, my goal is when to go back to the products page, and go back to the cart, holding the session variable and also fill the cart page with more products each time the array receives a new one as it is generating one product only. Thank you very much in advance. Here is the code below:

    <!DOCTYPE html>
    <!--
    To change this license header, choose License Headers in Project Properties.
    To change this template file, choose Tools | Templates
    and open the template in the editor.
    -->

   <?php session_start();?>

   <html>
   <head lang="pt-br">
   <meta charset="UTF-8">
   <title>E-Commerce</title>
   </head>
   <body>

   <h1>Produtos</h1>



    <ul>
        <li><a href="ipad.php">Ipad</a></li>
        <li><a href="playstation.php">Playstation</a></li>
        <li><a href="xbox.php">Xbox</a></li>
    </ul>

    <br/>

    <a href="carrinho.php">Ver Carrinho</a>
    <?php
    // put your code here
    ?>
    </body>
    </html>

Now the cart.php page:

    <!DOCTYPE html>
    <!--
    To change this license header, choose License Headers in Project Properties.
    To change this template file, choose Tools | Templates
    and open the template in the editor.
    -->
    <?php session_start(); ?>

    <html>
    <head>
    <meta charset="UTF-8">
    <title>E-Commerce</title>
    </head>
    <body>

    <h1>Carrinho de Compras</h1>

    <?php

    $_SESSION['valor'] = array();

    array_push($_SESSION['valor'], $_GET['valor']);

    ?>

    <?php foreach($_SESSION['valor'] as $list):?>
    <ul>
        <li><?php echo $list;?></li>
    </ul>
    <?php endforeach; ?>

    <a href="produtos.php">Continuar Comprando</a>
    <br/>
    <br/>
    <a href="#">Efetuar Pagamentos</a>




    </body>
    </html>

And the product page, which differs from the others only in the product name passed by the url,

    !DOCTYPE html>
    <!--
    To change this license header, choose License Headers in Project Properties.
    To change this template file, choose Tools | Templates
    and open the template in the editor.
    -->

    <?php session_start();?>

    <html lang="pt-br">
    <head>
    <meta charset="UTF-8">
    <title>E-Commerce</title>
    </head>
    <body>

    <h1>IPAD</h1>

    <ul>

        <li><a href="carrinho.php?valor=ipad">Colocar no Carrinho</li>
        <li><a href="carrinho.php">Ver Carrinho</a></li>
        <li><a href="produtos.php">Produtos</a></li>
    </ul>


    <?php
    // put your code here
    ?>
    </body>
    </html>

What I'm not getting is when you leave and return to the cart keep visualizing the product, and present more products on the cart page. Thanks in advance!

    
asked by anonymous 12.03.2015 / 00:15

1 answer

1

Click to test

Code:

<?php 

//iniciando a asessão
session_start ();
header("Content-Type: text/html; charset=utf-8",true);

?>


<html> 
<head> 
    <title> Demonstração de matriz Session usado para carrinho</title> 
</head> 
<body>
    <?php 

    //Calculando o total com a função sizeof() que retorna de key's.

    if (!isset($_GET['carrinho']) && isset($_SESSION['carrinho'])) {

        $contagem = sizeof($_SESSION['carrinho']);

    }

    if (isset($_GET['carrinho'])) {

        $contagem = sizeof($_SESSION['carrinho']) + 1;

    }

    echo "Número de itens no carrinho =" .$contagem."<a href=index.php?vazio=0><br />Remover tudo </a> <br> ";?> 

    <br /><br /><br />
    <a href="index.php?carrinho=1">Calça Brega</a><br />
    <a href="index.php?carrinho=2">Pirulito pop</a><br />
    <a href="index.php?carrinho=3">Carro de luxo</a><br />

</body>
</html>

<?php

if (isset($_GET['carrinho'])){    

    $prod_id = $_GET['carrinho'];

    //isto evita o uso da função array push
    $_SESSION['carrinho'][] = $prod_id;


 } 

//removendo todos os produtos
if (isset($_GET['vazio'])) {

    while (list ($key, $val) = each ($_SESSION['carrinho'])) { 
        //echo "$key -> $val <br>"; 
        unset($_SESSION['carrinho'][$key]);
        $contagem = 0;

    }

}




if (isset($_SESSION['carrinho'])) {
echo '<br /><br />ID dos poutos adicionados:';
foreach($_SESSION['carrinho'] as $list):?>

    <ul>
        <li><?php echo $list;?></li>
    </ul>

<?php endforeach; } 

//fim :)
    
12.03.2015 / 00:28