My pagination is not working
When my result search more than 10 result, at the bottom, it is only [ 1 ] <-the page only show first page, but not the other pages.
but i can view the page by using this
$search=search strings
#= 1,2,3,4.....
search1-7.php?search=$search&page=#
on the page 2,3,4.......
it only has this at the bottom
[First Page] [Prev] [ 1 ]
Here is my search script
<?php
include ('template/header.php');
include ('connect.php');
if( isset( $_GET['search'] ) && !empty( $_GET['search'] ) )
{
$rowsPerPage = 10;
$pageNum = 1;
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}
// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;
$keywords = explode(" ", $search);
$query = "SELECT * FROM artist LEFT OUTER JOIN lyric on (artist.artist_id = lyric.artist_id) WHERE artist_name LIKE '%".$keywords['0']."%' OR song_title LIKE '%".$keywords['0']."%' OR english_name LIKE '%".$keywords['0']."%' OR lyric LIKE '%".$keywords['0']."%'";
for ($i=1; $i<count($keywords); $i++) {
$query = $query." AND artist_name LIKE '%".$keywords[$i]."%' OR english_name LIKE '%".$keywords[$i]."%' OR song_title LIKE '%".$keywords[$i]."%'";
}
$query = $query." ORDER BY english_name LIMIT $offset, $rowsPerPage";
//$query = $query." ORDER by english_name ASC";
$result2 = mysql_query($query) or die(mysql_error());
?>
<form method="GET" action="search1-7.php">
<b>Search:</b> <input type="text" name="search" size="20" />
<input type="submit" value="Search!" />
</form>
<?
$keywords = explode(" ", $search);
$query = "SELECT COUNT(english_name) AS numrows FROM artist WHERE english_name LIKE '%".$keywords['0']."%'";
for ($i=1; $i<count($keywords); $i++) {
$query = $query." AND english_name LIKE '%".$keywords[$i]."%'";
}
$result = mysql_query($query) or die('Error, query failed');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);
// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav = '';
for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pageNum)
{
$nav .= " $page ";
}
else
{
$nav .= " <a href=\"$self?search=$search&page=$page\">$page</a> ";
}
}
// creating previous and next link
// plus the link to go straight to
// the first and last page
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"$self?search=$search&page=$page\">[Prev]</a> ";
$first = " <a href=\"$self?search=$search&page=1\">[First Page]</a> ";
}
else
{
$prev = ' ';
$first = ' ';
}
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\"$self?search=$search&page=$page\">[Next]</a> ";
$last = " <a href=\"$self?search=$search&page=$maxPage\">[Last Page]</a> ";
}
else
{
$next = ' ';
$last = ' ';
}
echo "<b><font color=\"00BFFF\">搜尋結果: $numrows </b></font> <br \>";
echo "<table height=\"100%\" width=\"80%\" broder=\"0\"><tr><td valign=\"top\">";
while($row = mysql_fetch_array($result2))
{
$artist_id = $row['artist_id'];
$artist_name = $row['artist_name'];
$english_name = $row['english_name'];
$song_title = $row['song_title'];
$lyric_id = $row['lyric_id'];
$lyric = $row['lyric'];
echo "<a href=\"show.php?artist_id=$artist_id\"><b>$artist_name $english_name</b></a> - <a href=\"lyric.php?lyric_id=$lyric_id\">$song_title</a><br><br>";
}
echo "</td></tr>";
echo "</table>";
echo "<font color=\"00BFFF\"><br><center><b>Result:</b></center>";
echo "<center>".$first . $prev . "[".$nav."]" . $next . $last."</center></font>";
} else {
echo "No result";
}
?>
<?
include ('template/footer.php');
?>