* @copyright 2010 Bartolomé Sintes Marco * @license http://www.gnu.org/licenses/agpl.txt AGPL 3 or later * @version 2010-03-25 * @link https://www.mclibre.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ // Función de recogida de datos function recoge($key, $type = "") { if (!is_string($key) && !is_int($key) || $key == "") { trigger_error("Function recoge(): Argument #1 (\$key) must be a non-empty string or an integer", E_USER_ERROR); } elseif ($type !== "" && $type !== []) { trigger_error("Function recoge(): Argument #2 (\$type) is optional, but if provided, it must be an empty array or an empty string", E_USER_ERROR); } $tmp = $type; if (isset($_REQUEST[$key])) { if (!is_array($_REQUEST[$key]) && !is_array($type)) { $tmp = trim(htmlspecialchars($_REQUEST[$key])); } elseif (is_array($_REQUEST[$key]) && is_array($type)) { $tmp = $_REQUEST[$key]; array_walk_recursive($tmp, function (&$value) { $value = trim(htmlspecialchars($value)); }); } } return $tmp; } function cabecera($texto) { print "\n"; print "\n"; print "\n"; print " \n"; print " \n"; print " $texto. Validación.\n"; print " Ejercicios. PHP. Bartolomé Sintes Marco. www.mclibre.org\n"; print " \n"; print " \n"; print " \n"; print "\n"; print "\n"; print "\n"; print "

$texto

\n"; print "\n"; } function pie($conEnlace, $conBotones, $numeroValores) { print " \n"; print "\n"; if ($conEnlace) { print "

Volver al principio

\n"; print "\n"; } if ($conBotones) { print "

\n"; print " \n"; print " \n"; print " \n"; print " \n"; print "

\n"; print " \n"; print "\n"; } print " \n"; print "\n"; print "\n"; } // Recoge el número de datos y lo valida, aumenta o reduce define("FORM_METHOD", "get"); define("TAM_VALOR", 5); define("NUM_VALORES_INICIAL", 4); define("NUM_VALORES_MINIMO", 2); define("NUM_VALORES_MAXIMO", 10); define("CON_ENLACE", true); define("SIN_ENLACE", false); define("CON_BOTONES", true); define("SIN_BOTONES", false); $valores = recoge("valores", []); $numeroValores = recoge("numeroValores"); if ($numeroValores < NUM_VALORES_MINIMO) { $numeroValores = NUM_VALORES_MINIMO; } elseif ($numeroValores > NUM_VALORES_MAXIMO) { $numeroValores = NUM_VALORES_MAXIMO; } if (isset($_REQUEST["anyadir"]) && ($numeroValores < NUM_VALORES_MAXIMO)) { $numeroValores++; $valores[$numeroValores] = ""; // Al añdir se crea un nuevo valor vacío } elseif (isset($_REQUEST["quitar"]) && ($numeroValores > NUM_VALORES_MINIMO)) { $numeroValores--; } $valoresOk = []; $valoresTodoOk = true; for ($i = 1; $i <= $numeroValores; $i++) { $valoresOk[$i] = true; if (!isset($valores[$i])) { // Por si falta un valor en la matriz $valoresTodoOk = false; $valores[$i] = ""; } elseif ($valores[$i] == "") { // Por si un valor es vacío $valoresTodoOk = false; } elseif ($valores[$i] != "" && !is_numeric($valores[$i])) { // Por si un valor no es numérico $valoresOk[$i] = false; $valoresTodoOk = false; } } $valoresTodoVacio = true; for ($i = 1; $i <= $numeroValores; $i++) { if ($valores[$i] != "") { $valoresTodoVacio = false; } } if ($valoresTodoOk) { cabecera("Resultado válido"); print"

Los valores introducidos son correctos.

\n"; print "\n"; for ($i = 1; $i <= $numeroValores; $i++) { print "

Valor $i: $valores[$i]

\n"; print "\n"; } pie(CON_ENLACE, SIN_BOTONES, $numeroValores); } elseif (!$valoresTodoVacio && (isset($_REQUEST["enviar"]) || isset($_REQUEST["anyadir"]) || isset($_REQUEST["quitar"]))) { cabecera("Resultado inválido"); print"

Por favor, corrija los datos incorrectos y/o complete todas las casillas:

\n"; print "\n"; print "
\n"; print " \n"; for ($i = 1; $i <= $numeroValores; $i++) { print " \n"; print " \n"; print " \n"; print " \n"; } pie(CON_ENLACE, CON_BOTONES, $numeroValores); } else { cabecera("Formulario"); print"

Escriba $numeroValores números:

\n"; print "\n"; print " \n"; print "
Valor $i:"; if (!$valoresOk[$i]) { print " El valor no es correcto"; } elseif ($valores[$i] == "") { print " Escriba un valor"; } print "
\n"; for ($i = 1; $i <= $numeroValores; $i++) { print " \n"; print " \n"; print " \n"; print " \n"; } pie(SIN_ENLACE, CON_BOTONES, $numeroValores); }
Valor $i:"; print "