我需要一些帮助来添加搜索栏以搜索我的.txt文件。我想要的是用户可以搜索和比较他们的搜索与我的moviedata.txt文件中的内容的方式。我真的只希望他们按电影标题名称搜索,而不是数据或流派。我希望它以PHP形式,并且只是基于他们搜索提供的结果以数组形式。所以最有可能需要一个数组。它可能需要在txt文件中每行末尾的分隔符才能使搜索栏成为可能。我将在下面发布我的html和moviedata.txt信息。
感谢您的帮助。
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
答案 0 :(得分:0)
您应该考虑将数据迁移到数据库。
使用这样的SQL语句在数据库中搜索会很容易:"SELECT * FROM `movies` WHERE title LIKE '%" . $searchQuery . "%'";
编辑: 要在文本文件中进行实际搜索,您可以使用他们在此帖中提出的解决方案:PHP to search within txt file and echo the whole line
编辑2: 您可以按照本教程制作表格:http://php.about.com/od/learnmysql/ss/create_tables_2.htm
你可以像这样设计你的表:
ID | Title | Genre | ETC...
____|_________|_______|_______
1 | Starwars|Scifi | etc...
然后在php中你需要连接到mysql。
你可以这样做:
<?php
// CONNECT TO THE DATABASE
$DB_NAME = 'DATABASE_NAME';
$DB_HOST = 'DATABASE_HOST';
$DB_USER = 'DATABASE_USER';
$DB_PASS = 'DATABASE_PASSWORD';
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
// A QUICK QUERY ON A FAKE USER TABLE
$query = "SELECT * FROM `tablename` WHERE `Title` LIKE '%" . $search . "%'";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
// GOING THROUGH THE DATA
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row['Genre']);
}
}
else {
echo 'NO RESULTS';
}
// CLOSE CONNECTION
mysqli_close($mysqli);
&GT;