Fórum Sobre carrinho de compras em php #573667

11/01/2017

0

E ae pessoal, sou iniciante em php e estou tendo um problema com meu carrinho de compras. Toda vez que atualizo a página o ultimo produto adicionado tem a quantidade alterada para mais um produto. Alguém sabe como consertar isso?
código abaixo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
session_start();         
          if(!isset($_SESSION['carrinho'])){
         $_SESSION['carrinho'] = array();
      }
        
      //adiciona produto
        
      if(isset($_GET['acao'])){
           
         //ADICIONAR CARRINHO
         if($_GET['acao'] == 'add'){
            $id = intval($_GET['id']);
            if(!isset($_SESSION['carrinho'][$id])){
               $_SESSION['carrinho'][$id] = 1;
            }else{
               $_SESSION['carrinho'][$id] += 1;
            }
         }
           
         //REMOVER CARRINHO
         if($_GET['acao'] == 'del'){
            $id = intval($_GET['id']);
            if(isset($_SESSION['carrinho'][$id])){
               unset($_SESSION['carrinho'][$id]);
            }
         }
           
         //ALTERAR QUANTIDADE
         if($_GET['acao'] == 'up'){
            if(is_array($_POST['prod'])){
               foreach($_POST['prod'] as $id => $qtd){
                  $id  = intval($id);
                  $qtd = intval($qtd);
                  if(!empty($qtd) || $qtd <> 0){
                     $_SESSION['carrinho'][$id] = $qtd;
                  }else{
                     unset($_SESSION['carrinho'][$id]);
                  }
               }
            }
         }
        
      }        
           
    ?>
<?php include_once "functions.php";
$uri = $_SERVER['PHP_SELF'];
$busca_title = mysqli_query($conexao, "SELECT * FROM menu WHERE url='$uri' limit 1");
 
while($title = mysqli_fetch_array($busca_title)){
    $titulos = $title['title'];   
}
?>
<!DOCTYPE html>
<html lang="pt-br">
    <head>
        <meta charset="utf-8"/>
        <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
        <meta name="viewport" content="width=device-width, initial-scale=1"/>
        <meta name="Descrição" content="Loja Virtual"/>
        <meta name="Autor" content="Márcio Barbosa - arte.marciobarbosa@gmail.com"/>
        <title><?php echo $titulos; ?></title>
        <link rel="stylesheet" type="text/css" href="style.css"/>
        <link rel="stylesheet" href="http://www.w3schools.com/lib/w3.css"/>
        <script>
            function mudafoto1(){
            document.getElementById("foto").src = "<?php echo $prod_img1; ?>";
            }
            function mudafoto2(){
            document.getElementById("foto").src = "<?php echo $prod_img2; ?>";
            }
            function mudafoto3(){
            document.getElementById("foto").src = "<?php echo $prod_img3; ?>";
            }
            function mudafoto4(){
            document.getElementById("foto").src = "<?php echo $prod_img4; ?>";
            }
        </script>
    </head>
    <body>
        <div class="container clearfix">
            <header id="cabecalho">
                <div class="central">
                    <div class="logo">
                        <a href="index.php"><img src="img/logotipo.jpg" alt="www.sualoja.com.br"/></a>
                    </div>
                    <form class="searchbar" method="get" action="busca.php">
                        <input type="text" class="cx-busca" name="busca" placeholder="Digite o que você procura"/>
                        <input type="image" src="img/magnifier.png" class="btn-busca" name="buscar" value="Buscar"/>
                    </form>               
                    <div class="login">
                        <?php
                            if(empty($_SESSION['usuario'])){
                            echo "<p><a href='login.php'>Login</a> | <a href='login.php'>Cadastre-se</a></p>";
                            }else{
                            echo "Seja bem Vindo ".$_SESSION['usuario'];
                            echo "<p><a href='#'>Meus Pedidos</a> | <a href='#'>Minha Conta</a></p>";
                            echo "<a href='sair.php'>Sair</a>";
                            }
                        ?>
                    </div>
                    <nav id="menu">                       
                        <div class="paginas-menu">
                            <?php
                            $sql_menu = "SELECT * FROM menu";
                            $busca_menu = mysqli_query($conexao, $sql_menu);
                                while($row_menu = mysqli_fetch_array($busca_menu)){
                                    $nome = $row_menu['nome'];
                                    $pagina = $row_menu['pagina'];
                                    echo "<a href='$pagina'><div class='menu'>$nome</div></a>";
                                }
                            ?>
                        </div>
                    </nav>
                </div>
            </header>
            <div class="interface">
                <table class="tabela-carrinho">
                <caption><h2 class="detalhes">Carrinho de Compras</h2></caption>
                    <thead>
          <tr>
            <th width="244">Produto</th>
            <th width="79">Quantidade</th>
            <th width="89">Preço</th>
            <th width="100">SubTotal</th>
            <th width="64">Remover</th>
          </tr>
    </thead>
            <form action="?acao=up" method="post">
    <tfoot>
           <tr>
            <td colspan="5"><input type="submit" value="Atualizar Carrinho" /></td>
            <tr>
            <td colspan="5"><a href="index.php">Continuar Comprando</a></td>
    </tfoot>
       
    <tbody>
               <?php
                     if(count($_SESSION['carrinho']) == 0){
                        echo '<tr><td colspan="5">Não há produto no carrinho</td></tr>';
                     }else{
                        require("functions.php");
                                                               $total = 0;
                        foreach($_SESSION['carrinho'] as $id => $qtd){
                              $sql   = "SELECT *  FROM produtos WHERE id= '$id'";
                              $qr    = mysqli_query($conexao, $sql) or die(mysql_error());
                              $ln    = mysqli_fetch_assoc($qr);
                                
                              $nome  = $ln['nome'];
                              $preco = $ln['valor'];
                              $sub   = $ln['valor'] * $qtd;
                                
                              $total += $ln['valor'] * $qtd;
                             
                           echo '<tr>      
                                 <td>'.$nome.'</td>
                                 <td><input type="text" size="3" name="prod['.$id.']" value="'.$qtd.'" /></td>
                                 <td>R$ '.$preco.'</td>
                                 <td>R$ '.$sub.'</td>
                                 <td><a href="?acao=del&id='.$id.'">Remove</a></td>
                              </tr>';
                        }
                           $total = $total;
                           echo '<tr>
                                    <td colspan="4">Total</td>
                                    <td>R$ '.$total.'</td>
                              </tr>';
                     }
               ?>
     
     </tbody>
        </form>
            </table>
        </div>
        <?php include_once 'footer.php'; ?>
    </body>
