Foros del Web » Programando para Internet » PHP »

PHP OO Sumar datos de diferentes tablas

Estas en el tema de Sumar datos de diferentes tablas en el foro de PHP en Foros del Web. hola, espero me puedan ayudar, lo que pasa es que quiero sumar los datos de difentes tablas este es mi codigo Este es mmi codigo ...
  #1 (permalink)  
Antiguo 18/09/2011, 18:14
 
Fecha de Ingreso: febrero-2011
Mensajes: 19
Antigüedad: 13 años, 2 meses
Puntos: 0
Sonrisa Sumar datos de diferentes tablas

hola, espero me puedan ayudar, lo que pasa es que quiero sumar los datos de difentes tablas este es mi codigo
Este es mmi codigo para contar o sumar los datos

<html>
<title></title>
<head></head>
<body>
<table> <tr>
<td>Alumnos Desestores </td>
<td>

<?php
extract($_POST);
$desertores= "select count(i.No_Control) as total, i.estado, i.Id_Carrera, g.Id_Carrera, g.Id_Grupo, g.Clave_Tutor, g.Semestre_Ingreso
from indicador1 as i, grupo as g
where i.estado='BD5' and i.Id_Carrera=g.Id_Carrera and g.Id_Carrera='$carrera' and g.Id_Grupo='1' and g.Semestre_Ingreso='$periodo'";
mysql_query($desertores, $con) or die (mysql_error());

$desertores2= "select count(i.No_Control) as total2, i.estado, i.Id_Carrera, g.Id_Carrera, g.Id_Grupo, g.Clave_Tutor, g.Semestre_Ingreso
from indicador1 as i, grupo as g
where i.estado='BD6' and i.Id_Carrera=g.Id_Carrera and g.Id_Carrera='$carrera' and g.Id_Grupo='1' and g.Semestre_Ingreso='$periodo'";
mysql_query($desertores2, $con) or die (mysql_error());

$desertores3= "select count(i.No_Control) as total3, i.estado, i.Id_Carrera, g.Id_Carrera, g.Id_Grupo, g.Semestre_Ingreso
from indicador1 as i, grupo as g
where i.estado='BD7' and i.Id_Carrera=g.Id_Carrera and i.Id_Carrera='$carrera' and g.Id_Grupo='1' and g.Semestre_Ingreso='$periodo'";
mysql_query($desertores3, $con) or die (mysql_error());

$desertores4= "select count(i.No_Control) as total4, i.estado, i.Id_Carrera, g.Id_Carrera, g.Id_Grupo, g.Semestre_Ingreso
from indicador1 as i, grupo as g
where i.estado='BD7' and i.Id_Carrera=g.Id_Carrera and i.Id_Carrera='$carrera' and g.Id_Grupo='1' and g.Semestre_Ingreso='$periodo'";
mysql_query($desertores4, $con) or die (mysql_error());

$desertores5= "select i.estado2, i.Id_Carrera, g.Id_Carrera, g.Id_Grupo, g.Semestre_Ingreso, count(i.No_Control) as total5
from indicador2 as i, grupo as g
where g.Id_Carrera=i.Id_Carrera and i.Id_Carrera='$carrera' and i.estado2='BD5' and g.Id_Grupo='1' and g.Semestre_Ingreso='$periodo'";
mysql_query($desertores5, $con) or die (mysql_error());

$desertores6= "select i.estado2, i.Id_Carrera, g.Id_Carrera, g.Id_Grupo, g.Clave_Tutor and g.Semestre_Ingreso, count(i.No_Control) as total6
from indicador2 as i, grupo as g
where g.Id_Carrera=i.Id_Carrera and i.Id_Carrera='$carrera' and g.Id_Grupo='1' and i.estado2='BD6' and g.Semestre_Ingreso='$periodo'";
mysql_query($desertores6, $con) or die (mysql_error());

$desertores7= "select i.estado2, i.Id_Carrera, g.Id_Carrera, g.Id_Grupo, g.Semestre_Ingreso, count(i.No_Control) as total7
from indicador2 as i, grupo as g
where i.Id_Carrera=g.Id_Carrera and i.Id_Carrera='$carrera' and g.Id_Grupo='1' and i.estado2='BD7' and g.Semestre_Ingreso='$periodo'";
mysql_query($desertores7, $con) or die (mysql_error());

