Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

vivi@ne

Soma de inputs dinamicas complicada

Recommended Posts

Boa noite,

 

Estou com um quebra cabeças que parecia se super simples mas que só parece.

 

Funciona assim: existe uma tabela com os dias da semana e os produtos listados para compra.

Esse produto pode ser comprado com quantidades diferentes em dias diferentes.

No fim da linha de cada produto/semana aparece o sub-total das quantidades marcadas. Até aqui maravilha... o problema é que não consigo somar esses valores (subtotal) num total geral.

 

Será que alguem consegue me ajudar?

Segue o código até onde eu fiz.

 

Brigado e bjs...

 

 

<? $sqpd = query("select * from produto order by id_categoria, titulo asc");
   $i=1;
   $a=0;
   while($tdp = $sqpd->fetch_assoc()): 
   if (($i % 2) == 1){ $fundo="#53301c"; $texto = "#ffb800"; }else{ $fundo="#FFFFFF"; $texto = "#47250F";}
   $a = $a+1;
$i++;?>

<script>
function number_format( number, decimals, dec_point, thousands_sep ) {
var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
var d = dec_point == undefined ? "," : dec_point;
var t = thousands_sep == undefined ? "." : thousands_sep, s = n < 0 ? "-" : "";
var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}
function Calcula<?=$a?>()
{
document.getElementById('total<?=$a?>').value = document.getElementById('qtd<?=$a?>').value * document.getElementById('valor<?=$a?>').value;
document.getElementById('totala<?=$a?>').value =document.getElementById('qtdb<?=$a?>').value * document.getElementById('valor<?=$a?>').value;
document.getElementById('totals<?=$a?>').value =document.getElementById('qtdc<?=$a?>').value * document.getElementById('valor<?=$a?>').value;
document.getElementById('totald<?=$a?>').value =document.getElementById('qtdd<?=$a?>').value * document.getElementById('valor<?=$a?>').value;
document.getElementById('totalf<?=$a?>').value =document.getElementById('qtde<?=$a?>').value * document.getElementById('valor<?=$a?>').value;
document.getElementById('totalg<?=$a?>').value =document.getElementById('qtdf<?=$a?>').value * document.getElementById('valor<?=$a?>').value;

document.getElementById('subtotal<?=$a?>').value = 
(parseFloat(document.getElementById('total<?=$a?>').value)) + 
(parseFloat(document.getElementById('totala<?=$a?>').value)) + 
(parseFloat(document.getElementById('totals<?=$a?>').value))+ 
(parseFloat(document.getElementById('totald<?=$a?>').value)) + 
(parseFloat(document.getElementById('totalf<?=$a?>').value)) +
(parseFloat(document.getElementById('totalg<?=$a?>').value));
document.getElementById('stotal<?=$a?>').innerHTML = number_format(document.getElementById('subtotal<?=$a?>').value, 2,",",".");
}
</script>


                   <input type="hidden" name="valor<?=$a?>" id="valor<?=$a?>"  value="<?=$opreco2?>"/>
                   <input name="qtd<?=$a?>" type="text" id="qtd<?=$a?>" style="width:24px; height:15px; border:none; background-color:#cbc8c8;" size="5" onkeyup="Calcula<?=$a?>();" />
                   <input name="qtdb<?=$a?>" type="text" id="qtdb<?=$a?>" style="width:24px; height:15px; border:none; background-color:#cbc8c8;" size="5" onkeyup="Calcula<?=$a?>();"/>
                   <input name="qtdc<?=$a?>" type="text" id="qtdc<?=$a?>" style="width:24px; height:15px; border:none; background-color:#cbc8c8;" size="5" onkeyup="Calcula<?=$a?>();" />
                   <input name="qtdd<?=$a?>" type="text" id="qtdd<?=$a?>" style="width:24px; height:15px; border:none; background-color:#cbc8c8;" size="5" onkeyup="Calcula<?=$a?>();" /> 
                   <input name="qtde<?=$a?>" type="text" id="qtde<?=$a?>" style="width:24px; height:15px; border:none; background-color:#cbc8c8;" size="5" onkeyup="Calcula<?=$a?>();" />
                   <input name="qtdf<?=$a?>" type="text" id="qtdf<?=$a?>" style="width:24px; height:15px; border:none; background-color:#cbc8c8;" size="5" onkeyup="Calcula<?=$a?>();" />

                   <input name="subtotal<?=$a?>" type="hidden" id="subtotal<?=$a?>" size="4"  />

                   <input name="total<?=$a?>" type="hidden" id="total<?=$a?>" size="4" />
                   <input name="totala<?=$a?>" type="hidden" id="totala<?=$a?>" size="4" />
                   <input name="totals<?=$a?>" type="hidden" id="totals<?=$a?>" size="4" />
                   <input name="totald<?=$a?>" type="hidden" id="totald<?=$a?>" size="4" />
                   <input name="totalf<?=$a?>" type="hidden" id="totalf<?=$a?>" size="4" />
                   <input name="totalg<?=$a?>" type="hidden" id="totalg<?=$a?>" size="4" />

                             <div id='stotal<?=$a?>'></div>
               <? endwhile?>




