Mes: marzo 2017
Examen
https://twitter.com/Frikisite/status/846301105780899840
Encuesta Escape room
Excursión eShow
Manejar ficheros
Recorrer carpetas en php
$archivos = scandir(".");
print_r($archivos);
$gestor_dir = opendir(".");
while ($nombre_fichero = readdir($gestor_dir)) {
$ficheros[] = $nombre_fichero;
}
print_r($ficheros);
recorrerCarpeta(".");
function recorrerCarpeta($carpeta) {
echo "Contenido de: " . $carpeta . "<br/>";
$archivos = scandir($carpeta);
array_shift($archivos);
array_shift($archivos);
foreach ($archivos as $a){
if (is_dir($a)){
recorrerCarpeta($carpeta."/".$a);
}
else{
echo $carpeta."/".$a."<br/>";
}
}
}
Funciones de archivo
Escribir en un fichero:
if (!$f = @fopen("pepe.txt", "w")) {
die("No he podido abrir el fichero: " . error_get_last()['message']);
}
for ($i = 1; $i <= 10; $i++) {
fwrite($f, "$i x 5=".(5*$i)."\r\n");
}
fclose($f);
if (!$f = @fopen("pepe.txt", "a")) {
die("No he podido abrir el fichero: " . error_get_last()['message']);
}
for ($i = 1; $i <= 10; $i++) {
fwrite($f, "$i x 7=".(7*$i)."\r\n");
}
fclose($f);
Leer un fichero:
$f = @fopen("pepe.txt", "r") or die("No he podido abrir el fichero");
//Lee una línea
while (!feof($f)) {
echo "<h2>".fgets($f)."</h2>";
}
fclose($f);
Leer directamente un fichero:
$cadena= file_get_contents("pepe.txt");
$tabla=file("pepe.txt");
echo $cadena;
print_r($tabla);
Ventajas de la vagancia
Ejercicio palabras mal escritas
<style>
.celdako,.celdaok{
cursor: pointer;
width:100px;
height:50px;
text-align: center;
}
.celdaok:active {
background-color:lightgreen;
}
.celdako:active {
background-color: red;
}
</style>
<?php
function muestraTabla($tabla) {
echo "<pre>";
print_r($tabla);
echo "</pre>";
}
$palabras = array(
array('length', array('lenght', 'legnth', 'langth', 'lentgh', 'lenhgt')),
array('locomotion', array('lomocotion', 'lotocomion', 'molocotion', 'locamotion', 'locomocion')),
array('bedroom', array('vedroom', 'bedrum', 'berdoom', 'bedrun', 'bedroon', 'beddroom')),
array('coin', array('coyn', 'cony', 'cain', 'con')),
array('stable', array('estable', 'stoble', 'stabel', 'tsable')),
array('crush', array('cruhs', 'clush', 'cruss', 'crusah')),
array('fantasy', array('fantasi', 'fantascy', 'fatnasy', 'frantasy')),
array('pillow', array('pellow', 'pillou', 'pilluw', 'pilow', 'pilou')),
);
function array_to_table($tabla, $buena,$ancho=2) {
$res = "<table border=1>";
for ($i = 0; $i < count($tabla); $i++) {
if ($i % $ancho == 0) {
$res .= "<tr>";
}
$res .= '<td class="' . ($tabla[$i] == $buena ? 'celdaok' : 'celdako') . '">' . $tabla[$i] . "</td>";
if ($i % $ancho == $ancho-1) {
$res .= "</tr>";
}
}
$res .= "</table>";
return $res;
}
//Seleccionar una palabra al azar rand (0,count($palabras)-1)
$palabra = $palabras[rand(0, count($palabras) - 1)];
// muestraTabla($palabra);
//Coger la palabra buena y seleccionar tres palabras malas al azar
$tablero[] = $palabra[0];
// $malos = $palabra[1];
//shuffle($malos);
//$malos=array_slice($malos, 0, 8);
$tablero = array_merge($tablero, $palabra[1]);
while (count($tablero)<9){
$a=$palabras[array_rand($palabras)];
$palazar=$a[1][array_rand($a[1])];
if (!in_array($palazar, $tablero)){
$tablero[]=$palazar;
}
}
// muestraTabla($tablero);
//Mezclar
shuffle($tablero);
//Y mostrar
echo array_to_table($tablero, $palabra[0],3);
?>
Ejemplo validación
<form > <div class="form-group"> <label for="s1">Sumando 1:</label> <input type="text" class="form-control" name="s1" > </div> <div class="form-group"> <label for="s2">Sumando 2:</label> <input type="number" class="form-control" name="s2" required=""> </div> <input type="submit" value="Manda los datos"> </form> <?php $s1= filter_input(INPUT_GET,'s1',FILTER_VALIDATE_INT); $s2= filter_input(INPUT_GET,'s2',FILTER_VALIDATE_INT); echo "La suma es: ".($s1+$s2);