$desertores8= "select i.estado2, i.Id_Carrera, g.Id_Carrera, g.Id_Grupo, g.Semestre_Ingreso, count(i.No_Control) as total8
from indicador2 as i, grupo as g
where i.estado2='BD8' and i.Id_Carrera=g.Id_Carrera and i.Id_Carrera='$carrera' and g.Id_Grupo='1' and g.Semestre_Ingreso='$periodo'";
mysql_query($desertores8, $con) or die (mysql_error());
$desertores9= "select count(i.No_Control) as total9, i.estado3, i.Id_Carrera, g.Id_Carrera, g.Id_Grupo, g.Semestre_Ingreso
from indicador3 as i, grupo as g
where i.estado3='BD5' and i.Id_Carrera=g.Id_Carrera and i.Id_Carrera='$carrera' and g.Id_Grupo='1' and g.Semestre_Ingreso='$periodo'";
mysql_query($desertores9, $con) or die (mysql_error());

$desertores10= "select count(i.No_Control) as total10, i.estado3, i.Id_Carrera, g.Id_Carrera, g.Id_Grupo, g.Semestre_Ingreso
from indicador3 as i, grupo as g
where i.estado3='BD6' and i.Id_Carrera=g.Id_Carrera and i.Id_Carrera='$carrera' and g.Id_Grupo='1' and g.Semestre_Ingreso='$periodo'";
mysql_query($desertores10, $con) or die (mysql_error());
$desertores11= "select count(i.No_Control) as total11, i.estado3, i.Id_Carrera, g.Id_Carrera, g.Id_Grupo, g.Semestre_Ingreso
from indicador3 as i, grupo as g
where i.estado3='BD7' and i.Id_Carrera=g.Id_Carrera and i.Id_Carrera='$carrera' and g.Id_Grupo='1' and g.Semestre_Ingreso='$periodo'";
mysql_query($desertores11, $con) or die (mysql_error());

$desertores12= "select count(i.No_Control) as total12, i.estado3, i.Id_Carrera, g.Id_Carrera, g.Id_Grupo, g.Semestre_Ingreso
from indicador3 as i, grupo as g
where i.estado3='BD8' and i.Id_Carrera=g.Id_Carrera and i.Id_Carrera='$carrera' and g.Id_Grupo='1' and g.Semestre_Ingreso='$periodo'";
mysql_query($desertores12, $con) or die (mysql_error());

$desertores13= "select count(i.No_Control) as total13, i.estado4, i.Id_Carrera, g.Id_Carrera, g.Id_Grupo, g.Semestre_Ingreso
from indicador4 as i, grupo as g
where i.estado4='BD5' and i.Id_Carrera=g.Id_Carrera and i.Id_Carrera='$carrera' and g.Id_Grupo='1' and g.Semestre_Ingreso='$periodo'";
mysql_query($desertores13, $con) or die (mysql_error());

$desertores14= "select count(i.No_Control) as total14, i.estado4, i.Id_Carrera, g.Id_Carrera, g.Id_Grupo, g.Semestre_Ingreso
from indicador4 as i, grupo as g
where i.estado4='BD6' and i.Id_Carrera=g.Id_Carrera and i.Id_Carrera='$carrera' and g.Id_Grupo='1' and g.Semestre_Ingreso='$periodo'";
mysql_query($desertores14, $con) or die (mysql_error());

$desertores15= "select count(i.No_Control) as total15, i.estado4, i.Id_Carrera, g.Id_Carrera, g.Id_Grupo, g.Semestre_Ingreso
from indicador4 as i, grupo as g
where i.estado4='BD7' and i.Id_Carrera=g.Id_Carrera and i.Id_Carrera='$carrera' and g.Id_Grupo='1' and g.Semestre_Ingreso='$periodo'";
mysql_query($desertores15, $con) or die (mysql_error());

$desertores16= "select count(i.No_Control) as total16, i.estado4, i.Id_Carrera, g.Id_Carrera, g.Id_Grupo, g.Semestre_Ingreso
from indicador4 as i, grupo as g
where i.estado4='BD8' and i.Id_Carrera=g.Id_Carrera and i.Id_Carrera='$carrera' and g.Id_Grupo='1' and g.Semestre_Ingreso='$periodo'";
mysql_query($desertores16, $con) or die (mysql_error());

