Ver Mensaje Individual
  #1 (permalink)  
Antiguo 16/08/2012, 12:56
Avatar de totti026
totti026
 
Fecha de Ingreso: junio-2011
Mensajes: 150
Antigüedad: 12 años, 10 meses
Puntos: 4
Pregunta sumar la misma variable dentro de un while

Que tal, pues tengo esta consulta y dibujo una tabla:
Código PHP:
Ver original
  1. $sql="SELECT YEAR(fecha) AS ANIO, MONTH(fecha) AS MES, MONTHNAME(fecha) AS NOMBRE, SUM(iva) as tiva, SUM(importe) as timporte FROM solicitudes WHERE YEAR(fecha)='$anio' GROUP BY ANIO DESC , MES ASC";
  2. $fechas=mysql_query($sql,$conexion);
  3. echo "<table>";
  4. echo "<thead>";
  5. echo "<tr>";
  6. echo "<th>MES</th>";
  7. echo "<th>IMPORTE</th>";
  8. echo "</tr>";
  9. echo "</thead>";
  10. while ($fech=mysql_fetch_array($fechas))
  11. //inicia while
  12. {
  13. if($fech['MES'] == 1) {
  14. $timporte=$fech['tiva']+$fech['timporte'];
  15. echo "<tr>";
  16. echo "<td>ENERO</td>";
  17. echo "<td >$$timporte</td>";
  18. echo "</tr>";
  19. }
  20. if($fech['MES'] == 2) {
  21. $timporte=$fech['tiva']+$fech['timporte'];
  22. echo "<tr>";
  23. echo "<td>FEBRERO</td>";
  24. echo "<td >$$timporte</td>";
  25. echo "</tr>";
  26. }
  27. if($fech['MES'] == 3) {
  28. $timporte=$fech['tiva']+$fech['timporte'];
  29. echo "<tr>";
  30. echo "<td>MARZO</td>";
  31. echo "<td >$$timporte</td>";
  32. echo "</tr>";
  33. }
  34. if($fech['MES'] == 4) {
  35. $timporte=$fech['tiva']+$fech['timporte'];
  36. echo "<tr>";
  37. echo "<td>ABRIL</td>";
  38. echo "<td >$$timporte</td>";
  39. echo "</tr>";
  40. }
  41. if($fech['MES'] == 5) {
  42. $timporte=$fech['tiva']+$fech['timporte'];
  43. echo "<tr>";
  44. echo "<td>MAYO</td>";
  45. echo "<td >$$timporte</td>";
  46. echo "</tr>";
  47. }
  48. if($fech['MES'] == 6) {
  49. $timporte=$fech['tiva']+$fech['timporte'];
  50. echo "<tr>";
  51. echo "<td>JUNIO</td>";
  52. echo "<td >$$timporte</td>";
  53. echo "</tr>";
  54. }
  55. if($fech['MES'] == 7) {
  56. $timporte=$fech['tiva']+$fech['timporte'];
  57. echo "<tr>";
  58. echo "<td>JULIO</td>";
  59. echo "<td >$$timporte</td>";
  60. echo "</tr>";
  61. }
  62. if($fech['MES'] == 8) {
  63. $timporte=$fech['tiva']+$fech['timporte'];
  64. echo "<tr>";
  65. echo "<td>AGOSTO</td>";
  66. echo "<td >$$timporte</td>";
  67. echo "</tr>";
  68. }
  69. if($fech['MES'] == 9) {
  70. $timporte=$fech['tiva']+$fech['timporte'];
  71. echo "<tr>";
  72. echo "<td>SEPTIEMBRE</td>";
  73. echo "<td >$$timporte</td>";
  74. echo "</tr>";
  75. }
  76. if($fech['MES'] == 10) {
  77. $timporte=$fech['tiva']+$fech['timporte'];
  78. echo "<tr>";
  79. echo "<td>OCTUBRE</td>";
  80. echo "<td >$$timporte</td>";
  81. echo "</tr>";
  82. }
  83. if($fech['MES'] == 11) {
  84. $timporte=$fech['tiva']+$fech['timporte'];
  85. echo "<tr>";
  86. echo "<td>NOVIEMBRE</td>";
  87. echo "<td >$$timporte</td>";
  88. echo "</tr>";
  89. }
  90. if($fech['MES'] == 12) {
  91. $timporte=$fech['tiva']+$fech['timporte'];
  92. echo "<tr>";
  93. echo "<td>DICIEMBRE</td>";
  94. echo "<td >$$timporte</td>";
  95. echo "</tr>";
  96. }
  97.  
  98. }
  99. //fin while
  100. echo "<tr>";
  101. echo "<td></td>";
  102. echo "<td >TOTAL:</td>";
  103. //AQUI EL TOTAL DE $timporte
  104. echo "<td >$</td>";
  105. echo "</tr>";
  106. echo "</table>";

COMO SE VE, EN CADA IF TENGO LA VARIABLE "$timporte" QUE ES EL RESULTADO DE UNA SUMA, PERO AHORA LO QUE QUIERO ES SUMAR TODAS LAS $timporte...Y SACAR EL TOTAL GENERAL...