I need some help adding a search bar to search through my .txt file. What I want is a way users can search and compare their search with what is in my moviedata.txt file. I really only want them to search by movie title name, not data or genre. I want it in PHP form, and just based on what they search provide results in an array form. So an array is most likely needed. It might need a delimiter at the end of each line in the txt file to make a search bar able. I will post my html and moviedata.txt info below.
Thanks for the help in advance.
HTML
<DOCTYPE html>
<html>
<head>
<title>Movies</title>
<link rel="stylesheet" href="styles/main.css">
</head>
<body>
<div id="header">Cornell Students Favorite Flick Picks!</div>
<?php
if (!isset($movies)) {
$movies = file("moviedata.txt");
if (!$movies) {
print("Could not load moviedata.txt file\n");
exit;
}
}
$update = false;
for ($i = 0; $i < count($movies); $i++) {
if (isset($_POST["movie$i"])) {
$movie = $movies[$i];
$row = explode("\t",$movie);
$count = trim($row[3]);
$count++;
$row[3] = $count;
$movie = implode("\t",$row)."\n";
$movies[$i] = $movie;
$update = true;
}
}
if (isset($_POST["newmovie"])) {
if (($_POST["moviename"]!= "") && ($_POST["genre"] != "") && ($_POST["date"] != "") && ($_POST["nomovie"] != "")) {
$newmovie = $_POST['moviename']."\t".$_POST['genre']."\t".$_POST['date']."\t".$_POST['nomovie'];
$movies[] = $newmovie."\n";
$update = true;
}
}
if ($update) {
$fp = fopen("moviedata.txt","w");
if (!$fp) {
print("Can't open moviedata.txt for write.\n");
exit;
}
foreach ($movies as $movie) {
fputs($fp, $movie);
}
}
?>
<img src="images/2.jpg" alt="image">
<div id="required"> All Fields are Required</div>
<form method="post" action="index.php">
<table border="1">
<thead>
<th>Movie Title</th>
<th>Genre</th>
<th>Date</th>
<th># of times viewed</th>
<th>Increment</th>
</thead>
<?php
foreach ($movies as $movieindex => $movie) {
print("<tr>");
$row = explode("\t",$movie);
foreach ($row as $elementIndex => $element) {
if ($elementIndex == 3) {
print("<td class='nodiv'>$element</td>");
} else {
print("<td>$element</td>");
}
}
print("<td><input type='submit' name='movie$movieindex' value='Increment' \></td>");
print("</tr>");
}
?>
<tr>
<td><input type="text" name="moviename" pattern="[a-zA-Z0-9\s\.]+" title="Only Alphanumerical Characters allowed" required/></td>
<td>
<select name="genre">
<option value="Drama">Drama</option>
<option value="Comedy">Comedy</option>
<option value="Sci-fi">Sci-Fi</option>
<option value="Horror">Horror</option>
<option value="other">Other</option>
</select>
</td>
<td><input type="date" name="date" required/></td>
<td><input type="text" name="nomovie" /></td>
<td><input type="submit" name="newmovie" value="Add new"/></td>
</tr>
</table>
</form>
</body>
</html>
moviedata.txt
Star Wars Ep6 ROTJ Sci-fi 1985-05-25 1
Star Wars EP4 ANH Sci-fi 1977-05-25 3
Duplex Comedy 1998-10-30 2
Lord of the Rings Two Towers Sci-fi 2002-12-10 5
Monsters Inc. other 2014-02-06 1
Shooter Drama 2008-03-12 3
Campaign Comedy 2006-03-08 2