Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.36 KB | None | 0 0
  1. <?php
  2.     error_reporting(E_ALL); // Näyttää virheet
  3.     header('Content-Type: text/html; charset=UTF-8'); // Määrittää, että ulostuotettu teksti on "UTF-8" enkoodattu
  4.    
  5.     // Tietokantaan yhdistäminen
  6.     require_once("connect.php");
  7.     $output = '';
  8.  
  9.     // Tarkista, onko hakukenttä tyhjä
  10.     if(empty($_GET['keyword'])) {
  11.         $output = "Syötä hakusana";  
  12.     } else {
  13.  
  14.         $stmt = $pdo->prepare('SELECT id FROM movie_info
  15.                                WHERE title LIKE :keyword
  16.                                OR year LIKE :keyword
  17.                                OR genre LIKE :keyword
  18.                                OR actors LIKE :keyword');
  19.         $find = '%' . $_GET['keyword'] . '%';
  20.         $stmt->bindParam(':keyword', $find);
  21.         $stmt->execute();
  22.         $row_count = $stmt->rowCount();
  23.  
  24.         $page_count = 0;
  25.         if ($row_count === 0) {  
  26.             // maybe show some error since there is nothing in your table
  27.             $output = 'Ei hakutuloksia!';
  28.         } else {
  29.             // determine page number from $_GET
  30.             $page = 1;
  31.             if(!empty($_GET['page'])) {
  32.                 $page = filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT);
  33.                
  34.                 if(false === $page) {
  35.                     $page = 1;
  36.                 }
  37.             }
  38.  
  39.         $items_per_page = 3;
  40.         $offset = ($page - 1) * $items_per_page;
  41.            
  42.         // determine page_count
  43.         $page_count = (int)ceil($row_count / $items_per_page);
  44.         // double check that request page is in range
  45.         if($page > $page_count) {
  46.             // error to user, maybe set page to 1
  47.             $page = 1;
  48.         }
  49.  
  50.  
  51.         if(isset($_SESSION['id'])) {
  52.         // database query for logged in users
  53.         $query =
  54.             "SELECT m.id, m.title, m.year, m.genre, m.description, m.imdb, m.poster, m.duration, m.actors, m.trailer,
  55.             IF(f.movie_id IS NOT NULL, 1, 0) AS isFavourite
  56.             FROM movie_info m
  57.             LEFT JOIN favourites f ON f.movie_id = m.id AND f.user_id = :user_id
  58.             WHERE m.title LIKE :keyword
  59.             OR m.year LIKE :keyword
  60.             OR m.genre LIKE :keyword
  61.             OR m.actors LIKE :keyword
  62.             ORDER BY m.year DESC LIMIT :offset, :items_per_page";
  63.         } else {
  64.         // database query for guests (exclude the favorites part)
  65.         $query =
  66.             "SELECT m.id, m.title, m.year, m.genre, m.description, m.imdb, m.poster, m.duration, m.actors, m.trailer
  67.             FROM movie_info m
  68.             WHERE m.title LIKE :keyword
  69.             OR m.year LIKE :keyword
  70.             OR m.genre LIKE :keyword
  71.             OR m.actors LIKE :keyword
  72.             ORDER BY m.year DESC LIMIT :offset, :items_per_page";
  73.         }
  74.  
  75.         // prepare the query
  76.         $stmt = $pdo->prepare($query);
  77.  
  78.         $pdo->query("SET NAMES 'utf8'");
  79.      
  80.         // Add "%" signs around "keyword" before executing the query
  81.         $keyword_prefix = '%' . $_GET['keyword'] . '%';
  82.      
  83.         // bind keyword, offset and items_per_page
  84.         $stmt->bindParam(':keyword', $keyword_prefix);
  85.         $stmt->bindValue(':offset', (int) $offset, PDO::PARAM_INT);
  86.         $stmt->bindValue(':items_per_page', (int) $items_per_page , PDO::PARAM_INT);
  87.      
  88.         if(isset($_SESSION['id'])) {
  89.             // bind value for user id for logged in users
  90.             $stmt->bindValue(':user_id', (int) $_SESSION['id'], PDO::PARAM_INT);
  91.         }
  92.      
  93.         // execute the query
  94.         $stmt->execute();
  95.        
  96.             while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
  97.                
  98.                 $id = $row['id'];
  99.                 $poster = $row['poster'];
  100.                 $title = $row['title'];
  101.                 $description = $row['description'];
  102.                 $imdb = $row['imdb'];
  103.                 $actors = $row['actors'];
  104.                 $year = $row['year'];
  105.                 $duration = $row['duration'];
  106.                 $genre = $row['genre'];
  107.                 $trailer = $row['trailer'];
  108.                
  109.                 if(isset($_SESSION['id'])) {
  110.                     $isFavourite = $row['isFavourite'];
  111.                 }
  112.  
  113.                 $output .=  '<div class="row">'.
  114.                                 '<div class="col-xs-12 col-sm-12 col-md-12">'.
  115.                                     '<div id="search_results">'.
  116.                                         '<div class="poster">'.$poster.'</div>'.
  117.                                        
  118.                                         '<div class="title">'.
  119.                                             '<p class="movietxt">'.$title.'</p>'.
  120.                                         '</div>'.
  121.                                        
  122.                                         '<div class="description">'.
  123.                                             '<p class="movietxt">'.$description.'</p)>'.
  124.                                         '</div>'.
  125.                                        
  126.                                         '<div class="info">'.
  127.                                             '<p class=" movietxt infotxt"><b>IMDb:</b> '.$imdb.'</p>'.
  128.                                             '<p class=" movietxt infotxt"><b>Pääosissa:</b> '.$actors.'</p>'.
  129.                                             '<p class=" movietxt infotxt"><b>Valmistumisvuosi:</b> '.$year.'</p>'.
  130.                                             '<p class=" movietxt infotxt"><b>Kesto:</b> '.$duration.'</p>'.
  131.                                             '<p class=" movietxt infotxt"><b>Genre:</b> '.$genre.'</p>'.
  132.                                             '<p class=" movietxt infotxt"><b>Traileri:</b> '.$trailer.'</p>'.
  133.                                         '</div>';
  134.  
  135.                                         <?php if(isset($_SESSION['id'])):?>
  136.                                             <?php if ($isFavourite == 1): ?>
  137.                                                 <form class="star_form" method="POST" action="set_favourite.php">
  138.                                                     <input type="hidden" name="movie_id" value="'.$id.'" />
  139.                                                     <input type="submit" class="favourite_star_1" />
  140.                                                 </form>
  141.                                             <?php else: ?>
  142.                                                 <form class="star_form" method="POST" action="set_favourite.php">
  143.                                                     <input type="hidden" name="movie_id" value="'.$id.'" />
  144.                                                     <input type="submit" class="favourite_star_0" />
  145.                                                 </form>
  146.                                             <?php endif; ?>
  147.                                          <?php endif; ?>                
  148.                        $output .= '</div>'.
  149.                                 '</div>'.
  150.                             '</div>'.
  151.                             '<div class="isolator"></div>';
  152.             }
  153.  
  154.             if ($page_count > 1) {
  155.                  // Previous Button
  156.                 $output .= '<nav aria-label="Page navigation">'.
  157.                                 '<ul class="pagination">'.
  158.                                     '<li>'.
  159.                                         '<a href="/search.php?keyword=' .$_GET['keyword'] . '&page=' .( $page - 1 ). '" aria-label="Previous">'.
  160.                                             '<span aria-hidden="true">&laquo;</a></span>'.
  161.                                         '</a>'.
  162.                                     '</li>';
  163.                     // Page numbers
  164.                 for ($i = 1; $i <= $page_count; $i++) {
  165.                        $output .= ' <li><a href="/search.php?keyword=' .$_GET['keyword'] . '&page=' . $i . '">'.$i.'</a></li>';
  166.                 }
  167.          
  168.              // Next Button
  169.                 if ($page < $page_count) {
  170.                     $output .=      '<li>'.
  171.                                         '<a href="/search.php?keyword=' .$_GET['keyword'] . '&page=' .( $page + 1 ). '" aria-label="Next">'.
  172.                                             '<span aria-hidden="true">&raquo;</span>'.
  173.                                         '</a>'.
  174.                                     '</li>'.
  175.                                 '</ul>'.
  176.                             '</nav>';
  177.                 }
  178.             }
  179.         }
  180.     }
  181.     ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement