I have a textbox in my PHP form where user can insert a movie title, an actor or director name (with jQuery auto-completion). Then, if user click a "search" button, a new window will be opened which shows list of movies containing the title that user has inserted in the textbox(or if user inserted an actor/director name, then this window will show list of movies by that actor/director).
Since sometimes list of movies are too long(for example, user may type only "wall" in the textbox and in this case number of results are 107), I would prefer to paginate results (showing 20 movies per page).
Here is the code for clicking "search" button:
$('#btnSearch').on('click', function (e) {
window.textbox = $('#q').val();
window.searchType = $('input:radio[name=source]:checked').val();
popupCenter("movielist.php","_blank","400","400");
});
This is 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>
and this is searchfilm.php code:
$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;
}
}
?>
The result with my code is that it shows first 20 result(movies) in the new window,and also links to page2, page3, .., but when I click on page2, it again shows the same list of movies..(this is the same for other pages as well).
I know the problem is related to the ELSE part of my code in searchfilm.php where I put links to next pages. (I know there is no query after that to show the results), but even when I moved the lines related to query after else, then it shows all 107 movies in all pages (page1, page2, etc).
I really appreciate if someone kindly help me fix this problem,
Thanks in advance,
You should change your code in movielist.php
.
In your case, you send a GET request to searchfilm.php
via AJAX, and any query string which passed to movielist.php
will not passed to searchfilm.php
.
So try this instead of your AJAX get code :
url: 'searchfilm.php?page=<?php echo intval($_GET["page"]);?>'
then, you can pass the page
paramter to searchfilm.php
.
Pass your page variable to the php and add this to your code:
$page = (isset($_GET['page'])) ? $_GET['page'] : 1; /* page */
And also separate the pagination and the movie listing:
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.