lunes, 1 de marzo de 2010

Ingresar 3 números y que se ordene automáticamente de menor a mayor


<!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>Ejercicio C</title>
<!--SCRIPT PARA VALIDAR FORMULARIO-->
   <script language="javascript" type="text/javascript">
function validar(formu)
{

if(formu.n1.value==""){
alert("Ingrese Nº 1...");
formu.n1.focus();
return false;
}
if(formu.n2.value==""){
alert("Ingrese Nº 2...");
formu.n2.focus();
return false;
}
if(formu.n3.value==""){
alert("Ingrese Nº 3...");
formu.n3.focus();
return false;
}

alert("Datos llenados correctamente....");
return true;
}

</script>
<!--FIN DEL SCRIPT -->
</head>
<body>
<table align="center">
<form method="post" onSubmit="return validar(this);">
<tr>
<td>Ingrese Nº 1</td>
<td><input type="text" size="5" name="n1"/></td></tr>
<tr>
<td>Ingrese Nº 2</td>
<td><input type="text" size="5" name="n2"/></td></tr>

<tr>
<td>Ingrese Nº 3</td>
<td><input type="text" size="5" name="n3"/></td>
</tr>

<tr>
<td colspan="2" align="right"><input type="submit" value="Enviar" /></td>
</tr>
</form>
</table>
<?php

//ordenar manteniendo los índices, Reverso
$num = array($_POST["n1"],$_POST["n2"],$_POST["n3"]);
echo "Números ingresados:"."<br>";
echo" ";
echo $_POST['n1'].",".$_POST['n2'].",".$_POST['n3']."<br>";

echo "Números ordenados de menor a mayor:"."<br>";

rsort($num); // ordena los número de mayor a menor

$numx=array_reverse($num,true);//invierte el orden de menor a mayor

//recorro el array con foreach

foreach ($numx as $key => $val) {

//    echo $key ." = " . $val . "<br>";

//esto es solo para dar espacio

echo" ";

// fin del espacio



//imprimo el los número de manera ordenada

echo $val."<br>";

}

?>

<a href="index.php" target="_parent"><p align="center">Regresar...</p></a>

</body>

</html>

No hay comentarios:

Publicar un comentario