0

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
Terry
  • 142
  • 4
  • 16
  • 2
    putting the data in a Database management system, would be easier to maintain, and faster search, by a factor of a bazillion –  Feb 09 '14 at 19:32
  • could you help me set that kind of a thing up? – Terry Feb 09 '14 at 19:44
  • that's very broad, do a little research make some attempts, and post when you get stuck –  Feb 09 '14 at 19:45

1 Answers1

0

You should really consider migrating your data to a database.

searching in the database would be quite easy with a SQL statement like this:

"SELECT * FROM `movies` WHERE title LIKE '%" . $searchQuery . "%'";

EDIT: To acctually search in a text file you can use the solution they propose in this post: PHP to search within txt file and echo the whole line

EDIT 2: You could make a table following this tutorial: http://php.about.com/od/learnmysql/ss/create_tables_2.htm

You could design you table like this:

 ID  |  Title  | Genre | ETC...
 ____|_________|_______|_______
  1  | Starwars|Scifi  | etc...

Then in php you need to connect to mysql.

You can do that like this:

<?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);

?>

Community
  • 1
  • 1
Philip G
  • 4,098
  • 2
  • 22
  • 41