PHP Using SESSION with Array

Asked

Viewed 8,366 times

1

Good evening guys, my goal is when go back to the products page, and go back to the cart, keeping the session variable and also fill the cart page with more products every time the array gets a new one, as it is generating a single product. Thanks in advance. Follow 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 page.php:

    <!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 I go out and go back to the cart continue viewing the product, and present more products in the cart page. It was worth it !

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 :)
  • So I already did, but my code keeps giving the problem of: Notice: Undefined index: value in C: xampp htdocs Project_phpsessions carrinho.php on line 11 // When I go back to Carrinho and is not adding more than one product.

  • Where is the line 11??? I think I already know.

  • array_push($_SESSION['cart'], $_GET['value']); // Is this line.

  • So I passed $_GET['value'] to $variable and it’s not like that either rsr There’s a way to get for $_GET ?

  • Still not understood, got confused, the isset add here, solved that problem, now, to leave the page and keep the value not.

  • value is kept in $_SESSION['cart']

  • You have to start session_start();, before

  • @Thiago, nothing yet?

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.