$desertores17= "select count(i.No_Control) as total17, i.estado5, i.Id_Carrera, g.Id_Carrera, g.Id_Grupo, g.Semestre_Ingreso
from indicador5 as i, grupo as g
where i.estado5='BD5' and i.Id_Carrera=g.Id_Carrera and i.Id_Carrera='$carrera' and g.Id_Grupo='1' and g.Semestre_Ingreso='$periodo'";
mysql_query($desertores17, $con) or die (mysql_error());

$desertores18= "select count(i.No_Control) as total18, i.estado5, i.Id_Carrera, g.Id_Carrera, g.Id_Grupo, g.Semestre_Ingreso
from indicador5 as i, grupo as g
where i.estado5='BD6' and i.Id_Carrera=g.Id_Carrera and i.Id_Carrera='$carrera' and g.Id_Grupo='1' and g.Semestre_Ingreso='$periodo1'";
mysql_query($desertores18, $con) or die (mysql_error());

$desertores19= "select count(i.No_Control) as total19, i.estado5, i.Id_Carrera, g.Id_Carrera, g.Id_Grupo, g.Semestre_Ingreso
from indicador5 as i, grupo as g
where i.estado5='BD7' and i.Id_Carrera=g.Id_Carrera and i.Id_Carrera='$carrera' and g.Id_Grupo='1' and g.Semestre_Ingreso='$periodo'";
mysql_query($desertores19, $con) or die (mysql_error());

$desertores20= "select count(i.No_Control) as total20, i.estado5, i.Id_Carrera, g.Id_Carrera, g.Id_Grupo, g.Semestre_Ingreso
from indicador5 as i, tutorado_indicador as t, grupo as g
where i.estado5='BD8' and i.Id_Carrera=g.Id_Carrera and i.Id_Carrera='$carrera' and g.Id_Grupo='1' and g.Semestre_Ingreso='$periodo'";
mysql_query($desertores20, $con) or die (mysql_error());

$suma12 = ($total + $total2 + $total3 + $total4 + $total5 + $total6 + $total7 + $total8 + $total9 + $total10 + $total11 + $total12 + $total13 + $total14 + $total15 + $total16 + $total17 + $total18 + $total19 + $total20 + $total21 + $total22 + $total23 + $total24 + $total25 + $total26);
echo $suma12; ?> </td>
</tr></td>
</table></body>
</html>


Como puedo hacer para que se realice la suma bien o hay otra forma de sumar datos de las tablas sin realizar tanto codigo, espero me puedan ayudar
  #2 (permalink)  
Antiguo 18/09/2011, 19:09
Avatar de Triby
Mod on free time
 
Fecha de Ingreso: agosto-2008
Ubicación: $MX->Gto['León'];
Mensajes: 10.106
Antigüedad: 15 años, 8 meses
Puntos: 2237
Respuesta: Sumar datos de diferentes tablas

Si todos los totales vienen por $_POST, bien podrias modificar los campos para que te lleguen como array:

Código HTML:
Ver original
  1. Total 1: <input type="text" name="total[]" value="" />
  2. ...
  3. Total N: <input type="text" name="total[]" value="" />

De esta forma, recibirias todo en $_POST['total'] (iniciando desde cero, hasta N - 1) y podrias sumarlos facilmente:

Código PHP:
Ver original
  1. $suma = 0;
  2. foreach($_POST['total'] as $total) {
  3.     $suma += $total;
  4. }
  5. echo $suma;

Ahora, si los totales los vas a generar desde tus consultas:
Código PHP:
Ver original
  1. $total = array();
  2. // Ejecutas la consulta y extraes el resultado:
  3. $result = mysql_query(/* aqui la consulta */) or die('Error en consulta: ' . mysql_error());
  4. $row = mysql_fetch_assoc($result);
  5. $total[] = $row['total'];

Logicamente, esto es en caso de que vayas a hacer algo con cada 'total', pero si solo necesitas la suma, lo haces directamente, definiendo primero $suma con valor de cero, y sumando al ejecutar cada consulta $suma += $row['total'];
__________________
- León, Guanajuato
- GV-Foto

Etiquetas: html, mysql, tabla, tablas
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 18:01.