Forum Moderators: coopster

Message Too Old, No Replies

adding total of a column

trying to add the total amount of a column filtered by month

         

mikey74

4:18 pm on Jul 1, 2007 (gmt 0)

10+ Year Member



i have try a lot of things not a expert on php but trying to do my best any help how i can get the total amount of expensives from a column filtered by month.

What im trying to do is my building condo calculation expensive of how many each apartment has to pay for this i have a table named gastos, this table will be filtered by month and it has to calculate the total amount in the column costo that total will be divided by the amount of apartments in the building to have what each has to pay every month

this is the test code theres a lot of coments of all the test i made from what i have readed on the web, no matter what i have try i have not been able to display the total or i get wrong numbers with the total:

The database table is

id_g int 100 primary
nombre varchar 255
descripcion text
costo decimal 30,2
mes int 4
fecha int 4

<?php
$x_costo = NULL;
$ox_costo = NULL;
$z_costo = NULL;
$ar_x_costo = NULL;
$ari_x_costo = NULL;
$nStartRec = 0;
$nStopRec = 0;
$totalRows_prueba = 0;
$nRecCount = 0;
$nRecActual = 0;
$sKeyMaster = "";

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc()? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string")? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue!= "")? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue!= "")? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue!= "")? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue!= "")? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue!= "")? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$colname_prueba = "-1";
if (isset($_GET['mes'])) {
$colname_prueba = $_GET['mes'];
}
mysql_select_db($database_condominio, $condominio);
$query_prueba = sprintf("SELECT * FROM gastos WHERE mes = %s ORDER BY id_g ASC", GetSQLValueString($colname_prueba, "int"));
$prueba = mysql_query($query_prueba, $condominio) or die(mysql_error());
$row_prueba = mysql_fetch_assoc($prueba);
$totalRows_prueba = mysql_num_rows($prueba);

<?php //require('includes/common/KT_functions.inc.php');?>
<?php
/**********/
//$total = 0; // Declare total variable
//$sql="SELECT costo FROM gastos"; // SQL statement to run
//$result=mysql_query($sql); // Query database

// Loops through query result and adds each value to $total
//while ($row = mysql_fetch_array($result)) {
//$total = $total + $row[costo];
//}
//echo $total; // Prints total to screen
//$resultado = mysql_query($query_probando);
//while($row_probando['costo'] = mysql_fetch_assoc($resultado))
//{
// la suma de las columnas/registros vengan las que vengan
//$suma = array_sum($row_probando['costo']);
//}
/*$row_prueba['costo']->MoveFirst();
$total = 0;
while ($prueba->EOF){
$total = $total + $row_prueba['costo'];
$prueba->MoveNext();
}*/
//while ($prueba->EOF){
//$tot_x_costo = 0; // Initialise total to zero for aggregation
//$x_costo = $row_probando['costo']
//$tot_x_costo += $row_prueba['costo'];
//$prueba->MoveNext();
// Move to the first record
// Set the last record to display
if ($nDisplayRecs <= 0) { // Display all records
$nDisplayRecs = $nTotalRecs;
}
$nStartRec = 1;
SetUpStartRec(); // Set up start record position

//$nStopRec = $nStartRec + $nDisplayRecs - 1;

$nRecCount = $nStartRec - 1;
if (@mysql_num_rows($prueba) > 0) {
@mysql_data_seek($prueba, $nStartRec -1);
}
$tot_x_costo = 0; // Initialise total to zero for aggregation
$nRecActual = 0;
while (($row = @mysql_fetch_array($prueba)) && ($nRecCount < $nStopRec)) {
$nRecCount = $nRecCount + 1;
if ($nRecCount >= $nStartRec) {
$nRecActual++;
$x_costo = $row_prueba['costo'];
$tot_x_costo += $x_costo; // Accumulate Total
}
}

?>
<? $xx = $row_prueba['fecha'];?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

</head>

<body>
<table width="800" border="1">
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td><?php do {?>
<table width="800" border="1">
<tr>
<td width="200"><?php echo $row_prueba['nombre'];?></td>
<td width="200"><?php echo $row_prueba['descripcion'];?></td>
<td width="108"><?php echo $row_prueba['mes'];?></td>
<td width="101"><?php echo $row_prueba['fecha'];?></td>
<td width="149"><?php echo $row_prueba['costo'];?></td>
<td width="7">&nbsp;</td>
<td width="9">&nbsp;</td>
</tr>
</table>
<?php } while ($row_prueba = mysql_fetch_assoc($prueba));?></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td>total: <?php
$x_costo = $tot_x_costo;
if ($totalRows_prueba > 0) {?>
<? echo $x_costo;?>
<?php }?>
</td>
</tr>
<tr>
<td><? echo $xx;?>&nbsp;</td>
</tr>
</table>
<p>&nbsp;</p>

</body>
</html>
<?php
mysql_free_result($prueba);
?>

any idea on how i can solve this?

ergophobe

5:10 pm on Jul 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That's a lot of code to read through. Perhaps you can explain a little more simply what you want to do. Without getting too far in and checking out the code, can I suggest that you use the SUM and GROUP BY functionality of SQL to get the answer straight out of the DB server as in

$query = "SELECT mes, SUM(costo) AS MonthlyCost FROM gastos GROUP BY mes";

That will directly return costs by month.

mikey74

5:16 pm on Jul 1, 2007 (gmt 0)

10+ Year Member



Thanks works good but only shows me the first month how can i tell it for example from a link to show month 2 or 3 for example

mikey74

5:35 pm on Jul 1, 2007 (gmt 0)

10+ Year Member



i figured out thanks man for the replay i did this to filter by url

$abx_abc = "-1";
if (isset($_GET['mes'])) {
$abx_abc = $_GET['mes'];
}
mysql_select_db($database_condominio, $condominio);
$query_abc = sprintf("SELECT SUM(costo)AS MonthlyCost, mes FROM gastos WHERE gastos.mes = %s GROUP BY mes", GetSQLValueString($abx_abc, "int"));
$abc = mysql_query($query_abc, $condominio) or die(mysql_error());
$row_abc = mysql_fetch_assoc($abc);
$totalRows_abc = mysql_num_rows($abc);

ergophobe

6:09 pm on Jul 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Well that sure is a lot less code! Glad it's working.