PROBLEMA CON SUMA DE VALORES EN PHP
Publicado por Alexander (4 intervenciones) el 18/11/2014 22:53:49
Hola tengo un problema estoy iniciandome en php y he hecho un carrito de compras hasta ahora me lista los valores agregados al carrito pero la suma total siempre se pasa por 50 por 25 y hasta por 100 aqui les dejo el codigo para que me puedan ayudar gracias de antemano
mi carrito.php recoje los valores de los input text que se rellena en el ticket de compra el cual seria este:
ticket.php
ESTE SERIA EL CARRITO:PHP QUE ALMACENA Y LISTA LOS VALORES INTRODUCIDOS EN EL TICKET DE PAGO
como dije el problema esta en que las sumas me las da de forma incorrecta es decir no me suma bien los valores listados
mi carrito.php recoje los valores de los input text que se rellena en el ticket de compra el cual seria este:
ticket.php
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
/*--- FUNCION QUE SUMA LOS VALORES DE LOS CAMPOS----------------------------------------------------------------------*/
<script>
//Función que realiza la suma
function Suma()
{
var adults = document.calculadora.adults.value * <?php echo $shoptour['adults']; ?>;
var childs = document.calculadora.childs.value * <?php echo $shoptour['childs']; ?>;
try
{
//Calculamos el número escrito:
adults = (isNaN(parseInt(adults)))? 0 : parseInt(adults);
childs = (isNaN(parseInt(childs)))? 0 : parseInt(childs);
document.calculadora.resultado.value = adults+childs;
}
//Si se produce un error no hacemos nada
catch(e) {}
}
</script>
<!--- FORMULARIO DEL TICKET ------------------------------------------------------------------------------------------------------>
<form action="carrito.php" method="get" enctype="multipart/form-data" name="calculadora">
<div class="tag">
<p>Fecha :</p>
<p>Hora de Salida :</p>
<p style="margin-top:-10px;">Adultos :</p>
<p style="margin-top:-10px;">Niños :</p>
<p style="margin-top:-10px;">Importe Total : $ </p>
<button style="height:50px; background-color:#36C; color:#FFF;" value="<?php echo $shoptour['tour'];?>" class="botoncompra"> Agregar al carrito
</button>
</div>
<div class="input">
<input type="text" name="fecha2" id="fecha" onfocus="if(this.value=='Formato de fecha: DD / MM / YY')this.value='';" onblur="if(this.value=='')this.value='Formato de fecha: DD / MM / YY';" value="Formato de fecha: DD / MM / YY" readonly class="date1" style="height:40px;">
<br>
<select name="select" style="height:40px; margin-top:20px;">
<option value="<?php echo $shoptour['starttime1']; ?>"><?php echo $shoptour['starttime1']; ?></option>
<option value="<?php echo $shoptour['starttime2']; ?>"><?php echo $shoptour['starttime2']; ?></option>
</select>
<br>
<input name="adults" id="adults" type="text" size="5" onKeyUp="Suma()" style="height:40px; margin-top:17px;">
X <?php echo number_format($shoptour['adults'],2); ?>
<br>
<input name="childs" id="childs" type="text" size="5" onKeyUp="Suma()" style="height:40px; margin-top:17px;">
X <?php echo number_format($shoptour['childs'],2); ?>
</p>
<p style="margin-top:-13px;">
<input name="resultado" id="resultado" size="5" style="border:none; font-family:BirchStd; font-size: 30px;"> <span style="margin-left:-50px;">,00 USD</span></p>
<input type="hidden" name="id" id="id" value="<?php echo $shoptour['id']; ?>">
<input type="hidden" name="tour" id="tour" value="<?php echo $shoptour['tour']; ?>">
</div>
</p>
</form>
ESTE SERIA EL CARRITO:PHP QUE ALMACENA Y LISTA LOS VALORES INTRODUCIDOS EN EL TICKET DE PAGO
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
session_start();
$suma = 0;
$_SESSION['producto'][$_GET['id']] = $_GET['tour']." Adultos: ".$_GET['adults']." Niños: ".$_GET['childs']." Total: ".number_format($_GET['resultado'],2);
echo "<h1>Ud a comprado : </h1>";
echo '<ol>';
foreach($_SESSION['producto'] as $k)
{
echo "<li>".$k."</li>";
$suma += $_GET['resultado'];
}
echo "</ol>";
echo "Importe total : $ ".number_format($suma,2);
?>
como dije el problema esta en que las sumas me las da de forma incorrecta es decir no me suma bien los valores listados
Valora esta pregunta


0