查询结果的分页无法正常工作

时间:2014-11-20 14:44:14

标签: php jquery mysql pagination

我的PHP表单中有一个文本框,用户可以在其中插入电影标题,演员或导演名称(使用jQuery自动完成)。然后,如果用户单击“搜索”按钮,将打开一个新窗口,其中显示包含用户已插入文本框中的标题的电影列表(或者如果用户插入了演员/导演名称,则此窗口将显示列表那个演员/导演的电影。

问题:

由于有时电影列表太长(例如,用户可能只在文本框中键入“wall”,在这种情况下,结果数量为107),我更喜欢对结果进行分页(每页显示20部电影)。

以下是点击“搜索”按钮的代码:

$('#btnSearch').on('click', function (e) {           
           window.textbox = $('#q').val();
           window.searchType = $('input:radio[name=source]:checked').val();
           popupCenter("movielist.php","_blank","400","400");
  });

这是movielist.php:

<body>
<div id= "field">
</div>
 <script type="text/javascript">
  var searchType = parent.window.opener.searchType;
  var textbox = parent.window.opener.textbox;
  var pagenumber = $_GET['page'];

  $.ajax({
         url: 'searchfilm.php',
         datatype: "json",
         data:{q:textbox, t:searchType, page:pagenumber},
         success: function(response) {     
                     $("#field").html(response);
                   }
        });

</script>
</body>

这是searchfilm.php代码:     

$searchText = (isset($_GET['q'])) ? $_GET['q'] : "";
$searchType = (isset($_GET['t'])) ? $_GET['t'] : ""; /* type of research */

 if($searchText !== ""){
  switch ($searchType) {

     case 'byTitle':

      $page = (isset($_GET['page'])) ? $_GET['page'] : 1;

      $items_per_page = 20;
      $offset = 0;
      $page_count = 0;

      include('imdbConnection.php');
      $query1 = $conn->prepare("SELECT DISTINCT m.movieName, m.ImdbId, m.year, f.posterLink FROM featuredfilms_EN as m JOIN film_info as f ON m.ImdbId = f.ImdbId WHERE m.movieName LIKE :q");
      $query1->execute(array(':q' => '%' . $searchText . '%'));

      $row_count = $query1->rowCount();
      echo $row_count;
      if ($row_count === 0){
            echo '<td colspan="3">Sorry, there are no film matching your search</td>';
      }else{
           $page_count = (int)ceil($row_count / $items_per_page);
           echo $page_count;
           if($page > $page_count) { 
               $page = 1;    
           }
      }   

            $offset = ($page - 1) * $items_per_page;
          $query = $conn->prepare("SELECT DISTINCT m.movieName, m.ImdbId, m.year, f.posterLink FROM featuredfilms_EN as m JOIN film_info as f ON m.ImdbId = f.ImdbId WHERE m.movieName LIKE :q LIMIT " . $offset . "," . $items_per_page);
          $query->execute(array(':q' => '%' . $searchText . '%'));

             while ($row = $query->fetch(PDO::FETCH_ASSOC)):
         ?> 
              <tr>
               <td><img class='imdbImage' id="image" src='imdbImage.php?url=<?php echo $row['posterLink']; ?>' alt="" /></td>
               <td><label id='year'><?php echo $row['year']; ?> </label></td>
               <td><a href="http://www.imdb.com/title/<?php echo urlencode($row['ImdbId']); ?>"><?php echo $row['movieName']; ?></a></td>
              </tr>

         <?php
            endwhile;

          for ($i = 1; $i <= $page_count; $i++) {
             if ($i === $page) { // this is current page
                 echo "<strong>" . $i . "Page</strong>";
             } else {  
                echo '<a href="/movielist.php?page=' . $i . '">Page ' . $i . '</a><br>';
                }  
          }            

     break;



    case 'byActor' or 'byDirector':
      include('imdbConnection.php');
      $query = $conn->prepare("SELECT DISTINCT c.movieName, c.castName, c.ImdbId, f.year, f.posterLink FROM cast_movie as c JOIN film_info as f ON c.ImdbId = f.ImdbId WHERE c.castName = :q");
      $query->execute(array(':q' => $searchText ));
       //SAME AS BY TITLE CASE....
    break;           
  }
 }
  ?>

我的问题:

我的代码的结果是它在新窗口中显示前20个结果(电影),还链接到page2,page3,..,但是当我点击第2页时,它再次显示相同的电影列表。 。(其他页面也是如此)。

我知道问题与searchfilm.php中我的代码的ELSE部分有关,我将链接放到下一页。 (我知道之后没有查询显示结果),但即使我在其他地方移动了与查询相关的行,它也会显示所有页面中的所有107部电影(第1页,第2页等)。

如果有人帮我解决这个问题,我真的很感激,

提前致谢,

2 个答案:

答案 0 :(得分:0)

您应该在movielist.php中更改代码。

在您的情况下,您通过AJAX向searchfilm.php发送GET请求,并且传递给movielist.php的任何查询字符串都不会传递给searchfilm.php

所以试试这个而不是你的AJAX获取代码:

url: 'searchfilm.php?page=<?php echo intval($_GET["page"]);?>'

然后,您可以将page参数传递给searchfilm.php

答案 1 :(得分:0)

将您的页面变量传递给php并将其添加到您的代码中:

$page = (isset($_GET['page'])) ? $_GET['page'] : 1; /* page */

还将分页和电影列表分开:

        echo 'Page ' . $i . '<br>';
        $offset = ($page - 1) * $items_per_page;
        $query = $conn->prepare("SELECT DISTINCT m.movieName, m.ImdbId, m.year, f.posterLink FROM featuredfilms_EN as m JOIN film_info as f ON m.ImdbId = f.ImdbId WHERE m.movieName LIKE :q LIMIT " . $offset . "," . $items_per_page);
        $query->execute(array(':q' => '%' . $searchText . '%'));
        while ($row = $query->fetch(PDO::FETCH_ASSOC)):
            ?> 
            <tr>
                <td><img class='imdbImage' id="image" src='imdbImage.php?url=<?php echo $row['posterLink']; ?>' alt="" /></td>
                <td><label id='year'><?php echo $row['year']; ?> </label></td>
                <td><a href="http://www.imdb.com/title/<?php echo urlencode($row['ImdbId']); ?>"><?php echo $row['movieName']; ?></a></td>
            </tr>

            <?php
        endwhile;

        for ($i = 1; $i <= $page_count; $i++) {
            if ($i === $page) { // this is current page
                echo "<strong>" . $i . "Page</strong>";
            } else {  //I THINK THE PROBLEM IS THIS PART
                echo '<a href="/movielist.php?page=' . $i . '">Page ' . $i . '</a><br>';
            }
        }
        break;