Ver Mensaje Individual
  #1 (permalink)  
Antiguo 29/06/2014, 16:27
Jezukaz
 
Fecha de Ingreso: agosto-2013
Mensajes: 41
Antigüedad: 10 años, 8 meses
Puntos: 0
Sumar datos de un mismo campo

Hola, quiero saber como podría calcular el promedio de un campo de una BD.

El problema consiste en que tengo 1 campo que tiene 3 valores, y debo sumar esos 3 valores y dividirlos entre los valores que tengo en ese campo, osea entre 3.

Aca les dejo mi código:

Página principal:

Código:
<html>
<head>
<?php
include('main.inc.php');
$contenido="";
if($statusConexion==true){
	$contenido=consultaUsuarios($conex);	
}?>

<meta charset="utf-8">
<!-- linkear css-->
<link rel="stylesheet" href="bootstrap/bootstrap/css/bootstrap.min.css" />
<link rel="stylesheet" href="jquery-ui-1.10.3.custom/jquery-ui-1.10.3.custom/css/no-theme/jquery-ui-1.10.3.custom.min.css"/>
<link rel="stylesheet" href="mystyle.css"/>

<!--linkear JAVASCRIPT-->
<script type="text/javascript" src="jquery-1.10.2.js"></script>
<script type="text/javascript" src="myjavascript.js"></script>
<script type="text/javascript" src="jquery-ui-1.10.3.custom/jquery-ui-1.10.3.custom/js/jquery-ui-1.10.3.custom.min.js"></script>
<title></title>
</head>


<body>
	<div>
    	<header>
        	<h2>IMPLEMENTACION DE PROCESOS DE CPU</h2>
        </header>
        <div id="div-frm">
                <input type="text" name="opcion" id="opcion" required style="visibility:hidden;margin-top: -50px"/>
        	<form name="frm_user" id="frm_user" action="" method="post">
            <fieldset>
            	<label for="id_user">Proceso</label>
                <input type="text" name="id_user" id="id_user" placeholder="ID" required />
                <label for="id_nombre">Rafaga</label>
                <input type="text" name="id_nombre" id="id_nombre" placeholder="Nombre" required />
                <label for="edad_user">Llegada</label>
                <input type="text" name="edad_user" id="edad_user" placeholder="Edad" required />
                <label for="telefono_user">Prioridad</label>
                <input type="text" name="telefono_user" id="telefono_user" placeholder="Telefono" required />
                
                </fieldset>
                <fieldset>
                <input type="submit" id="enviar" value="Continuar" class="btn btn-primary"/>
                </fieldset>
                <fieldset id="loader" >
                <span>Espere un Momento</span>
                	<img src="images/loader.gif">
                </fieldset>
            </form>
        </div>
 
        <div>
        	<button id="agregar" name="agregar" class="btn btn-inverse btn-small">Agregar Proceso</button>
        </div>
        <section>
            <table class="table table-bordered table-condensed table-hover">
            	<thead>   
                	<tr>
                    	<th>Proceso</th>
                        <th>Rafaga</th>
                        <th>Llegada</th>
                        <th>Prioridad</th>                    
                        <th></th>
                    </tr>
                </thead>
                <tbody id="listausuarios">
                	<?php echo $contenido ?>
                </tbody>
            </table>
        </section>
            <section><h2>Algoritmos de Planificacion</h2>
            	
                <button id="agregar" name="agregar" class="btn btn-inverse btn-small">FCFS</button>                
                <button id="agregar" name="agregar" class="btn btn-inverse btn-small">SJF</button>
                <button id="agregar" name="agregar" class="btn btn-inverse btn-small">PRIORIDAD</button>
                <button id="agregar" name="agregar" class="btn btn-inverse btn-small">ROUND ROBIN</button>
            
            </section>
    </div>
    
</body>

</html>
REGISTRO EN LA BD

Código:
<?php
include("main.inc.php");
sleep(3);
if($statusConexion){
	$respuesta="DONE";
	$mensaje="";
	$ContenidoHTML="";
	if($_POST!="" && !empty($_POST)){
		switch($_POST['Op']){
			case "nuevo":                       
			$consulta=mysql_query("insert into user values(".$_POST['id_user'].",".$_POST['id_nombre'].",".$_POST['edad_user'].",".$_POST['telefono_user'].")",$conex);
			if(mysql_affected_rows()>0){
				$mensaje="Registro Insertado";
				$ContenidoHTML=consultaUsuarios($conex);	
			}
			else{
				$respuesta="BAD";
				$mensaje="Error al realizar la insercion del registro";
			}
			break;
		}
	}
	else{
		$respuesta="BAD";
		$mensaje="Error en parametros";
	}
}
$salidaJSON=array("respuesta" => $respuesta,"mensaje" => $mensaje,"contenido" => $ContenidoHTML);
echo json_encode($salidaJSON);
?>
ACA LISTO LO QUE TENGO EN LA BD

Código:
<?php
$statusConexion=true;
function consultaUsuarios($conexion){
	$salida='';
	$consulta=mysql_query('select id,nombre,edad,telefono from user');
	 if(mysql_num_rows($consulta)>0){
		 while($dato=mysql_fetch_array($consulta)){
			 $salida.='
			 	<tr>
					<td>'.$dato["id"].'</td>
					<td>'.$dato["nombre"].'</td>
					<td>'.$dato["edad"].'</td>
					<td>'.$dato["telefono"].'</td>
				</tr>                                
			 ';
                         }
	 }
	 else
	 {
		 $salida='
		 	<tr id="sinDatos">
				<td colspan="7">No hay Registros en la Base de Datos, Tu codigo!!</td>
			</tr>
		 ';
	 }
	 return $salida;
}

if(!$conex=mysql_connect('localhost','root','')){
	$statusConexion=false;
}
if(!mysql_select_db('ajax',$conex)){
	$statusConexion=false;
}
else{
	mysql_query("set names 'utf-8'",$conex);
}

?>
Espero me puedan ayudar. GRACIAS.