Ir para conteúdo
Fórum Script Brasil
  • 0

Problema...


Abiriguitwoelvs

Pergunta

Seguinte pessoa... eu tenho esse seguinte codigo:

<?
...
//SUBTOTATIS DE CADA PRODUTO
$subtotal = $_SESSION["cesta"][$indice]["QTDE"]*
ereg_replace(',','.',$_SESSION["cesta"][$indice]["preço"]);
//TOTAL GERAL
$total += $subtotal;
...
?>

vamos a explicaçao...

este é um programa que vai calcular as quantidade(s) do(s) produto(s) escolhido pelo

cliente... e calcula também o valor TOTAL que é o preço do subtotal de cada produto...

mas ai esta minha duvida.. quando eu coloco pra roda esse programa ele da um erro... diz

que a variavel total não esta definida.. mas ele calcula o total de todos os produtos

no carrinho!!! Oke devo fazer para definir essa variavel...???

Valeuw pessoal pela atençao...

abraços... smile.gif

Link para o comentário
Compartilhar em outros sites

11 respostass a esta questão

Posts Recomendados

  • 0

Valeu pela atenção brother consegui...

defini a variavel ... $total = $subtotal ...

então fico assim:

<?
//SUBTOTATIS DE CADA PRODUTO
$subtotal = $_SESSION["cesta"][$indice]["QTDE"]*
ereg_replace(',','.',$_SESSION["cesta"][$indice]["preço"]);
//TOTAL GERAL
$total = $subtotal;
$total += $subtotal;
?>

Link para o comentário
Compartilhar em outros sites

  • 0

ai vai o codigo...

wink.gif


<?PHP 
session_start();

$v_prod = $_POST["txtprod"];
$chave  = array_keys($v_prod);
for($i=0; $i<sizeof($chave); $i++){
    $indice = $chave[$i];
       if(!empty($v_prod[$indice]["QTDE"])){
           $cesta[$indice]["CODIGO"] = $v_prod[$indice]["CODIGO"];
     $cesta[$indice]["NOME"]   = $v_prod[$indice]["NOME"];
     $cesta[$indice]["preço"]  = $v_prod[$indice]["preço"];
     $cesta[$indice]["QTDE"]   = $v_prod[$indice]["QTDE"];
  }
}
//verefica se tem produto no carrinho para puxar
if(count($_SESSION["cesta"])>0){
//pega a chave do array
$chave = array_keys($_SESSION["cesta"]);
//pega os dados da sessao
for($i=0; $i<sizeof($chave); $i++){
$indice = $chave[$i];
$cesta[$indice]["CODIGO"] = $_SESSION["cesta"][$indice]["CODIGO"];
$cesta[$indice]["NOME"] = $_SESSION["cesta"][$indice]["NOME"];
$cesta[$indice]["preço"] = $_SESSION["cesta"][$indice]["preço"];
$cesta[$indice]["QTDE"] = $_SESSION["cesta"][$indice]["QTDE"];
}//FECHA FOR
}//FECHA IF
$_SESSION["cesta"] = $cesta;
?>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>principal</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<!--#include file="pisca_link.js" -->
<style type="text/css">
<!--
.fundotab {
	background-color: #FFFFFF;
	border-right-width: 1px;
	border-bottom-width: 1px;
	border-left-width: 1px;
	border-right-style: solid;
	border-bottom-style: solid;
	border-left-style: solid;
	border-right-color: #000000;
	border-bottom-color: #000000;
	border-left-color: #000000;
}
.copyright {
	font-family: Arial;
	font-size: 10px;
	font-weight: lighter;
	color: #FFFFFF;
}
.texto {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 13px;
	color: #000000;
	margin-top: 0px;
	margin-right: 5px;
	margin-bottom: 0px;
	margin-left: 5px;
}
.bordas {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 13px;
	margin-top: 0px;
	margin-right: 5px;
	margin-bottom: 0px;
	margin-left: 5px;
	border: 1px solid #FF0099;
}
-->
</style>
<link href="img.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
-->
</style>
</head>

<body bgcolor="#FF80BF" leftmargin="28" topmargin="0">
<table width="722" height="281" border="0" cellpadding="0" cellspacing="0">
  <tr> 
    <td width="724" height="281" align="left" valign="top" class="fundotab"><br>
      <strong><font color="#FFFFFF" size="1"><em>+</em></font><em><font color="#FF6699" face="Arial, Helvetica, sans-serif">MEU 
      CARRINHO</font></em></strong> <br>
      <br>
      <table width="710" border="0" align="center" cellpadding="0" cellspacing="2" class="bordas">
        <tr bgcolor="#FF99CC" class="texto"> 
          <td width="4%">&nbsp;</td>
          <td width="8%"><strong><font color="#FFFFFF"><span class="style2">&nbsp;Qtde</span></font></strong></td>
          <td width="46%"><strong><font color="#FFFFFF"><span class="style2">&nbsp;Produto</span></font></strong></td>
          <td width="20%"><strong><font color="#FFFFFF"><span class="style2">&nbsp;Valor</span></font></strong></td>
          <td width="22%"><strong><font color="#FFFFFF">&nbsp;Subtotal</font></strong></td>
        </tr>
        

