Foros del Web » Programando para Internet » PHP »

sumar la misma variable dentro de un while

Estas en el tema de sumar la misma variable dentro de un while en el foro de PHP en Foros del Web. Que tal, pues tengo esta consulta y dibujo una tabla: @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código PHP: Ver original $sql = "SELECT YEAR(fecha) AS ANIO, MONTH(fecha) AS MES, ...
  #1 (permalink)  
Antiguo 16/08/2012, 12:56
Avatar de 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...
  #2 (permalink)  
Antiguo 16/08/2012, 13:14
Colaborador
 
Fecha de Ingreso: mayo-2008
Ubicación: $MX['VZ']['Xalapa']
Mensajes: 3.005
Antigüedad: 15 años, 11 meses
Puntos: 528
Respuesta: sumar la misma variable dentro de un while

Si hicieras algo como:
$meses[1]='Enero';
$meses[2]='Febrero'; etc
no necesitas tantos if y tanto código:

Código PHP:
Ver original
  1. $total=0;
  2. while ($fech=mysql_fetch_array($fechas)){
  3.    $timporte=$fech['tiva']+$fech['timporte'];
  4.    echo "
  5.   <tr>
  6.       <td>".$meses[$fech['MES']]."</td>
  7.       <td >$$timporte</td>
  8.   </tr>";
  9.    $total+=$timporte;
  10. }
  11. echo "<tr>
  12. <td></td>
  13. <td >TOTAL:</td>
  14. <td >$total</td>
  15. </tr>
  16. </table>";
  #3 (permalink)  
Antiguo 16/08/2012, 13:23
Avatar de totti026  
Fecha de Ingreso: junio-2011
Mensajes: 150
Antigüedad: 12 años, 10 meses
Puntos: 4
Respuesta: sumar la misma variable dentro de un while

wow si es mucho menos codigo! gracias...
solo una pregunta mas,esto
$meses[1]='Enero';
$meses[2]='Febrero'; etc
es antes del while?
voy aprendiendo!
  #4 (permalink)  
Antiguo 16/08/2012, 13:26
Colaborador
 
Fecha de Ingreso: mayo-2008
Ubicación: $MX['VZ']['Xalapa']
Mensajes: 3.005
Antigüedad: 15 años, 11 meses
Puntos: 528
Respuesta: sumar la misma variable dentro de un while

Claro, de lo contrario, llenarías una y otra vez el arreglo con los valores de los meses

Etiquetas: sql, tabla, variables
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 08:36.