PHP - sumar una colunma a partir de una busqueda PHP y mysql

 
Vista:

sumar una colunma a partir de una busqueda PHP y mysql

Publicado por paula (2 intervenciones) el 17/08/2012 15:03:42
Hola! soy nueva en esto, hace unos meses que estoy estudiando, No logro encontrar la respuesta en los foros, aunque es algo que obviamente ya esta resuelto.

Estoy intentando hacer una aplicacion de control de gastos de una familia.
Tengo una tabla con los datos: id fecha categoria nombre concepto cantidad

Hice que mediante un campo de texto, ingreso una fecha y eso me devuelve una pagina con los resultados. Lo que quisiera es que la columna "cantidad" apareciera sumada, de acuerdo al criterio de busqueda "fecha".
Logro sumar toda la columna "cantidad" pero solo quiero que sume la de la pagina de resultados.
Paso lo que tengo hecho:



<?php virtual('/Connections/gastos.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$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;
}
}

$maxRows_Recordset1 = 10;
$pageNum_Recordset1 = 0;
if (isset($_GET['pageNum_Recordset1'])) {
$pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;

mysql_select_db($database_gastos, $gastos);
$query_Recordset1 = "SELECT * FROM MOV";
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $gastos) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

if (isset($_GET['totalRows_Recordset1'])) {
$totalRows_Recordset1 = $_GET['totalRows_Recordset1'];
} else {
$all_Recordset1 = mysql_query($query_Recordset1);
$totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
}
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;$maxRows_Recordset1 = 10;
$pageNum_Recordset1 = 0;
if (isset($_GET['pageNum_Recordset1'])) {
$pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;

$colname_Recordset1 = "-1";
if (isset($_POST['fecha'])) {
$colname_Recordset1 = $_POST['fecha'];
}
mysql_select_db($database_gastos, $gastos);
$query_Recordset1 = sprintf("SELECT * FROM MOV WHERE fecha = %s", GetSQLValueString($colname_Recordset1, "text"));
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $gastos) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

if (isset($_GET['totalRows_Recordset1'])) {
$totalRows_Recordset1 = $_GET['totalRows_Recordset1'];
} else {
$all_Recordset1 = mysql_query($query_Recordset1);
$totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
}
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Documento sin título</title>
</head>

<body><table width="1446" height="89" border="0">
<tr>
<td width="718" height="85" bgcolor="#CCCCCC"><h1>Buscar por fechas</h1>
<p>&nbsp;</p></td>

</tr>
</table>


<blockquote>
<form action="" method="post" name="buscar" id="buscar">
<table width="1075" border="0">
<tr>
<td width="1069" align="center" valign="middle">Fecha:
<input type="text" name="fecha" id="fecha">
<input type="submit" name="enviar" id="enviar" value="buscar"></td>
</tr>
</table>
<table width="1075" border="0">
<tr> </tr>
</table>
</form><p>&nbsp;</p>
<p>&nbsp;</p>
<table border="2">
<tr>
<td><h2>FECHA</h2></td>
<td><h2><strong>NOMBRE</strong></h2></td>
<td><h2><strong>CATEGORIA</strong></h2></td>
<td><h2><strong>CONCEPTO</strong></h2></td>
<td><h2><strong>IMPORTE</strong></h2></td>
<td><h2><strong>INGRESO/EGRESO</strong></h2></td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_Recordset1['fecha']; ?></td>
<td><?php echo $row_Recordset1['nombre']; ?></td>
<td><?php echo $row_Recordset1['nombre_cat']; ?></td>
<td><?php echo $row_Recordset1['concepto']; ?></td>
<td><?php echo $row_Recordset1['cantidad']; ?></td>
<td><?php echo $row_Recordset1['entrada']; ?></td>
</tr>

<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>
<table width="626" height="36" border="1">
<tr align="center" valign="middle">
<th width="436" align="right" valign="bottom">total</th>
<th width="174" height="28" align="right" valign="bottom">
</tr>
</table>
<p>&nbsp;</p><?php

$query=mysql_query("select * from MOV" );
$suma=0;
while($sumar=mysql_fetch_array($query))
{
$suma=$suma+$sumar["cantidad"];
}
echo $suma;?>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder