PHP - Consultas con while anidadas

 
Vista:
sin imagen de perfil

Consultas con while anidadas

Publicado por Sergio (2 intervenciones) el 30/12/2014 23:06:07
Hola, tengo este código sencillo que no quiere funcionar. El primer while lista el año y según sea, suma las ventas de cada mes. Sencillo no?

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
require_once("../conexion/conexion.php");
?>
 
<html>
<table align="center" width="800px" border="1">
	<tr>
	<td valign="top" align="center" width="100%" colspan="13">
	<b>IVA VENTAS</b>
	</td>
	</tr>
    <tr>
	<td align="center" bgcolor="red">ANO</td>
	<td align="center">Ene</td>
	<td align="center">Feb</td>
	<td align="center">Mar</td>
	<td align="center">Abr</td>
	<td align="center">May</td>
	<td align="center">Jun</td>
	<td align="center">Jul</td>
	<td align="center">Ago</td>
	<td align="center">Set</td>
	<td align="center">Oct</td>
	<td align="center">Nov</td>
	<td align="center">Dic</td>
    </tr>
 
<?php
 
$sql="SELECT year(fecha)as year
from ivaventas group by year(fecha)";
$resul=mysql_query($sql,$con);
 
while ($regis=mysql_fetch_array($resul))
{
?>
 
		<tr>
				<?php
				$sql="SELECT year(fecha)as anio,
				(SELECT sum(venta)as venta from ivaventas where year(fecha)='2014' and month(fecha)='1' )as ene,
				(SELECT sum(venta)as venta from ivaventas where year(fecha)='2014' and month(fecha)='2' )as feb,
				(SELECT sum(venta)as venta from ivaventas where year(fecha)='2014' and month(fecha)='3' )as mar,
				(SELECT sum(venta)as venta from ivaventas where year(fecha)='2014' and month(fecha)='4' )as abr,
				(SELECT sum(venta)as venta from ivaventas where year(fecha)='2014' and month(fecha)='5' )as may,
				(SELECT sum(venta)as venta from ivaventas where year(fecha)='2014' and month(fecha)='6' )as jun,
				(SELECT sum(venta)as venta from ivaventas where year(fecha)='2014' and month(fecha)='7' )as jul,
				(SELECT sum(venta)as venta from ivaventas where year(fecha)='2014' and month(fecha)='8' )as ago,
				(SELECT sum(venta)as venta from ivaventas where year(fecha)='2014' and month(fecha)='9' )as sep,
				(SELECT sum(venta)as venta from ivaventas where year(fecha)='2014' and month(fecha)='10' )as oct,
				(SELECT sum(venta)as venta from ivaventas where year(fecha)='2014' and month(fecha)='11' )as nov,
				(SELECT sum(venta)as venta from ivaventas where year(fecha)='2014' and month(fecha)='12' )as dic
				 from ivaventas group by year(fecha)";
				$res=mysql_query($sql,$con);
				while($reg=mysql_fetch_array($res))
				{
				?>
 
				<td>
				<?php echo $regis["year"]; ?>
				</td>
				<td>
				<?php echo $reg["ene"]; ?>
				</td>
				<td>
				<?php echo $reg["feb"]; ?>
				</td>
				<td>
				<?php echo $reg["mar"]; ?>
				</td>
				<td>
				<?php echo $reg["abr"]; ?>
				</td>
				<td>
				<?php echo $reg["may"]; ?>
				</td>
				<td>
				<?php echo $reg["jun"]; ?>
				</td>
				<td>
				<?php echo $reg["jul"]; ?>
				</td>
				<td>
				<?php echo $reg["ago"]; ?>
				</td>
				<td>
				<?php echo $reg["sep"]; ?>
				</td>
				<td>
				<?php echo $reg["oct"]; ?>
				</td>
				<td>
				<?php echo $reg["nov"]; ?>
				</td>
				<td>
				<?php echo $reg["dic"]; ?>
				</td>
 
				<?php
				}
				?>
 
		</tr>
 
<?php
}
?>
 
</table>
 
</html>

El resultado es este:


iva

que estoy haciendo mal??
Desde ya gracias por los aportes
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de xve
Val: 3.943
Oro
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Consultas con while anidadas

Publicado por xve (6935 intervenciones) el 31/12/2014 09:13:29
Hola Sergio, yo creo que lo que te sobra es el segundo while... creo que no tienes que estar, ya que la consulta te devuelve un solo valor por mes y año... no creo que tengas que utilizar un while, aunque no se si es ese el problema.

Prueba a quitar el while de dentro... coméntanos, ok?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
sin imagen de perfil

Consultas con while anidadas

Publicado por Sergio (2 intervenciones) el 31/12/2014 14:12:51
Hola xve, Gracias por tu respuesta. Voy a probar lo que me dices. El caso es que primero consulto por el año, y despues por la sumatoria de la facturación de los días, para sacar el total del mes. Donde dice '2014' debe ir el año consultado Como tienen que ir en un mismo <TR> el llamado lo coloco dentro del segundo while, o afuera, no hace diferencia. Es la forma que se me ocurre. Es que DEBE haber OTRA forma seguro, si alguien la sabe...
FELIZ AÑO NUEVO!!!!!!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar