Foros del Web » Programando para Internet » PHP »

Calcular Promedio y Redondear PHP

Estas en el tema de Calcular Promedio y Redondear PHP en el foro de PHP en Foros del Web. Hola buenas tardes, me estoy iniciando en la programación y requiero del apoyo de los conocedores. Tengo una tabla y quisiera hacer dos cosas: Sacar ...
  #1 (permalink)  
Antiguo 08/09/2015, 21:41
 
Fecha de Ingreso: septiembre-2015
Ubicación: Porlamar
Mensajes: 3
Antigüedad: 8 años, 7 meses
Puntos: 0
Calcular Promedio y Redondear PHP

Hola buenas tardes, me estoy iniciando en la programación y requiero del apoyo de los conocedores.

Tengo una tabla y quisiera hacer dos cosas:
Sacar el promedio de Cada Mes, es decir promediar las columnas.
Redondear el resultado a un numero entero



Envío codigo para ver si me pueden ayudar:

<?php require_once('../Connections/local.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;
}
}

$currentPage = $_SERVER["PHP_SELF"];

$maxRows_cant_cursos_andes = 10;
$pageNum_cant_cursos_andes = 0;
if (isset($_GET['pageNum_cant_cursos_andes'])) {
$pageNum_cant_cursos_andes = $_GET['pageNum_cant_cursos_andes'];
}
$startRow_cant_cursos_andes = $pageNum_cant_cursos_andes * $maxRows_cant_cursos_andes;

mysql_select_db($database_local, $local);
$query_cant_cursos_andes = "SELECT * FROM `cantidad de cursos region andes`";
$query_limit_cant_cursos_andes = sprintf("%s LIMIT %d, %d", $query_cant_cursos_andes, $startRow_cant_cursos_andes, $maxRows_cant_cursos_andes);
$cant_cursos_andes = mysql_query($query_limit_cant_cursos_andes, $local) or die(mysql_error());
$row_cant_cursos_andes = mysql_fetch_assoc($cant_cursos_andes);

if (isset($_GET['totalRows_cant_cursos_andes'])) {
$totalRows_cant_cursos_andes = $_GET['totalRows_cant_cursos_andes'];
} else {
$all_cant_cursos_andes = mysql_query($query_cant_cursos_andes);
$totalRows_cant_cursos_andes = mysql_num_rows($all_cant_cursos_andes);
}
$totalPages_cant_cursos_andes = ceil($totalRows_cant_cursos_andes/$maxRows_cant_cursos_andes)-1;

$queryString_cant_cursos_andes = "";
if (!empty($_SERVER['QUERY_STRING'])) {
$params = explode("&", $_SERVER['QUERY_STRING']);
$newParams = array();
foreach ($params as $param) {
if (stristr($param, "pageNum_cant_cursos_andes") == false &&
stristr($param, "totalRows_cant_cursos_andes") == false) {
array_push($newParams, $param);
}
}
if (count($newParams) != 0) {
$queryString_cant_cursos_andes = "&" . htmlentities(implode("&", $newParams));
}
}
$queryString_cant_cursos_andes = sprintf("&totalRows_cant_cursos_andes=%d%s", $totalRows_cant_cursos_andes, $queryString_cant_cursos_andes);

session_start();
require("conexion.php");
$usu=$_SESSION['usuario'];
$consulta11=mysql_query("SELECT * FROM usuario where usuario='$usu' ") or die(mysql_error());
$pacientes=mysql_fetch_array($consulta11);
$usu=$pacientes['nombre']." ".$pacientes['apellido'];
?>


<!-- -------------------------------Parametros del fondo----------------------------------------- -->
<body>



<?php do { ?>
<tr>
<td bgcolor="#E0E0E0"><strong><?php echo $row_cant_cursos_andes['Region']; ?>&nbsp; </a></strong></td>
<td><?php echo $row_cant_cursos_andes['year']; ?>&nbsp; </td>
<td><?php echo $row_cant_cursos_andes['Enero']; ?>&nbsp; </td>
<td><?php echo $row_cant_cursos_andes['Febrero']; ?>&nbsp; </td>
<td><?php echo $row_cant_cursos_andes['Marzo']; ?>&nbsp; </td>
<td><?php echo $row_cant_cursos_andes['Abril']; ?>&nbsp; </td>
<td><?php echo $row_cant_cursos_andes['Mayo']; ?>&nbsp; </td>
<td><?php echo $row_cant_cursos_andes['Junio']; ?>&nbsp; </td>
<td><?php echo $row_cant_cursos_andes['Julio']; ?>&nbsp; </td>
<td><?php echo $row_cant_cursos_andes['Agosto']; ?>&nbsp; </td>
<td><?php echo $row_cant_cursos_andes['Septiembre']; ?>&nbsp; </td>
<td><?php echo $row_cant_cursos_andes['Octubre']; ?>&nbsp; </td>
<td><?php echo $row_cant_cursos_andes['Noviembre']; ?>&nbsp; </td>
<td><?php echo $row_cant_cursos_andes['Diciembre']; ?>&nbsp; </td>
<td><strong> <?php echo $suma=$row_cant_cursos_andes['Enero']+ $row_cant_cursos_andes['Febrero']+ $row_cant_cursos_andes['Marzo']+ $row_cant_cursos_andes['Abril']+ $row_cant_cursos_andes['Mayo']+ $row_cant_cursos_andes['Junio']+ $row_cant_cursos_andes['Julio']+ $row_cant_cursos_andes['Agosto']+ $row_cant_cursos_andes['Septiembre']+ $row_cant_cursos_andes['Octubre']+ $row_cant_cursos_andes['Noviembre']+ $row_cant_cursos_andes['Diciembre'] ?>&nbsp; </strong></td>
</tr>


<?php } while ($row_cant_cursos_andes = mysql_fetch_assoc($cant_cursos_andes)); ?>

</table>


<br>
<table border="0">
<tr>
<td><?php if ($pageNum_cant_cursos_andes > 0) { // Show if not first page ?>
<a href="<?php printf("%s?pageNum_cant_cursos_andes=%d%s", $currentPage, 0, $queryString_cant_cursos_andes); ?>">First</a>
<?php } // Show if not first page ?></td>
<td><?php if ($pageNum_cant_cursos_andes > 0) { // Show if not first page ?>
<a href="<?php printf("%s?pageNum_cant_cursos_andes=%d%s", $currentPage, max(0, $pageNum_cant_cursos_andes - 1), $queryString_cant_cursos_andes); ?>">Previous</a>
<?php } // Show if not first page ?></td>
<td><?php if ($pageNum_cant_cursos_andes < $totalPages_cant_cursos_andes) { // Show if not last page ?>
<a href="<?php printf("%s?pageNum_cant_cursos_andes=%d%s", $currentPage, min($totalPages_cant_cursos_andes, $pageNum_cant_cursos_andes + 1), $queryString_cant_cursos_andes); ?>">Next</a>
<?php } // Show if not last page ?></td>
<td><?php if ($pageNum_cant_cursos_andes < $totalPages_cant_cursos_andes) { // Show if not last page ?>
<a href="<?php printf("%s?pageNum_cant_cursos_andes=%d%s", $currentPage, $totalPages_cant_cursos_andes, $queryString_cant_cursos_andes); ?>">Last</a>
<?php } // Show if not last page ?></td>
</tr>
</table>


Mostrando: <?php echo ($startRow_cant_cursos_andes + 1) ?> a <?php echo min($startRow_cant_cursos_andes + $maxRows_cant_cursos_andes, $totalRows_cant_cursos_andes) ?> años de: <?php echo $totalRows_cant_cursos_andes ?> años



<p align="center">Gerencia de Capacitación ®</p>

</body>
</html>
<?php
mysql_free_result($cant_cursos_andes);
?>
  #2 (permalink)  
Antiguo 08/09/2015, 22:12
 
Fecha de Ingreso: septiembre-2015
Ubicación: Porlamar
Mensajes: 3
Antigüedad: 8 años, 7 meses
Puntos: 0
Respuesta: Calcular Promedio y Redondear PHP

Hola, ya resolví el redondeo por años,

Ahora quisiera redondear por meses, es decir:

Enero 2012
Enero 2013
Enero 2014
Enero 2015

.... y seguir hasta dic.

Lo que no se es como sumar y luego promediar las columnas,

Les envio imagen y la parte del codigo nuevamente

Imagen:



Código:

<?php require_once('../Connections/local.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;
}
}

$currentPage = $_SERVER["PHP_SELF"];

$maxRows_cant_cursos_andes = 10;
$pageNum_cant_cursos_andes = 0;
if (isset($_GET['pageNum_cant_cursos_andes'])) {
$pageNum_cant_cursos_andes = $_GET['pageNum_cant_cursos_andes'];
}
$startRow_cant_cursos_andes = $pageNum_cant_cursos_andes * $maxRows_cant_cursos_andes;

mysql_select_db($database_local, $local);
$query_cant_cursos_andes = "SELECT * FROM `cantidad de cursos region andes`";
$query_limit_cant_cursos_andes = sprintf("%s LIMIT %d, %d", $query_cant_cursos_andes, $startRow_cant_cursos_andes, $maxRows_cant_cursos_andes);
$cant_cursos_andes = mysql_query($query_limit_cant_cursos_andes, $local) or die(mysql_error());
$row_cant_cursos_andes = mysql_fetch_assoc($cant_cursos_andes);

if (isset($_GET['totalRows_cant_cursos_andes'])) {
$totalRows_cant_cursos_andes = $_GET['totalRows_cant_cursos_andes'];
} else {
$all_cant_cursos_andes = mysql_query($query_cant_cursos_andes);
$totalRows_cant_cursos_andes = mysql_num_rows($all_cant_cursos_andes);
}
$totalPages_cant_cursos_andes = ceil($totalRows_cant_cursos_andes/$maxRows_cant_cursos_andes)-1;

$queryString_cant_cursos_andes = "";
if (!empty($_SERVER['QUERY_STRING'])) {
$params = explode("&", $_SERVER['QUERY_STRING']);
$newParams = array();
foreach ($params as $param) {
if (stristr($param, "pageNum_cant_cursos_andes") == false &&
stristr($param, "totalRows_cant_cursos_andes") == false) {
array_push($newParams, $param);
}
}
if (count($newParams) != 0) {
$queryString_cant_cursos_andes = "&" . htmlentities(implode("&", $newParams));
}
}
$queryString_cant_cursos_andes = sprintf("&totalRows_cant_cursos_andes=%d%s", $totalRows_cant_cursos_andes, $queryString_cant_cursos_andes);

session_start();
require("conexion.php");
$usu=$_SESSION['usuario'];
$consulta11=mysql_query("SELECT * FROM usuario where usuario='$usu' ") or die(mysql_error());
$pacientes=mysql_fetch_array($consulta11);
$usu=$pacientes['nombre']." ".$pacientes['apellido'];
?>


<!-- -------------------------------Parametros del fondo----------------------------------------- -->
<body>





<table border="1" align="center" bgcolor="#CBCBCB" width="1280">
<tr bgcolor="#E0E0E0">
<td><strong>Región</strong></td>
<td><strong>Año</strong></td>
<td><strong>Enero</strong></td>
<td><strong>Febrero</strong></td>
<td><strong>Marzo</strong></td>
<td><strong>Abril</strong></td>
<td><strong>Mayo</strong></td>
<td><strong>Junio</strong></td>
<td><strong>Julio</strong></td>
<td><strong>Agosto</strong></td>
<td><strong>Septiembre</strong></td>
<td><strong>Octubre</strong></td>
<td><strong>Noviembre</strong></td>
<td><strong>Diciembre</strong></td>
<td><strong>Total</strong></td>
<td><strong>Promedio</strong></td>
</tr>


<?php do { ?>
<tr>
<td bgcolor="#E0E0E0"><strong><?php echo $row_cant_cursos_andes['Region']; ?>&nbsp; </a></strong></td>
<td><?php echo $row_cant_cursos_andes['year']; ?>&nbsp; </td>
<td><?php echo $row_cant_cursos_andes['Enero']; ?>&nbsp; </td>
<td><?php echo $row_cant_cursos_andes['Febrero']; ?>&nbsp; </td>
<td><?php echo $row_cant_cursos_andes['Marzo']; ?>&nbsp; </td>
<td><?php echo $row_cant_cursos_andes['Abril']; ?>&nbsp; </td>
<td><?php echo $row_cant_cursos_andes['Mayo']; ?>&nbsp; </td>
<td><?php echo $row_cant_cursos_andes['Junio']; ?>&nbsp; </td>
<td><?php echo $row_cant_cursos_andes['Julio']; ?>&nbsp; </td>
<td><?php echo $row_cant_cursos_andes['Agosto']; ?>&nbsp; </td>
<td><?php echo $row_cant_cursos_andes['Septiembre']; ?>&nbsp; </td>
<td><?php echo $row_cant_cursos_andes['Octubre']; ?>&nbsp; </td>
<td><?php echo $row_cant_cursos_andes['Noviembre']; ?>&nbsp; </td>
<td><?php echo $row_cant_cursos_andes['Diciembre']; ?>&nbsp; </td>
<td><strong> <?php echo $suma=$row_cant_cursos_andes['Enero']+ $row_cant_cursos_andes['Febrero']+ $row_cant_cursos_andes['Marzo']+ $row_cant_cursos_andes['Abril']+ $row_cant_cursos_andes['Mayo']+ $row_cant_cursos_andes['Junio']+ $row_cant_cursos_andes['Julio']+ $row_cant_cursos_andes['Agosto']+ $row_cant_cursos_andes['Septiembre']+ $row_cant_cursos_andes['Octubre']+ $row_cant_cursos_andes['Noviembre']+ $row_cant_cursos_andes['Diciembre'] ?>&nbsp; </strong></td>
<td><strong> <?php $prom=($row_cant_cursos_andes['Enero']+ $row_cant_cursos_andes['Febrero']+ $row_cant_cursos_andes['Marzo']+ $row_cant_cursos_andes['Abril']+ $row_cant_cursos_andes['Mayo']+ $row_cant_cursos_andes['Junio']+ $row_cant_cursos_andes['Julio']+ $row_cant_cursos_andes['Agosto']+ $row_cant_cursos_andes['Septiembre']+ $row_cant_cursos_andes['Octubre']+ $row_cant_cursos_andes['Noviembre']+ $row_cant_cursos_andes['Diciembre'])/12; echo $prom= round($prom, 1) ?>&nbsp; </strong></td>
</tr>


<?php } while ($row_cant_cursos_andes = mysql_fetch_assoc($cant_cursos_andes)); ?>

</table>




<br>
<table border="0">
<tr>
<td><?php if ($pageNum_cant_cursos_andes > 0) { // Show if not first page ?>
<a href="<?php printf("%s?pageNum_cant_cursos_andes=%d%s", $currentPage, 0, $queryString_cant_cursos_andes); ?>">First</a>
<?php } // Show if not first page ?></td>
<td><?php if ($pageNum_cant_cursos_andes > 0) { // Show if not first page ?>
<a href="<?php printf("%s?pageNum_cant_cursos_andes=%d%s", $currentPage, max(0, $pageNum_cant_cursos_andes - 1), $queryString_cant_cursos_andes); ?>">Previous</a>
<?php } // Show if not first page ?></td>
<td><?php if ($pageNum_cant_cursos_andes < $totalPages_cant_cursos_andes) { // Show if not last page ?>
<a href="<?php printf("%s?pageNum_cant_cursos_andes=%d%s", $currentPage, min($totalPages_cant_cursos_andes, $pageNum_cant_cursos_andes + 1), $queryString_cant_cursos_andes); ?>">Next</a>
<?php } // Show if not last page ?></td>
<td><?php if ($pageNum_cant_cursos_andes < $totalPages_cant_cursos_andes) { // Show if not last page ?>
<a href="<?php printf("%s?pageNum_cant_cursos_andes=%d%s", $currentPage, $totalPages_cant_cursos_andes, $queryString_cant_cursos_andes); ?>">Last</a>
<?php } // Show if not last page ?></td>
</tr>
</table>


Mostrando: <?php echo ($startRow_cant_cursos_andes + 1) ?> a <?php echo min($startRow_cant_cursos_andes + $maxRows_cant_cursos_andes, $totalRows_cant_cursos_andes) ?> años de: <?php echo $totalRows_cant_cursos_andes ?> años



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

Etiquetas: calcular, html, mysql, promedio, redondear, select, sql, tabla
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 20:32.