<?
//PEGA A CHAVE
$chave_cesta = @array_keys($_SESSION["cesta"]);
//EXIBE OS PRODUTOS DA CESTA
for($i=0; $i<sizeof($chave_cesta); $i++) { 
$indice = $chave_cesta[$i]; 
//SUBTOTATIS DE CADA PRODUTO
$subtotal = $_SESSION["cesta"][$indice]["QTDE"]*ereg_replace(',','.',$_SESSION["cesta"][$indice]["preço"]);

$total += $subtotal;   // AQUI QUE TA O PROBLEMA

?>


        <tr valign="top" bgcolor="#f1f1f1" class="texto"> 
          <td height="18" valign="middle"> 
            <div align="center"> </div></td>
          <td height="18" valign="middle"><em><font face='Arial' size='2'><? echo $_SESSION["cesta"][$indice]["QTDE"]; ?></font></em></td>
          <td height="18" valign="middle"><em><font face='Arial' size='2'><? echo $_SESSION["cesta"][$indice]["NOME"]; ?></font></em></td>
          <td valign="middle"><em><font face='Arial' size='2'>R$ <? echo $_SESSION["cesta"][$indice]["preço"]; ?></font></em></td>
          <td height="18" valign="middle"><em>R$ <?php echo number_format($subtotal,2,',','.');?></em></td>
        </tr>
        <?
}//FECHA FOR ?>
      </table>
<br>
      <table width="710" border="0" cellpadding="0" cellspacing="0" class="texto">
        <tr>
          <td width="236">&nbsp;</td>
          <td width="316"><div align="right"><strong><font color="#FF6699"><em>Total - </em></font></strong></div></td>
          <td width="158"> <em><strong>&nbsp;R$ <? echo number_format($total,2,',','.'); ?></strong></em></td>
        </tr>
      </table>
      <br>
      <br>
      <table width="710" border="0" cellpadding="0" cellspacing="0" class="texto">
        <tr>
          <td width="356"><div align="center">Voltar e continuar comprando</div></td>
          <td width="354"><div align="center">Finalizar compras</div></td>
        </tr>
      </table> </td>
  </tr>
</table>
...

vALEU FABYO PELA ATENÇAO... smile.gif

Link para o comentário
Compartilhar em outros sites

  • 0

bah q bagunça. tenta assim maninho:

<?
$total = 0; // só iniciei fora do laço.
//PEGA A CHAVE
$chave_cesta = @array_keys($_SESSION["cesta"]);
//EXIBE OS PRODUTOS DA CESTA
for($i=0; $i<sizeof($chave_cesta); $i++) { 
$indice = $chave_cesta[$i]; 
//SUBTOTATIS DE CADA PRODUTO
$subtotal = $_SESSION["cesta"][$indice]["QTDE"]*ereg_replace(',','.',$_SESSION["cesta"][$indice]["preço"]);

$total += $subtotal;   

/* segue o código e a vida é bela  */


?>

meus 2 centavos.

Link para o comentário
Compartilhar em outros sites

  • 0

Valeu mesmo ein...

Tem outro probleminha agora... no inicio do codigo...

<?PHP 
session_start();

$v_prod = $_POST["txtprod"];
$chave  = array_keys($v_prod);
for($i=0; $i<sizeof($chave); $i++){
   $indice = $chave[$i];
      if(!empty($v_prod[$indice]["QTDE"])){
          $cesta[$indice]["CODIGO"] = $v_prod[$indice]["CODIGO"];
    $cesta[$indice]["NOME"]   = $v_prod[$indice]["NOME"];
    $cesta[$indice]["preço"]  = $v_prod[$indice]["preço"];
    $cesta[$indice]["QTDE"]   = $v_prod[$indice]["QTDE"];
 }
}
...

Quando o codigo executado pela primeira vez, ele da um erro e diz que a string $cesta, não foi definida, mas na segunda vez ele já não aparece mais... como eu corrijo isso?

valeu...

Link para o comentário
Compartilhar em outros sites

  • 0

tente assim:

<?PHP 
session_start();

$cesta = array(); // inicializei.. vazia, mas inicializei.
$v_prod = $_POST["txtprod"];
$chave  = array_keys($v_prod);
for($i=0; $i<sizeof($chave); $i++){
  $indice = $chave[$i];
     if(!empty($v_prod[$indice]["QTDE"])){
         $cesta[$indice]["CODIGO"] = $v_prod[$indice]["CODIGO"];
   $cesta[$indice]["NOME"]   = $v_prod[$indice]["NOME"];
   $cesta[$indice]["preço"]  = $v_prod[$indice]["preço"];
   $cesta[$indice]["QTDE"]   = $v_prod[$indice]["QTDE"];
}
}

mais 2 centavos ;P

abração cara

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,1k
    • Posts
      651,8k
×
×
  • Criar Novo...