Quantcast
Channel: Tópicos
Viewing all articles
Browse latest Browse all 14700

Carrinho de compras -

$
0
0
Olá,
estou com problemas no carrinho de compras do meu site  .. ao adicionar um produto ele fica na tabela do carrinho mas se eu adicionar outro apenas aparece o ultima adicionado ao carrinho .. acho que o problema também pode ser do link "comprar" ...  o codigo é o seguinte :

servidores_main.php
Código (PHP):
<html>
<head>
<link href="ram.css" rel="stylesheet" type="text/css" media="screen" />
<style type="text/css">
.titulo {
    font-size: 38px;
    font-style: italic;
    color: #333;
    padding: 10px;
    text-shadow: -1px -1px 0px #101010, 1px 1px 0px #505050;
  }
</style>
</head>
<body>
<?php
include "config.php"; //configurar a ligação á  BD
include "inicial3.php";

$quantidade=4;
$paginas = (isset($_GET['paginas'])) ? (int)$_GET['paginas'] : 1;
$inicio = ($quantidade * $paginas) - $quantidade;

$query = ("select * from produtos where codtipoproduto ='9' LIMIT $inicio ,$quantidade");

if ($_POST['sentido'] == ''){
$sql = ("select * from produtos where codtipoproduto ='9' order by codproduto asc LIMIT $inicio, $quantidade" );

} else if (($_POST['sentido'] == 'asc') && ($_POST['ordem'] == 'preco')){
$sql = ("select * from produtos where codtipoproduto ='9' order by preco asc LIMIT $inicio, $quantidade" );

} else if (($_POST['sentido'] == 'desc') && ($_POST['ordem'] == 'preco')){
$sql = ("select * from produtos where codtipoproduto ='9' order by preco desc LIMIT $inicio, $quantidade" );

} else if (($_POST['sentido'] == 'asc') && ($_POST['ordem'] == 'alfabeto')){
$sql = ("select * from produtos where codtipoproduto ='9' order by nomeproduto asc LIMIT $inicio, $quantidade" );

} else if (($_POST['sentido'] == 'desc') && ($_POST['ordem'] == 'alfabeto')){
$sql = ("select * from produtos where codtipoproduto ='9' order by nomeproduto desc LIMIT $inicio, $quantidade" );

} else if (($_POST['sentido'] == 'desc') && ($_POST['ordem'] == 'recente')){
$sql = ("select * from produtos where codtipoproduto ='9' order by codproduto desc LIMIT $inicio, $quantidade" );

} else if (($_POST['sentido'] == 'asc') && ($_POST['ordem'] == 'recente')){
$sql = ("select * from produtos where codtipoproduto ='9' order by codproduto asc LIMIT $inicio, $quantidade" );
}

$qr = mysql_query($sql) or die (mysql_error());
?>
<div style="position: absolute; left: 350px; top:330px;  ">
<div id="conteudo-master">
<div id="conteudo">
<div class="titulo"> Servidores </div><br>
<form action="index.php?pagina=servidores" method="post">

          <label id="textonormal">Dispor por:</label>

          <select name="ordem">
                <option value="preco">Preco</option>
<option value="alfabeto">Alfabetica</option>
<option value="recente">Disponibilizacao</option>
          </select>

          <select name="sentido">
                <option value="asc">Crescente</option>
                <option value="desc">Decrescente</option>
          </select>

          <input type="submit" name="button"  class="btnLogin" value="Filtrar"/> <br /><br />
</form>


<table cellpadding="20" cellspacing="10"  height="450px" width="800px">
<tr>
<?php
require("config.php");
$qr = mysql_query($sql);
$i = 1;
while ($lista = mysql_fetch_array($qr) or die (mysql_error())){
echo '<font color="black" bgcolor="white">';
?>
<img width="75" height="75" src="images/produtos/<?php echo $lista['imagem']; ?>" alt="<?php echo $lista['imagem']; ?>"> <?php
echo '<h2>'.$lista['nomeproduto'].'</h2> <br>';
echo  $lista['descricao'].'<br />';
echo 'Preço :' .$lista['preco'].'€ <br />';
echo '<a href="carrinho_main.php?pagina=servidores&acao=add&codproduto='.$lista['codproduto'].'">Comprar</a>';
//echo '<a href="carrinho.php?acao=add&id='.$ln['id'].'">Comprar</a>';

}
$i = 0;
$i++;
?>
</table>
<div class="url tabela" width='500px' >
<?php
$sqlTotal = "Select codproduto from produtos where codtipoproduto ='9'";
$qrTotal = mysql_query($sqlTotal) or die (mysql_error());
$numTotal = mysql_num_rows($qrTotal);
$totalPagina = ceil ( $numTotal/$quantidade);

