Ver Mensaje Individual
  #3 (permalink)  
Antiguo 02/06/2008, 11:17
lexer
 
Fecha de Ingreso: mayo-2008
Mensajes: 9
Antigüedad: 15 años, 10 meses
Puntos: 0
Respuesta: Totalizar tabla por cada registro

Gracias por tu respuesta gnzsoloyo

Ahora estoy tratando mediante dos while anidados en PHP leer registros de Mysql y subtotalizar la duracion y costo por cada campo Ext. Para ello cree una segunda tabla llamada "estructura" donde solo se tiene un campo "Ext", mi idea ir leyendo mediante el primer While de la tabla estructura cada campo Ext para luego con el segundo While leer de la tabla registro, despues mediante un If ver si los campos registro.Ext = estructura. Ext, si son iguales ir sumando duracion y costo por cada Ext del primer While, una vez se termine el segundo while hacer Echo del total duracion y costo. Y buscar segund registro Ext con el primer While y asi sucesivamente.

Esta es la tabla "estructura":

Ext
0
206
208
210
220
222
223
236

Este es el programa:

<?php
$Durtotal=0;
$Costototal=0;
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("llamadas", $con);

//Primer Query Seleccionar extensiones de la estructura
$query = "SELECT estructura.Ext ".
"FROM estructura ".
"ORDER BY estructura.Ext ";

//Segundo Query buscar registros de llamadas
$query2 = "SELECT registro.Ext, registro.duracion, registro.costo ".
"FROM registro, estructura ".
"WHERE registro.Ext = estructura.Ext ORDER BY estructura.Ext ";

$result = mysql_query($query) or die(mysql_error());
$result2 = mysql_query($query2) or die(mysql_error());



// Primer While Seleccionar Ext de estructura
while($row = mysql_fetch_array($result)){
// Segundo While Ver si registro de Ext = Ext estructura
while($row = mysql_fetch_array($result2)){
//Si estructura.Ext = registro.Ext hacer echo e ir sumando duracion y costo
if ($row['registro.Ext'] == $row['estructura.Ext']) //$row['estructura.Ext']
{
echo $row['Ext']." - ". $row['costo']. " - ". $row['duracion'];
echo "<br />";
$Durtotal=$row['costo'] + $Durtotal;
$Costototal=$row['duracion'] + $Costototal;
}
}
echo " DuracionTotal ".$Durtotal. " CostoTotal ".$Costototal ;
echo "<br />";
//Inicializar Durtotal Costototal
$Durtotal=0;
$Costototal=0;
}
?>

Esta es la salida que me da el programa:

0 - 0 - 5
206 - 30 - 1
210 - 120 - 4
220 - 60 - 2
222 - 150 - 5
222 - 0 - 3
223 - 90 - 3
223 - 90 - 3
236 - 60 - 2
236 - 60 - 2
236 - 150 - 5
DuracionTotal 810 CostoTotal 35
DuracionTotal 0 CostoTotal 0
DuracionTotal 0 CostoTotal 0
DuracionTotal 0 CostoTotal 0
DuracionTotal 0 CostoTotal 0
DuracionTotal 0 CostoTotal 0
DuracionTotal 0 CostoTotal 0
DuracionTotal 0 CostoTotal 0

El programa no hace lo que pienso que deberia hacer.

Por favor si alguien me puede dar una luz