Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting(E_ALL); // Näyttää virheet
- header('Content-Type: text/html; charset=UTF-8'); // Määrittää, että ulostuotettu teksti on "UTF-8" enkoodattu
- // Tietokantaan yhdistäminen
- require_once("connect.php");
- $output = '';
- // Tarkista, onko hakukenttä tyhjä
- if(empty($_GET['keyword'])) {
- $output = "Syötä hakusana";
- } else {
- $stmt = $pdo->prepare('SELECT id FROM movie_info
- WHERE title LIKE :keyword
- OR year LIKE :keyword
- OR genre LIKE :keyword
- OR actors LIKE :keyword');
- $find = '%' . $_GET['keyword'] . '%';
- $stmt->bindParam(':keyword', $find);
- $stmt->execute();
- $row_count = $stmt->rowCount();
- $page_count = 0;
- if ($row_count === 0) {
- // maybe show some error since there is nothing in your table
- $output = 'Ei hakutuloksia!';
- } else {
- // determine page number from $_GET
- $page = 1;
- if(!empty($_GET['page'])) {
- $page = filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT);
- if(false === $page) {
- $page = 1;
- }
- }
- $items_per_page = 3;
- $offset = ($page - 1) * $items_per_page;
- // determine page_count
- $page_count = (int)ceil($row_count / $items_per_page);
- // double check that request page is in range
- if($page > $page_count) {
- // error to user, maybe set page to 1
- $page = 1;
- }
- if(isset($_SESSION['id'])) {
- // database query for logged in users
- $query =
- "SELECT m.id, m.title, m.year, m.genre, m.description, m.imdb, m.poster, m.duration, m.actors, m.trailer,
- IF(f.movie_id IS NOT NULL, 1, 0) AS isFavourite
- FROM movie_info m
- LEFT JOIN favourites f ON f.movie_id = m.id AND f.user_id = :user_id
- WHERE m.title LIKE :keyword
- OR m.year LIKE :keyword
- OR m.genre LIKE :keyword
- OR m.actors LIKE :keyword
- ORDER BY m.year DESC LIMIT :offset, :items_per_page";
- } else {
- // database query for guests (exclude the favorites part)
- $query =
- "SELECT m.id, m.title, m.year, m.genre, m.description, m.imdb, m.poster, m.duration, m.actors, m.trailer
- FROM movie_info m
- WHERE m.title LIKE :keyword
- OR m.year LIKE :keyword
- OR m.genre LIKE :keyword
- OR m.actors LIKE :keyword
- ORDER BY m.year DESC LIMIT :offset, :items_per_page";
- }
- // prepare the query
- $stmt = $pdo->prepare($query);
- $pdo->query("SET NAMES 'utf8'");
- // Add "%" signs around "keyword" before executing the query
- $keyword_prefix = '%' . $_GET['keyword'] . '%';
- // bind keyword, offset and items_per_page
- $stmt->bindParam(':keyword', $keyword_prefix);
- $stmt->bindValue(':offset', (int) $offset, PDO::PARAM_INT);
- $stmt->bindValue(':items_per_page', (int) $items_per_page , PDO::PARAM_INT);
- if(isset($_SESSION['id'])) {
- // bind value for user id for logged in users
- $stmt->bindValue(':user_id', (int) $_SESSION['id'], PDO::PARAM_INT);
- }
- // execute the query
- $stmt->execute();
- while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
- $id = $row['id'];
- $poster = $row['poster'];
- $title = $row['title'];
- $description = $row['description'];
- $imdb = $row['imdb'];
- $actors = $row['actors'];
- $year = $row['year'];
- $duration = $row['duration'];
- $genre = $row['genre'];
- $trailer = $row['trailer'];
- if(isset($_SESSION['id'])) {
- $isFavourite = $row['isFavourite'];
- }
- $output .= '<div class="row">'.
- '<div class="col-xs-12 col-sm-12 col-md-12">'.
- '<div id="search_results">'.
- '<div class="poster">'.$poster.'</div>'.
- '<div class="title">'.
- '<p class="movietxt">'.$title.'</p>'.
- '</div>'.
- '<div class="description">'.
- '<p class="movietxt">'.$description.'</p)>'.
- '</div>'.
- '<div class="info">'.
- '<p class=" movietxt infotxt"><b>IMDb:</b> '.$imdb.'</p>'.
- '<p class=" movietxt infotxt"><b>Pääosissa:</b> '.$actors.'</p>'.
- '<p class=" movietxt infotxt"><b>Valmistumisvuosi:</b> '.$year.'</p>'.
- '<p class=" movietxt infotxt"><b>Kesto:</b> '.$duration.'</p>'.
- '<p class=" movietxt infotxt"><b>Genre:</b> '.$genre.'</p>'.
- '<p class=" movietxt infotxt"><b>Traileri:</b> '.$trailer.'</p>'.
- '</div>';
- <?php if(isset($_SESSION['id'])):?>
- <?php if ($isFavourite == 1): ?>
- <form class="star_form" method="POST" action="set_favourite.php">
- <input type="hidden" name="movie_id" value="'.$id.'" />
- <input type="submit" class="favourite_star_1" />
- </form>
- <?php else: ?>
- <form class="star_form" method="POST" action="set_favourite.php">
- <input type="hidden" name="movie_id" value="'.$id.'" />
- <input type="submit" class="favourite_star_0" />
- </form>
- <?php endif; ?>
- <?php endif; ?>
- $output .= '</div>'.
- '</div>'.
- '</div>'.
- '<div class="isolator"></div>';
- }
- if ($page_count > 1) {
- // Previous Button
- $output .= '<nav aria-label="Page navigation">'.
- '<ul class="pagination">'.
- '<li>'.
- '<a href="/search.php?keyword=' .$_GET['keyword'] . '&page=' .( $page - 1 ). '" aria-label="Previous">'.
- '<span aria-hidden="true">«</a></span>'.
- '</a>'.
- '</li>';
- // Page numbers
- for ($i = 1; $i <= $page_count; $i++) {
- $output .= ' <li><a href="/search.php?keyword=' .$_GET['keyword'] . '&page=' . $i . '">'.$i.'</a></li>';
- }
- // Next Button
- if ($page < $page_count) {
- $output .= '<li>'.
- '<a href="/search.php?keyword=' .$_GET['keyword'] . '&page=' .( $page + 1 ). '" aria-label="Next">'.
- '<span aria-hidden="true">»</span>'.
- '</a>'.
- '</li>'.
- '</ul>'.
- '</nav>';
- }
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement