In this cart I have to select whether the customer wants to share the value of the product more than once a month
example.
product costs $ 50.00, divided in three months would be 3 x $ 19.00, divided in six months would be 6x 10.00.
this part is right.
My problem is time to save it in the db.
to display the correct values I used a switch in every way that is selected brings a different value.
when I save the db, always brings the reset value, does not bring the value of the variable that place.
follows the codes.
SESSION
Code: Select all
session_start();
if(!isset($_SESSION['carrinho'])){
$_SESSION['carrinho'] = array();
}
//adiciona produto
if(isset($_GET['acao'])){
//ADICIONAR CARRINHO
if($_GET['acao'] == 'add'){
$idproduto = intval($_GET['idproduto']);
if(!isset($_SESSION['carrinho'][$idproduto])){
$_SESSION['carrinho'][$idproduto] = 1;
}else{
//$_SESSION['carrinho'][$idproduto] += 1;
}
}
//REMOVER CARRINHO
if($_GET['acao'] == 'del'){
$idproduto = intval($_GET['id']);
if(isset($_SESSION['carrinho'][$idproduto])){
unset($_SESSION['carrinho'][$idproduto]);
}
}
//ALTERAR QUANTIDADE
if($_GET['acao'] == 'up'){
if(is_array($_POST['prod'])){
foreach($_POST['prod'] as $idproduto => $qtd){
$idproduto = intval($idproduto);
$qtd = intval($qtd);
if(!empty($qtd) || $qtd <> 0){
$_SESSION['carrinho'][$idproduto] = $qtd;
}else{
unset($_SESSION['carrinho'][$idproduto]);
}
}
}
}
}Code: Select all
if(count($_SESSION['carrinho']) == 0){
echo '<tr><td colspan="5">Não há produto no carrinho</td></tr>';
}else{
require("conexao.php");
$total = 0;
foreach($_SESSION['carrinho'] as $idproduto => $qtd){
$sql = "SELECT * FROM estoque WHERE idproduto= '$idproduto'";
$qr = mysql_query($sql) or die(mysql_error());
$ln = mysql_fetch_assoc($qr);
$idprodut = $ln['idproduto'];
$marca = $ln['marca'];
$modelo = $ln['modelo'];
$cor = $ln['cor'];
$numero = $ln['numero'];
$preco = number_format($ln['precovenda'], 2, ',', '.');
$sub = number_format($ln['precovenda'] * $qtd, 2, ',', '.');
//if (!empty($_POST)) {
switch ($_POST['code']) {
case 1:
$sql6 = mysql_query("SELECT precovenda FROM estoque WHERE idproduto= '$idproduto'");
$resu6 = mysql_fetch_assoc($sql6);
$value = $resu6['precovenda'];
$forma = 'A vista';
$formacb = 'A Vista';
$valuetotal = $value * 1;
break;
case 2:
$sql7 = mysql_query("SELECT precotres FROM estoque WHERE idproduto= '$idproduto'");
$resu7 = mysql_fetch_assoc($sql7);
$value = $resu7['precotres'];
$forma = '3x de ';
$formacb = '3x';
$valuetotal = $value * 3;
break;
case 3:
$sql8 = mysql_query("SELECT precoseis FROM estoque WHERE idproduto= '$idproduto'");
$resu8 = mysql_fetch_assoc($sql8);
$value = $resu8['precoseis'];
$forma = '6x de ';
$formacb = '6x';
$valuetotal = $value * 6;
break;
//default:
// $value = '';
}
//}
$total += $valuetotal * $qtd;
echo '<tr>
<td>'.$idprodut.'</td>
<td>'.$marca.'</td>
<td>'.$modelo.'</td>
<td>'.$cor.'</td>
<td>'.$numero.'</td>
<td><input type="text" size="1" name="prod['.$idproduto.']" value="'.$qtd.'" /></td>
<td><form method="post" action="carrinho.php">
<select name="code" onchange="this.form.submit();">
<option>'; if (!empty($_POST)){ echo $formacb; } else{ echo 'Selecione...'; } echo '</option>
<option value="1">A Vista</option>
<option value="2">3x</option>
<option value="3">6x</option>
</select>
<td>'.$forma.' R$'. number_format($value, 2, ',', '.'). ' </td>';
echo '<td>R$ '. number_format($valuetotal, 2, ',', '.'). '</td>
<td><a href="?acao=del&id='.$idproduto.'">Remove</a></td>
</tr>';
}
$total = number_format($total, 2, ',', '.');
$valuetota = number_format($valuetotal, 2, ',', '.');
echo '<tr>
<td> </td>
<td colspan="7">Total</td>
<td>R$ '.$total.'</td>
<td> </td>
</tr>';
}
?>finish button and querys(in this part is my problem, whent i hit finish button, always send the variable $valuetota with 0)
Code: Select all
if(isset($_POST['enviar'])){
$formapagamento = $_POST['forma'];
$sqlinserevenda = mysql_query("INSERT into pedido(valortotal,data,formapagamento) VALUES ('$total',NOW(),'$formapagamento')")or die(mysql_error());
$idvenda = mysql_insert_id();
foreach($_SESSION['carrinho'] as $idproduto => $qtd){
$valortemp1 = mysql_query("SELECT * FROM valortemp");
$valortempr = mysql_fetch_assoc($valortemp1);
$valorvenda = $valortempr['valortemp'];
$sql1 = "SELECT * FROM estoque WHERE idproduto= '$idproduto'";
$qr1 = mysql_query($sql1) or die(mysql_error());
$ln1 = mysql_fetch_assoc($qr1);
//$sub1 = $ln1['precovenda'];
$sqlinsereitens = mysql_query("INSERT into itenspedido(idpedido,idproduto,quantidadeitens,valorunitario,data) VALUES('$idvenda','$idproduto','$qtd','$valuetota',NOW())")or die(mysql_error());
$sqlestoque = ("UPDATE estoque SET saldoestoque = saldoestoque - $qtd WHERE idproduto = $idproduto ") or die(mysql_error());
$sqlestoquesql = mysql_query($sqlestoque) or die(mysql_error());
$sqlestoque2 = ("UPDATE estoque SET totalsaida = totalsaida + $qtd WHERE idproduto = $idproduto ") or die(mysql_error());
$sqlestoquesql2 = mysql_query($sqlestoque2) or die(mysql_error());
}
echo '<script> alert("Venda concluida!'.$valuetotal.'")</script>';
echo '<script> window.location = "vender.php"</script>';
unset($_SESSION['carrinho']);
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<select id ="forma" name="forma">
<option>Selecione...</option>
<option value="Dinheiro">Dinheiro</option>
<option value="Cartão de Crédito">Cartão de Crédito</option>
<option value="Cartão de Débito">Cartão de Débito</option>
<option value="Cheque">Cheque</option>
<option value="Vale">Vale</option>
</select>
<input type="submit" name="enviar" value="Finalizar Venda" />
</form>