layout.png

Compartilhar este post


Link para o post
Compartilhar em outros sites

Cara ve se esse codigo vai rodar, eu fiz como um teste, troca os valores pelos valores do banco.

<? 
$i=1;
$a=0;
$tpd =0;
while($tpd <= 3){
   $opreco2 = 5.00;
if (($i % 2) == 1)
{ 
	$fundo="#53301c"; $texto = "#ffb800"; 
}
else
{ 
	$fundo="#FFFFFF"; $texto = "#47250F";
}
   $a = $a+1;
$i++;
?>

<script>
function number_format( number, decimals, dec_point, thousands_sep ) {
var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
var d = dec_point == undefined ? "," : dec_point;
var t = thousands_sep == undefined ? "." : thousands_sep, s = n < 0 ? "-" : "";
var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}
function Calcula<?=$a?>()
{
document.getElementById('total<?=$a?>').value = document.getElementById('qtd<?=$a?>').value * document.getElementById('valor<?=$a?>').value;
document.getElementById('totala<?=$a?>').value =document.getElementById('qtdb<?=$a?>').value * document.getElementById('valor<?=$a?>').value;
document.getElementById('totals<?=$a?>').value =document.getElementById('qtdc<?=$a?>').value * document.getElementById('valor<?=$a?>').value;
document.getElementById('totald<?=$a?>').value =document.getElementById('qtdd<?=$a?>').value * document.getElementById('valor<?=$a?>').value;
document.getElementById('totalf<?=$a?>').value =document.getElementById('qtde<?=$a?>').value * document.getElementById('valor<?=$a?>').value;
document.getElementById('totalg<?=$a?>').value =document.getElementById('qtdf<?=$a?>').value * document.getElementById('valor<?=$a?>').value;

document.getElementById('subtotal<?=$a?>').value = 
(parseFloat(document.getElementById('total<?=$a?>').value)) + 
(parseFloat(document.getElementById('totala<?=$a?>').value)) + 
(parseFloat(document.getElementById('totals<?=$a?>').value))+ 
(parseFloat(document.getElementById('totald<?=$a?>').value)) + 
(parseFloat(document.getElementById('totalf<?=$a?>').value)) +
(parseFloat(document.getElementById('totalg<?=$a?>').value));
document.getElementById('stotal<?=$a?>').innerHTML = number_format(document.getElementById('subtotal<?=$a?>').value, 2,",",".");
//aqui eu faco a conta. Veja que coloquei uma input no final para ver o resultado da conta.
var num1 = 0;
var num2 = 0;
var resultado = 0;
//aqui pego o valor já multiplicado.
num1 = document.getElementById('subtotal<?=$a?>').value;
//aqui eu pego o valor da input, que deixei num value de zero para ela fazer o calculo.
num2 = document.getElementById('totalgeral').value;
//aqui eu somo o valor dos 2.
resultado =  parseInt(num1) +  parseInt(num2);
//coloco o valor da soma na input totalgeral.
document.getElementById('totalgeral').value = resultado;

}
</script>


                   <input type="hidden" name="valor<?=$a?>" id="valor<?=$a?>"  value="<?=$opreco2?>"/>
                   <input name="qtd<?=$a?>" type="text" id="qtd<?=$a?>" style="width:24px; height:15px; border:none; background-color:#cbc8c8;" size="5" onkeyup="Calcula<?=$a?>();" />
                   <input name="qtdb<?=$a?>" type="text" id="qtdb<?=$a?>" style="width:24px; height:15px; border:none; background-color:#cbc8c8;" size="5" onkeyup="Calcula<?=$a?>();"/>
                   <input name="qtdc<?=$a?>" type="text" id="qtdc<?=$a?>" style="width:24px; height:15px; border:none; background-color:#cbc8c8;" size="5" onkeyup="Calcula<?=$a?>();" />
                   <input name="qtdd<?=$a?>" type="text" id="qtdd<?=$a?>" style="width:24px; height:15px; border:none; background-color:#cbc8c8;" size="5" onkeyup="Calcula<?=$a?>();" /> 
                   <input name="qtde<?=$a?>" type="text" id="qtde<?=$a?>" style="width:24px; height:15px; border:none; background-color:#cbc8c8;" size="5" onkeyup="Calcula<?=$a?>();" />
                   <input name="qtdf<?=$a?>" type="text" id="qtdf<?=$a?>" style="width:24px; height:15px; border:none; background-color:#cbc8c8;" size="5" onkeyup="Calcula<?=$a?>();" />

                   <input name="subtotal<?=$a?>" type="hidden" id="subtotal<?=$a?>" size="4"  />

                   <input name="total<?=$a?>" type="hidden" id="total<?=$a?>" size="4" />
                   <input name="totala<?=$a?>" type="hidden" id="totala<?=$a?>" size="4" />
                   <input name="totals<?=$a?>" type="hidden" id="totals<?=$a?>" size="4" />
                   <input name="totald<?=$a?>" type="hidden" id="totald<?=$a?>" size="4" />
                   <input name="totalf<?=$a?>" type="hidden" id="totalf<?=$a?>" size="4" />
                   <input name="totalg<?=$a?>" type="hidden" id="totalg<?=$a?>" size="4" />

                             <div id='stotal<?=$a?>'></div>
               <?
			$tpd++;
			}?>

			<div id='tot'>
				<input name="totalgeral" type="text" id="totalgeral" size="4"  value="0"/>
			</div>

 

