I have a page where there is an input text and a search button. And in another file I have the collection class that stores the necessary methods. When they press the button, I call the class method to look in a matrix and show in a table if there is a movie, but even if I put a word that exists, it does not find it, what's more, I do not think it even looks in the matrix, it just does not enter the loop. Next I put the code: This is the index.php:
<?php
require("coleccion.php");
if(isset($_REQUEST['operacion'])) $operacion=$_REQUEST['operacion'];
if(isset($_REQUEST['palabra'])) $palabra=$_REQUEST['palabra'];
if(!isset($palabra)) $palabra="";
?>
<html>
<head>
<title>Actividad 1</title>
</head>
<body bgcolor="#FF9900">
<hr>
<table border=0 align=center>
<tr>
<td valign=top align="center"><h1><font color="red">COLECCION DE PELICULAS</font></h1></td>
<td valign=bottom rowspan=2>
<table border=0 align=center width=375>
<tr>
<td colspan=2 height=40 align=center><h3><font color="red">Operaciones con la coleccion</font></h3></td>
</tr>
<tr>
<td align=right height=30><font color=red><b>Buscar pelicula </b></font></td>
<td>
<form name="form1" method="post" action="index.php">
<input type="text" name="nombre" size="10" maxlength="50" value="<?php echo $palabra; ?>">
<input type="submit" name="buscar" value="Buscar">
<input type="hidden" name="operacion" value="buscar">
</form>
</td>
</tr>
<tr>
<td colspan=2 align=center>
<form name="form2" method="post" action="index.php">
<input type="submit" name="listado" value="Ver listado completo de peliculas">
<input type="hidden" name="operacion" value="listar">
</form>
</td>
</tr>
<tr>
<td colspan=2 align=center>
<form name="form3" method="post" action="index.php">
<input type="submit" name="listado" value="Ver listado ordenado por titulo">
<input type="hidden" name="operacion" value="listar_ordenado">
</form>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align=center><img src=peliculas.png></td>
</tr>
</table>
<hr>
<?php
$peliculas=new coleccion();
$peliculas->add_pelicula("El gran dictador","Charles Chaplin","Comedia",1940);
$peliculas->add_pelicula("En busca del arca perdida","Steven Spielberg","Aventuras",1981);
$peliculas->add_pelicula("Los pajaros","Alfred Hitchocock","Thriller",1963);
$peliculas->add_pelicula("Pulp Fiction","Quentin Tarantino","Alternativo",1994);
$peliculas->add_pelicula("The Matrix","Andy y Larry Wacowsky","Ciencia ficcion",1999);
$peliculas->add_pelicula("2001: una odisea en el espacio","Stanley Kubrick","Ciencia ficcion",1968);
$peliculas->add_pelicula("Lawrance de Arabia","David Lean","Historica",1962);
if(isset($operacion)){
if($operacion=="listar"){
$total=$peliculas->listar_peliculas(false);
}
if($operacion=="buscar"){
$total=$peliculas->buscar($palabra);
}
if($operacion=="listar_ordenado"){
$total=$peliculas->listar_peliculas(true);
}
echo "<center><font color=white>El nº de peliculas encontradas es: ".$total."</font></center>";
}
?>
</body>
</html>
And this is the collection.php class:
<?php
class coleccion{
public $peliculas=array();
public $total;
function __construct(){
$this->peliculas=array();
}
function add_pelicula($titulo,$director,$genero,$año){
$pelicula=array("titulo"=>$titulo,"director"=>$director,"genero"=>$genero,"año"=>$año);
array_push($this->peliculas,$pelicula);
}
function num_peliculas(){
return sizeof($this->peliculas);
}
function listar_peliculas($ordenado){
if($ordenado) sort($this->peliculas);
echo "<table border=1 align=center width=500><tr>
<td><font color=lime><b>Titulo</b></font></td>
<td><font color=lime><b>Director</b></font></td>
<td><font color=lime><b>Genero</b></font></td>
<td><font color=lime><b>Año</b></font></td></tr>";
for($i=0;$i<sizeof($this->peliculas);$i++){
echo "<tr><td><font color=white>".$this->peliculas[$i]["titulo"]."</font></td>
<td><font color=white>".$this->peliculas[$i]["director"]."</font></td>
<td><font color=white>".$this->peliculas[$i]["genero"]."</font></td>
<td><font color=white>".$this->peliculas[$i]["año"]."</font></td></tr>";
}
echo "</table>";
return $this->num_peliculas();
}
function buscar($palabra){
$total=0;
if(($palabra)==""){
echo "No se ha introducido ninguna palabra";
}else{
echo "<center><font color=white>Las peliculas que contienen '<b>$palabra</b>' en el campo 'titulo' o 'director' son: </font></center><p>";
echo "<table border=1 align=center width=500><tr>
<td><font color=lime><b>Titulo</b></font></td>
<td><font color=lime><b>Director</b></font></td>
<td><font color=lime><b>Genero</b></font></td>
<td><font color=lime><B>Año</b></font></td></tr>";
for($i=0;$i<sizeof($this->peliculas);$i++){
if((strpos($this->peliculas[$i]["titulo"],$palabra)!==false)||(strpos($this->peliculas[$i]["director"],$palabra)!==false)||(strpos($this->peliculas[$i]["genero"],$palabra)!==false)||(strpos($this->peliculas[$i]["año"],$palabra)!==false)){
$total++;
echo "<tr><td><font color=white>".$this->peliculas[$i]["titulo"]."</font></td>
<td><font color=white>".$this->peliculas[$i]["director"]."</font></td>
<td><font color=white>".$this->peliculas[$i]["genero"]."</font></td>
<td><font color=white>".$this->peliculas[$i]["año"]."</font></td></tr>";
}
}
echo "</table>";
return $total;
}
}
}
?>