echo "Página ";
for ($i = 1; $i <= $totalPagina; $i++) {
if ($i == $paginas){
echo $i;
}
else
{
echo "<a href=\"index.php?pagina=servidores&paginas=$i\"> $i </a>";
}
echo "</div>";
}
?>
</div>
</font>
</body>
</html>
carrinho_main.php

Código (PHP):
<?php
       include ("config.php");
  include ("inicial3.php");

      if(!isset($_SESSION['carrinho']) || ($_SESSION['carrinho']=="")){
         $_SESSION['carrinho'] = array();
      }

      //adiciona produto

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


         //ADICIONAR CARRINHO
         if($_GET['acao'] == 'add'){
            $codproduto = intval($_GET['codproduto']);
            if(!isset($_SESSION['carrinho'][$codproduto])){
               $_SESSION['carrinho'][$codproduto] = 1;
            }else{
               $_SESSION['carrinho'][$codproduto] += 1;
            }
         }


         //REMOVER CARRINHO
         if($_GET['acao'] == 'del'){
            $codproduto = intval($_GET['codproduto']);
            if(isset($_SESSION['carrinho'][$codproduto])){
               unset($_SESSION['carrinho'][$codproduto]);
            }
         }

         //ALTERAR QUANTIDADE
         if($_GET['acao'] == 'up'){
            if(is_array($_POST['prod'])){
               foreach($_POST['prod'] as $codproduto => $quantidade){
                  $codproduto  = intval($codproduto);
                  $quantidade = intval($quantidade);
                  if(!empty($quantidade) || $quantidade <> 0){
                     $_SESSION['carrinho'][$codproduto] = $quantidade;
                  }else{
                     unset($_SESSION['carrinho'][$codproduto]);
                  }
               }
            }
         }

      }


?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
body {margin:0px; background-color:#CCCCCC;}
#conteudo-master {width: 100% : margin:0px; padding:0px;}
#conteudo [ width:960px; display:block; margin:25px auto; padding:15px; background-color:#ebebeb; position: relative; }
#conteudo hl {font-size:18px; color:#333333; display: block; border-bottom:double 3px #999999; padding-bottom:5px;}
.menu { display:block; margin:0px; padding:5px; }
.tag{
font-size:10px;
color:#FFFFFF;
display:inline;
padding:5px;
margin: 5px;
background-color:#a6bcd7;
}
.titulo {
    font-size: 38px;
    font-style: italic;
    color: #333;
    padding: 10px;
    text-shadow: -1px -1px 0px #101010, 1px 1px 0px #505050;
  }
</style>
<title>Ram@ Informática</title>
</head>
<body>
<div style="position: absolute; left: 350px; top:350px;">
<legend class="titulo"><strong> Carrinho de Compras </strong></legend><br><br>
</div>

<table style="position: absolute; left: 350px; top:520px;  width: 500px; border:0px;" class="tabela">
    <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("config.php");
                        $total = 0;
                        foreach($_SESSION['carrinho'] as $codproduto => $quantidade){
                              $sql   = "SELECT *  FROM produtos WHERE codproduto= '$codproduto'";
                              $qr    = mysql_query($sql) or die(mysql_error());
                              $lista    = mysql_fetch_assoc($qr);

                              $nomeproduto  = $lista['nomeproduto'];
                              $preco = number_format($lista['preco'], 2, ',', '.');
                              $sub   = number_format($lista['preco'] * $quantidade, 2, ',', '.');

                              $total += $lista['preco'] * $quantidade;

                           echo '<tr>      
                                <td>'.$nomeproduto.'</td>
                                <td><input type="text" size="3" name="prod['.$codproduto.']" value="'.$quantidade.'" /></td>
                                <td>€ '.$preco.'</td>
                                <td>€ '.$sub.'</td>
                                <td><a href="?acao=del&codproduto='.$codproduto.'">Remove</a></td>
                             </tr>';
                        }
                           $total = number_format($total, 2, ',', '.');
                           echo '<tr>
                                   <td colspan="3"><b>Total<b></td>
                                   <td>€ '.$total.'</td>
</td>
                             </tr>';
                     }
               ?>

     </tbody>
        </form>

</table>

</body>
</html>
aguardo resposta , obrigada ...

Viewing all articles
Browse latest Browse all 14700