Foros del Web » Programando para Internet » PHP »

[SOLUCIONADO] Sumar totales en horizontal y vertical

Estas en el tema de Sumar totales en horizontal y vertical en el foro de PHP en Foros del Web. Hola, Tengo el siguiente problema: La base de datos es categoria Enero Febrero Marzo ......Totales Salarios 00 00 00 Irpf 00 00 00 Gas 00 ...
  #1 (permalink)  
Antiguo 25/02/2014, 09:19
 
Fecha de Ingreso: agosto-2012
Ubicación: Barcelona
Mensajes: 243
Antigüedad: 11 años, 8 meses
Puntos: 1
Sumar totales en horizontal y vertical

Hola,

Tengo el siguiente problema:
La base de datos
es
categoria Enero Febrero Marzo ......Totales
Salarios 00 00 00
Irpf 00 00 00
Gas 00 00 00
---
---
Total 00 00 00


La cual tiene guardado los valore por meses y categorias el vaciado de estas estan bien lo que necesito es que me de los totales po fila y por columna.
Si alguien me puede sugerir como lo puedo hacer

dejo aquí el programa

Código PHP:
Ver original
  1. <?php
  2.  
  3. include("conexion.php");
  4. $consulta=("SELECT * FROM gastos  ORDER BY categoria");
  5.  
  6.  
  7. ?>
  8.  
  9. <?php
  10.  $datos=mysql_query($consulta,$conexion);
  11.  
  12.  
  13.  
  14.  
  15.  $tr_empresas="";
  16.  
  17.  
  18.         while($row = mysql_fetch_assoc($datos))
  19.        
  20.          
  21.        
  22.  {
  23.  
  24.    $tr_empresas.="
  25.  
  26.      
  27.    
  28.    <tr>
  29.         <td align='center'>".$row['categoria']."</td>
  30.         <td align='center'>".$row['01']."</td>
  31.         <td align='center'>".$row['02']."</td>
  32.         <td align='center'>".$row['03']."</td>
  33.         <td align='center'>".$row['04']."</td>
  34.         <td align='center'>".$row['05']."</td>
  35.         <td align='center'>".$row['06']."</td>
  36.         <td align='center'>".$row['07']."</td>
  37.         <td align='center'>".$row['08']."</td>
  38.         <td align='center'>".$row['09']."</td>
  39.         <td align='center'>".$row['10']."</td>
  40.         <td align='center'>".$row['11']."</td>
  41.         <td align='center'>".$row['12']."</td> 
  42.    </tr>
  43.     <tr>
  44.         <td><br></td>
  45.     </tr>
  46.  
  47.     ";
  48.    
  49.     }?>
  50.    
  51.  
  52.  
  53.  
  54. <style>
  55.  body{font-family:Arial, Helvetica, sans-serif; font-weight:bold;}
  56.  table.tabla_empresa{border:none;border-collapse:collapse}
  57.  table.tabla_empresa{background-color:#BFBFBF;width:120%;}
  58.  table.tabla_empresa th{background-color:#4D4D4D;color:#FFFFFF;border:1px solid #E6E6FA}
  59.  table.tabla_empresa td{background-color:#FFFFFF;color:#08285A;font-weight:bold;}
  60.  </style>
  61.  
  62.  
  63.  
  64. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  65.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  66. <!--
  67.  
  68.     Design by Free CSS Templates
  69.     http://www.freecsstemplates.org
  70.     Released for free under a Creative Commons Attribution License
  71.  
  72.     Name       : Eponymous
  73.     Version    : 1.0
  74.     Released   : 20130222
  75.  
  76. -->
  77. <html xmlns="http://www.w3.org/1999/xhtml">
  78.     <head>
  79.         <meta name="keywords" content="" />
  80.         <meta name="description" content="" />
  81.         <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/>
  82.      
  83.         <title></title>
  84.         <link href="css/paginacion.css" type="text/css" rel="stylesheet">
  85.         <link href="css/style.css" rel="stylesheet" type="text/css" />
  86.         <link href="css/menu.css" rel="stylesheet" type="text/css" />
  87.     </head>
  88.     <body>
  89.         <div id="bg">
  90.             <div id="outer">
  91.                 <div id="header">
  92.                     <div id="logo">
  93.                         <h1>
  94.                             <a href="index.html"></a>
  95.                         </h1>
  96.                     </div><br><br>
  97.                    
  98.                    
  99.                 <h2  style="font-weight:bold; color:#FF8E18; text-align:center; font-size: 36px;">Gastos por año</h2>
  100.                 <p   style="font-weight:bold; color:#FF8E18; text-align:center; font-size: 12px;"> Para imprimir exportar la base de datos a excel</p>
  101.  
  102.                
  103.  
  104.          <br />
  105.          <div id= "centrar_tabla">
  106.          <table class='tabla_empresa'>
  107.          <tr>
  108.                  <th>Descripcion</th>
  109.                  <th>Enero</th>
  110.                  <th>Febrero</th>
  111.                  <th>Marzo</th>
  112.                  <th>Abril</th>
  113.                  <th>Mayo</th>
  114.                  <th>Junio</th>
  115.                  <th>Julio</th>
  116.                  <th>Agosto</th>
  117.                  <th>Septiembre</th>
  118.                  <th>Octubre</th>
  119.                  <th>Noviembre</th>
  120.                  <th>Diciembre</th>
  121.                  <th>Totales</th>
  122.                  
  123.          </tr>
  124.                 <?php echo $tr_empresas;?>
  125.          </table>
  126.                    
  127.                
  128.     </body>
  129. </html>


Gracias y saludos
  #2 (permalink)  
Antiguo 25/02/2014, 11:10
 
Fecha de Ingreso: junio-2011
Ubicación: Barcelona
Mensajes: 212
Antigüedad: 12 años, 10 meses
Puntos: 17
Respuesta: Sumar totales en horizontal y vertical

Has probado añadir los valores un array y luego sumarlos? Ejemplo:

Código PHP:
Ver original
  1. <?php
  2. $datos=mysql_query($consulta,$conexion);
  3. $tr_empresas="";
  4. while($row = mysql_fetch_assoc($datos)){
  5. $gastos = array($row['01'],$row['02'],$row['03'],$row['04'],$row['05'],$row['06'],$row['07'],$row['08'],$row['09'],$row['10'],$row['11'],$row['12']);
  6. $tr_empresas.="
  7.     <tr>
  8.        <td align='center'>".$row['categoria']."</td>
  9.        <td align='center'>".$row['01']."</td>
  10.        <td align='center'>".$row['02']."</td>
  11.        <td align='center'>".$row['03']."</td>
  12.        <td align='center'>".$row['04']."</td>
  13.        <td align='center'>".$row['05']."</td>
  14.        <td align='center'>".$row['06']."</td>
  15.        <td align='center'>".$row['07']."</td>
  16.        <td align='center'>".$row['08']."</td>
  17.        <td align='center'>".$row['09']."</td>
  18.        <td align='center'>".$row['10']."</td>
  19.        <td align='center'>".$row['11']."</td>
  20.        <td align='center'>".$row['12']."</td>
  21.        <td align='center'>".array_sum($gastos)."</td>
  22.   </tr>
  23.    <tr>
  24.        <td><br></td>
  25.    </tr>";
  26. }
  27. ?>

A ver que tal te va.

Un saludo
__________________
Twitter: @ed33x
  #3 (permalink)  
Antiguo 25/02/2014, 11:23
 
Fecha de Ingreso: agosto-2012
Ubicación: Barcelona
Mensajes: 243
Antigüedad: 11 años, 8 meses
Puntos: 1
Respuesta: Sumar totales en horizontal y vertical

Hola,

Gracias por responder, lo he probado y va perfecto en horizontal pero me falta ahora que sume en vertical, como se podría hacer?

De nuevo gracias ...
  #4 (permalink)  
Antiguo 25/02/2014, 12:16
 
Fecha de Ingreso: junio-2011
Ubicación: Barcelona
Mensajes: 212
Antigüedad: 12 años, 10 meses
Puntos: 17
Respuesta: Sumar totales en horizontal y vertical

Aunque hay otros metodos mejores y mas optimizados para que te hagas una idea de las funciones que tienes que utilizar serian:


Código PHP:
Ver original
  1. <?php
  2. $datos=mysql_query($consulta,$conexion);
  3. $tr_empresas="";
  4. $gastos_vertical_1 = array();
  5. $gastos_vertical_2 = array();
  6. $gastos_vertical_3 = array();
  7. $gastos_vertical_4 = array();
  8. $gastos_vertical_5 = array();
  9. $gastos_vertical_6 = array();
  10. $gastos_vertical_7 = array();
  11. $gastos_vertical_8 = array();
  12. $gastos_vertical_9 = array();
  13. $gastos_vertical_10 = array();
  14. $gastos_vertical_11 = array();
  15. $gastos_vertical_12 = array();
  16. while($row = mysql_fetch_assoc($datos)){
  17. $gastos_horizontal = array($row['01'],$row['02'],$row['03'],$row['04'],$row['05'],$row['06'],$row['07'],$row['08'],$row['09'],$row['10'],$row['11'],$row['12']);
  18. array_push($gastos_vertical_1, $row['01');
  19. array_push($gastos_vertical_2, $row['02');
  20. array_push($gastos_vertical_3, $row['03');
  21. array_push($gastos_vertical_4, $row['04');
  22. array_push($gastos_vertical_5, $row['05');
  23. array_push($gastos_vertical_6, $row['06');
  24. array_push($gastos_vertical_7, $row['07');
  25. array_push($gastos_vertical_8, $row['08');
  26. array_push($gastos_vertical_9, $row['09');
  27. array_push($gastos_vertical_10, $row['10');
  28. array_push($gastos_vertical_11, $row['11');
  29. array_push($gastos_vertical_12, $row['12');
  30. $tr_empresas.="
  31.    <tr>
  32.        <td align='center'>".$row['categoria']."</td>
  33.        <td align='center'>".$row['01']."</td>
  34.        <td align='center'>".$row['02']."</td>
  35.        <td align='center'>".$row['03']."</td>
  36.        <td align='center'>".$row['04']."</td>
  37.        <td align='center'>".$row['05']."</td>
  38.        <td align='center'>".$row['06']."</td>
  39.        <td align='center'>".$row['07']."</td>
  40.        <td align='center'>".$row['08']."</td>
  41.        <td align='center'>".$row['09']."</td>
  42.        <td align='center'>".$row['10']."</td>
  43.        <td align='center'>".$row['11']."</td>
  44.        <td align='center'>".$row['12']."</td>
  45.        <td align='center'>".array_sum($gastos_horizontal)."</td>
  46.   </tr>
  47.    <tr>
  48.        <td><br></td>
  49.    </tr>";
  50. }
  51. ?>

Es para que veas como serias mas o menos y que funciones tienes que utilizar para sumar todas las entradas del array utiliza array_sum

Un saludo.
__________________
Twitter: @ed33x
  #5 (permalink)  
Antiguo 26/02/2014, 04:00
 
Fecha de Ingreso: agosto-2012
Ubicación: Barcelona
Mensajes: 243
Antigüedad: 11 años, 8 meses
Puntos: 1
Respuesta: Sumar totales en horizontal y vertical

Ok, funciona todo bien.
Dejo aquí lo que hice.
Código PHP:
Ver original
  1. <?php
  2.     $datos=mysql_query($consulta,$conexion);
  3.     $tr_empresas="";
  4.     $tr_totales="";
  5.     $gastos_vertical_1 = array();
  6.     $gastos_vertical_2 = array();
  7.     $gastos_vertical_3 = array();
  8.     $gastos_vertical_4 = array();
  9.     $gastos_vertical_5 = array();
  10.     $gastos_vertical_6 = array();
  11.     $gastos_vertical_7 = array();
  12.     $gastos_vertical_8 = array();
  13.     $gastos_vertical_9 = array();
  14.     $gastos_vertical_10 = array();
  15.     $gastos_vertical_11 = array();
  16.     $gastos_vertical_12 = array();
  17.     $gastos_vertical_13 = array();
  18.    
  19.     while($row = mysql_fetch_assoc($datos)){
  20.     $gastos_horizontal = array($row['01'],$row['02'],$row['03'],$row['04'],$row['05'],$row['06'],$row['07'],$row['08'],$row['09'],$row['10'],$row['11'],$row['12']);
  21.     array_push($gastos_vertical_1, $row['01']);
  22.      array_push($gastos_vertical_13, $row['01']);
  23.     array_push($gastos_vertical_2, $row['02']);
  24.      array_push($gastos_vertical_13, $row['02']);
  25.     array_push($gastos_vertical_3, $row['03']);
  26.      array_push($gastos_vertical_13, $row['03']);
  27.     array_push($gastos_vertical_4, $row['04']);
  28.      array_push($gastos_vertical_13, $row['04']);
  29.     array_push($gastos_vertical_5, $row['05']);
  30.      array_push($gastos_vertical_13, $row['05']);
  31.     array_push($gastos_vertical_6, $row['06']);
  32.      array_push($gastos_vertical_13, $row['06']);
  33.     array_push($gastos_vertical_7, $row['07']);
  34.      array_push($gastos_vertical_13, $row['07']);
  35.     array_push($gastos_vertical_8, $row['08']);
  36.      array_push($gastos_vertical_13, $row['08']);
  37.     array_push($gastos_vertical_9, $row['09']);
  38.      array_push($gastos_vertical_13, $row['09']);
  39.     array_push($gastos_vertical_10, $row['10']);
  40.      array_push($gastos_vertical_13, $row['10']);
  41.     array_push($gastos_vertical_11, $row['11']);
  42.      array_push($gastos_vertical_13, $row['11']);
  43.     array_push($gastos_vertical_12, $row['12']);
  44.      array_push($gastos_vertical_13, $row['12']);
  45.     $tr_empresas.="
  46.       <tr>
  47.           <td align='center'>".$row['categoria']."</td>
  48.           <td align='center'>".$row['01']."</td>
  49.           <td align='center'>".$row['02']."</td>
  50.           <td align='center'>".$row['03']."</td>
  51.           <td align='center'>".$row['04']."</td>
  52.           <td align='center'>".$row['05']."</td>
  53.           <td align='center'>".$row['06']."</td>
  54.           <td align='center'>".$row['07']."</td>
  55.           <td align='center'>".$row['08']."</td>
  56.           <td align='center'>".$row['09']."</td>
  57.           <td align='center'>".$row['10']."</td>
  58.           <td align='center'>".$row['11']."</td>
  59.           <td align='center'>".$row['12']."</td>
  60.           <td align='center'>".array_sum($gastos_horizontal)."</td>
  61.          
  62.      </tr>
  63.       <tr>
  64.         <td><br></td>
  65.          
  66.       </tr>";
  67.     }
  68.     $tr_totales.="
  69.    
  70.          <tr>
  71.              <td align='center'></td>
  72.             <td align='center'>".array_sum($gastos_vertical_1)."</td>
  73.              <td align='center'>".array_sum($gastos_vertical_2)."</td>
  74.              <td align='center'>".array_sum($gastos_vertical_3)."</td>
  75.              <td align='center'>".array_sum($gastos_vertical_4)."</td>
  76.              <td align='center'>".array_sum($gastos_vertical_5)."</td>
  77.              <td align='center'>".array_sum($gastos_vertical_6)."</td>
  78.              <td align='center'>".array_sum($gastos_vertical_7)."</td>
  79.              <td align='center'>".array_sum($gastos_vertical_8)."</td>
  80.              <td align='center'>".array_sum($gastos_vertical_9)."</td>
  81.              <td align='center'>".array_sum($gastos_vertical_10)."</td>
  82.              <td align='center'>".array_sum($gastos_vertical_11)."</td>
  83.              <td align='center'>".array_sum($gastos_vertical_12)."</td>
  84.              <td align='center'>".array_sum($gastos_vertical_13)."</td>
  85.          
  86.          </tr>
  87.    
  88.    
  89.     ";
  90.     ?>
  91.    
  92.  
  93.  
  94.  
  95. <style>
  96.  body{font-family:Arial, Helvetica, sans-serif; font-weight:bold;}
  97.  table.tabla_empresa{border:none;border-collapse:collapse}
  98.  table.tabla_empresa{background-color:#BFBFBF;width:120%;}
  99.  table.tabla_empresa th{background-color:#4D4D4D;color:#FFFFFF;border:1px solid #E6E6FA}
  100.  table.tabla_empresa td{background-color:#FFFFFF;color:#08285A;font-weight:bold;}
  101.  </style>
  102.  
  103.  
  104.  
  105. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  106.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  107. <!--
  108.  
  109.     Design by Free CSS Templates
  110.     http://www.freecsstemplates.org
  111.     Released for free under a Creative Commons Attribution License
  112.  
  113.     Name       : Eponymous
  114.     Version    : 1.0
  115.     Released   : 20130222
  116.  
  117. -->
  118. <html xmlns="http://www.w3.org/1999/xhtml">
  119.     <head>
  120.         <meta name="keywords" content="" />
  121.         <meta name="description" content="" />
  122.         <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/>
  123.      
  124.         <title></title>
  125.         <link href="css/paginacion.css" type="text/css" rel="stylesheet">
  126.         <link href="css/style.css" rel="stylesheet" type="text/css" />
  127.         <link href="css/menu.css" rel="stylesheet" type="text/css" />
  128.     </head>
  129.     <body>
  130.         <div id="bg">
  131.             <div id="outer">
  132.                 <div id="header">
  133.                     <div id="logo">
  134.                         <h1>
  135.                             <a href="index.html"></a>
  136.                         </h1>
  137.                     </div><br><br>
  138.                    
  139.                    
  140.                 <h2  style="font-weight:bold; color:#FF8E18; text-align:center; font-size: 36px;">Gastos por año</h2>
  141.                 <p   style="font-weight:bold; color:#FF8E18; text-align:center; font-size: 12px;"> Para imprimir exportar la base de datos a excel</p>
  142.  
  143.                
  144. <!--        <form method="GET" action=""/>
  145.  
  146.         Ingrese la fecha a buscar formato(aaaa-mm-dd)
  147.         <input type="date" size="32" value="" name="fechareg"/>
  148.         <a style="position:relative; margin: 0px 780px 0px;" href="contabilidad.php" >Volver</a>
  149.         </form>
  150. -->
  151.          <br />
  152.          <div id= "centrar_tabla">
  153.          <table class='tabla_empresa'>
  154.          <tr>
  155.                  <th>Descripcion</th>
  156.                  <th>Enero</th>
  157.                  <th>Febrero</th>
  158.                  <th>Marzo</th>
  159.                  <th>Abril</th>
  160.                  <th>Mayo</th>
  161.                  <th>Junio</th>
  162.                  <th>Julio</th>
  163.                  <th>Agosto</th>
  164.                  <th>Septiembre</th>
  165.                  <th>Octubre</th>
  166.                  <th>Noviembre</th>
  167.                  <th>Diciembre</th>
  168.                  <th>Totales</th>
  169.                  
  170.          </tr>
  171.                 <?php echo $tr_empresas;?>
  172.          </table>
  173.           <div id= "centrar_tabla">
  174.          <table class='tabla_empresa'>
  175.          <tr>
  176.                  <th><---Totales---></th>
  177.                  <th>Enero</th>
  178.                  <th>Febrero</th>
  179.                  <th>Marzo</th>
  180.                  <th>Abril</th>
  181.                  <th>Mayo</th>
  182.                  <th>Junio</th>
  183.                  <th>Julio</th>
  184.                  <th>Agosto</th>
  185.                  <th>Septiembre</th>
  186.                  <th>Octubre</th>
  187.                  <th>Noviembre</th>
  188.                  <th>Diciembre</th>
  189.                  <th>Totales</th>
  190.                  
  191.          </tr>
  192.                 <?php echo $tr_totales;?>
  193.          </table>          
  194.  
  195.            
  196.                
  197.     </body>
  198. </html>

De nuevo gracias y un saludo

Etiquetas: horizontal, html, mysql, select, sql, tabla, totales, vertical
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 22:41.