Para somar em javascript você tem que colocar o parseInt. ae olha na linha que eu deixei os comentarios. Observe que coloquei uma input text no final para ver o resultado gerado. Espero que tenha te ajudado.

 

Nossa chamei você de cara, desculpe. ahahahha

Compartilhar este post


Link para o post
Compartilhar em outros sites

Nossa 99% resolvido... muito bom mesmo, só fiz uma adaptaçãozinha e fez a soma.

 

você sabe como fazer quando muda o subtotal pra usar um valor novo? Eu testei e ele só soma novos valores sem excluir o antigo.

 

Muitíssimo obrigado por enquanto.

 

Bjs.

Viviane

Compartilhar este post


Link para o post
Compartilhar em outros sites

Ae fiz assim, deve resolver:

 

<? 
$i=1;
$a=0;
$tpd =0;
while($tpd <= 3){
   $opreco2 = 5.00;
if (($i % 2) == 1)
{ 
	$fundo="#53301c"; $texto = "#ffb800"; 
}
else
{ 
	$fundo="#FFFFFF"; $texto = "#47250F";
}
   $a = $a+1;
$i++;
?>

<script>
function number_format( number, decimals, dec_point, thousands_sep ) {
var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
var d = dec_point == undefined ? "," : dec_point;
var t = thousands_sep == undefined ? "." : thousands_sep, s = n < 0 ? "-" : "";
var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}
var temp = <?=$a?>;

function Calcula<?=$a?>()
{
document.getElementById('total<?=$a?>').value = document.getElementById('qtd<?=$a?>').value * document.getElementById('valor<?=$a?>').value;
document.getElementById('totala<?=$a?>').value =document.getElementById('qtdb<?=$a?>').value * document.getElementById('valor<?=$a?>').value;
document.getElementById('totals<?=$a?>').value =document.getElementById('qtdc<?=$a?>').value * document.getElementById('valor<?=$a?>').value;
document.getElementById('totald<?=$a?>').value =document.getElementById('qtdd<?=$a?>').value * document.getElementById('valor<?=$a?>').value;
document.getElementById('totalf<?=$a?>').value =document.getElementById('qtde<?=$a?>').value * document.getElementById('valor<?=$a?>').value;
document.getElementById('totalg<?=$a?>').value =document.getElementById('qtdf<?=$a?>').value * document.getElementById('valor<?=$a?>').value;

document.getElementById('subtotal<?=$a?>').value = 
(parseFloat(document.getElementById('total<?=$a?>').value)) + 
(parseFloat(document.getElementById('totala<?=$a?>').value)) + 
(parseFloat(document.getElementById('totals<?=$a?>').value))+ 
(parseFloat(document.getElementById('totald<?=$a?>').value)) + 
(parseFloat(document.getElementById('totalf<?=$a?>').value)) +
(parseFloat(document.getElementById('totalg<?=$a?>').value));
document.getElementById('stotal<?=$a?>').innerHTML = number_format(document.getElementById('subtotal<?=$a?>').value, 2,",",".");
var val = document.getElementById('subtotal<?=$a?>').value;
document.getElementById('resultado<?=$a?>').value = val;
var num1 = 0;
var num2 = 0;
var resultado = 0;
num1 = document.getElementById('subtotal<?=$a?>').value;
num2 = document.getElementById('totalgeral').value;
resultado =  parseInt(num1) +  parseInt(num2);
var cont = 0;
for (k=1; k < temp; k++)
{
var n1;
n1 = parseInt(document.getElementById('resultado'+k).value);
cont = cont + n1;
}

document.getElementById('totalgeral').value = cont;
}
</script>

				<input name="resultado" id ="resultado<?=$a?>" type="hidden" value="0">
                   <input type="hidden" name="valor<?=$a?>" id="valor<?=$a?>"  value="<?=$opreco2?>"/>
                   <input name="qtd<?=$a?>" type="text" id="qtd<?=$a?>" style="width:24px; height:15px; border:none; background-color:#cbc8c8;" size="5" onkeyup="Calcula<?=$a?>();" />
                   <input name="qtdb<?=$a?>" type="text" id="qtdb<?=$a?>" style="width:24px; height:15px; border:none; background-color:#cbc8c8;" size="5" onkeyup="Calcula<?=$a?>();"/>
                   <input name="qtdc<?=$a?>" type="text" id="qtdc<?=$a?>" style="width:24px; height:15px; border:none; background-color:#cbc8c8;" size="5" onkeyup="Calcula<?=$a?>();" />
                   <input name="qtdd<?=$a?>" type="text" id="qtdd<?=$a?>" style="width:24px; height:15px; border:none; background-color:#cbc8c8;" size="5" onkeyup="Calcula<?=$a?>();" /> 
                   <input name="qtde<?=$a?>" type="text" id="qtde<?=$a?>" style="width:24px; height:15px; border:none; background-color:#cbc8c8;" size="5" onkeyup="Calcula<?=$a?>();" />
                   <input name="qtdf<?=$a?>" type="text" id="qtdf<?=$a?>" style="width:24px; height:15px; border:none; background-color:#cbc8c8;" size="5" onkeyup="Calcula<?=$a?>();" />

                   <input name="subtotal<?=$a?>" type="hidden" id="subtotal<?=$a?>" size="4"  />

                   <input name="total<?=$a?>" type="hidden" id="total<?=$a?>" size="4" />
                   <input name="totala<?=$a?>" type="hidden" id="totala<?=$a?>" size="4" />
                   <input name="totals<?=$a?>" type="hidden" id="totals<?=$a?>" size="4" />
                   <input name="totald<?=$a?>" type="hidden" id="totald<?=$a?>" size="4" />
                   <input name="totalf<?=$a?>" type="hidden" id="totalf<?=$a?>" size="4" />
                   <input name="totalg<?=$a?>" type="hidden" id="totalg<?=$a?>" size="4" />

                             <div id='stotal<?=$a?>'></div>

               <?
			$tpd++;
			}?>

			<div id='tot'>
				<input name="totalgeral" type="text" id="totalgeral" size="4"  value="0"/>

			</div>

 

coloquei mais uma input para servir de somatorio, pois o parseFloat esta dando erros. Entao coloquei essa input para fazer os calculos e depois imprimir direto.

 

Estamos aqui para isso, um ajudar o outro.

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.