Foros del Web » Programando para Internet » PHP »

Validar Porcentajes

Estas en el tema de Validar Porcentajes en el foro de PHP en Foros del Web. Hola a todos, estoy realizando ingresos y modificaciones a unos datos que tienen campos de porcentaje, quisiera saber como puedo validar que la suma de ...
  #1 (permalink)  
Antiguo 16/03/2009, 09:35
 
Fecha de Ingreso: marzo-2009
Mensajes: 24
Antigüedad: 15 años, 1 mes
Puntos: 0
Pregunta Validar Porcentajes

Hola a todos, estoy realizando ingresos y modificaciones a unos datos que tienen campos de porcentaje, quisiera saber como puedo validar que la suma de los porcentajes sea o no 100 y le muestre al usuario un mensaje, ya sea cuando este ingresando los porcentajes y cuando los este modificando.

Este es el codigo para el ingreso de los datos, lo estoy haciendo por medio de un recordset en dreamweaver.

Código 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;
}
}

$editFormAction $_SERVER['PHP_SELF'];
if (isset(
$_SERVER['QUERY_STRING'])) {
  
$editFormAction .= "?" htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset(
$_POST["MM_insert"])) && ($_POST["MM_insert"] == "form3")) {
  
$insertSQL sprintf("INSERT INTO indicador (porcentaje, descripcion, cod_logro) VALUES (%s, %s, %s)",
                       
GetSQLValueString($_POST['porcentaje'], "text"),
                       
GetSQLValueString($_POST['descripcion'], "text"),
                       
GetSQLValueString($_POST['cod_logro'], "int"));

  
mysql_select_db($database_notas$notas);
  
$Result1 mysql_query($insertSQL$notas) or die(mysql_error());
}

if ((isset(
$_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  
$insertSQL sprintf("INSERT INTO indicador (cod_indicador, porcentaje, descripcion, cod_logro) VALUES (%s, %s, %s, %s)",
                       
GetSQLValueString($_POST['cod_indicador'], "int"),
                       
GetSQLValueString($_POST['porcentaje'], "text"),
                       
GetSQLValueString($_POST['descripcion'], "text"),
                       
GetSQLValueString($_POST['cod_logro'], "int"));

  
mysql_select_db($database_notas$notas);
  
$Result1 mysql_query($insertSQL$notas) or die(mysql_error());
  
  echo 
"Registro Insertado Satisfactoriamente <br>";
}

$maxRows_registro9 10;
$pageNum_registro9 0;
if (isset(
$_GET['pageNum_registro9'])) {
  
$pageNum_registro9 $_GET['pageNum_registro9'];
}
$startRow_registro9 $pageNum_registro9 $maxRows_registro9;

mysql_select_db($database_notas$notas);
$query_registro9 "SELECT cod_logro, porcentaje, descripcion FROM logro";
$query_limit_registro9 sprintf("%s LIMIT %d, %d"$query_registro9$startRow_registro9$maxRows_registro9);
$registro9 mysql_query($query_limit_registro9$notas) or die(mysql_error());
$row_registro9 mysql_fetch_assoc($registro9);

if (isset(
$_GET['totalRows_registro9'])) {
  
$totalRows_registro9 $_GET['totalRows_registro9'];
} else {
  
$all_registro9 mysql_query($query_registro9);
  
$totalRows_registro9 mysql_num_rows($all_registro9);
}
$totalPages_registro9 ceil($totalRows_registro9/$maxRows_registro9)-1;

mysql_select_db($database_notas$notas);
$query_Recordset1 "SELECT porcentaje, descripcion, cod_logro FROM indicador";
$Recordset1 mysql_query($query_Recordset1$notas) or die(mysql_error());
$row_Recordset1 mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 mysql_num_rows($Recordset1);
?>

Este es el codigo cuando se va a modificar los registros:

Código 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;
}
}

$editFormAction $_SERVER['PHP_SELF'];
if (isset(
$_SERVER['QUERY_STRING'])) {
  
$editFormAction .= "?" htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset(
$_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  
$updateSQL sprintf("UPDATE competencia SET porcentaje=%s, descripcion=%s, cod_modulo=%s WHERE cod_comp=%s",
                       
GetSQLValueString($_POST['porcentaje'], "text"),
                       
GetSQLValueString($_POST['descripcion'], "text"),
                       
GetSQLValueString($_POST['cod_modulo'], "int"),
                       
GetSQLValueString($_POST['cod_comp'], "int"));

  
mysql_select_db($database_notas$notas);
  
$Result1 mysql_query($updateSQL$notas) or die(mysql_error());
  if(
mysql_affected_rows()>0){
    echo 
"Los Datos De La Competencia Fueron Actualizados <br>";    
 }else{
    echo 
"Los Datos De La Competencia No Fueron Actualizados <br>"
 }
}

$colname_Recordset1 "-1";
if (isset(
$_POST['cod_comp'])) {
  
$colname_Recordset1 $_POST['cod_comp'];
}
mysql_select_db($database_notas$notas);
$query_Recordset1 sprintf("SELECT * FROM competencia WHERE cod_comp = %s"GetSQLValueString($colname_Recordset1"int"));
$Recordset1 mysql_query($query_Recordset1$notas) or die(mysql_error());
$row_Recordset1 mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 mysql_num_rows($Recordset1);

if (
$totalRows_Recordset1==0){
      echo 
"No Hay Datos Registrados Para El Código de Competencia Escogido <br>";
  }else{
      echo 
"Datos Encontrados Satisfactoriamente <br>";
  }
?>
Si alguien sabe como hacerlo y enque parte del codigo colocarlo le estare muy agradecida la ayuda.
  #2 (permalink)  
Antiguo 16/03/2009, 09:50
Avatar de the_web_saint  
Fecha de Ingreso: mayo-2008
Ubicación: localhost/tierra/america/panama
Mensajes: 1.229
Antigüedad: 15 años, 11 meses
Puntos: 43
Respuesta: Validar Porcentajes

Puedes hacer eso con javascript,
Validar la suma de las entradas antes de enviarlo o al ir tipeando.

Saludos
__________________
..::The Saint::..
El pesimista se queja del viento; el optimista espera que cambie; el realista ajusta las velas.
  #3 (permalink)  
Antiguo 16/03/2009, 09:52
 
Fecha de Ingreso: marzo-2009
Mensajes: 24
Antigüedad: 15 años, 1 mes
Puntos: 0
Respuesta: Validar Porcentajes

Sabes como hacerlo en javascript? pues no tengo idea de como pueda ser, pues lo ideal es que se valide al ir escribiendolos, gracias.
  #4 (permalink)  
Antiguo 16/03/2009, 10:01
Avatar de Ronruby  
Fecha de Ingreso: julio-2008
Ubicación: 18°30'N, 69°59'W
Mensajes: 4.879
Antigüedad: 15 años, 9 meses
Puntos: 416
Respuesta: Validar Porcentajes

Entonces si necesitas Javascript, espera a que un moderador mueva tu tema al foro correcto.
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 16:25.