</html>
Márcio Barbosa

Márcio Barbosa

Responder

Post mais votado

12/01/2017

Muito obrigado pela atenção, mas acabei achando um solução. Eu redirecionei a página depois de adicionar um produto ao carrinho ou incrementar mais um produto. Ficou assim o trecho do código.

1
2
3
4
5
6
7
8
9
10
11
12
13
if(isset($_GET['acao'])){
 
//ADICIONAR CARRINHO
if($_GET['acao'] == 'add'){
$id = intval($_GET['id']);
if(!isset($_SESSION['carrinho'][$id])){
$_SESSION['carrinho'][$id] = 1;
header("location: carrinho.php");
}else{
$_SESSION['carrinho'][$id] += 1;
header("location: carrinho.php");
}
}


Deu certinho. :)

Márcio Barbosa

Márcio Barbosa
Responder

Gostei + 1

Mais Posts

12/01/2017

Victor Machado

Olá Márcio, pelo o que reparei você está utilizando o método GET para fazer as ações, que são pegas da url, então quando você da um refresh na página ele utiliza os dados que estão na url para e refaz a ultima ação.

O que você pode fazer é mudar o seu sistema para utilizar o método POST ao invés de GET.
Responder

Gostei + 0

19/08/2022

Breno

Muito obrigado pela atenção, mas acabei achando um solução. Eu redirecionei a página depois de adicionar um produto ao carrinho ou incrementar mais um produto. Ficou assim o trecho do código.

1
2
3
4
5
6
7
8
9
10
11
12
13
if(isset($_GET['acao'])){
 
//ADICIONAR CARRINHO
if($_GET['acao'] == 'add'){
$id = intval($_GET['id']);
if(!isset($_SESSION['carrinho'][$id])){
$_SESSION['carrinho'][$id] = 1;
header("location: carrinho.php");
}else{
$_SESSION['carrinho'][$id] += 1;
header("location: carrinho.php");
}
}


Deu certinho. :)

Ler Mais...



Vlw mano, eu estava com esse problema também, e estava quebrando a cabeça aqui, mas essa solução foi muito boa.
Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar