PHP mysql Pagination Problem

Discussion in 'PHP' started by nita, May 5, 2007.

  1. #1
    Hi.
    I have a problem with pagination of my mysql database results.
    Script is printing the first 10 results and links to the rest with no problem.
    But once i press on the link is putting me back to root of application, instead of paging out the rest of results.

    to test it - got: www.nita-on-line.com/movies/

    
    if(isset($_GET['cat'])) // when looking at selected category of movies
    {
    
    	// printing out menu with categories
    	$result = mysql_query("SELECT * FROM category ORDER BY cat") or die(mysql_error());
    	while($row = mysql_fetch_array( $result )) 
    	{
    	echo "<A href='index.php?cat=$row[cat]'>$row[cat]</a><br>";
    	}
    
    	$rowsPerPage = 10;
    	$pageNum = 1;
    	
    	if(isset($_GET['page']))
    	{
        $pageNum = $_GET['page'];
    	}
    	$offset = ($pageNum - 1) * $rowsPerPage;
    	
    	$cat=$_GET['cat'];
     	$result=mysql_query("SELECT * FROM movies WHERE cat='$cat' ORDER BY name LIMIT $offset, $rowsPerPage") or die(mysql_error());
     	while($row=mysql_fetch_array($result))
     	{
    	include "display_rec.php";
    	}
    
    $query   = "SELECT COUNT(name) AS numrows FROM movies WHERE cat='$cat'";
    $result  = mysql_query($query) or die('Error, query failed');
    $row1     = mysql_fetch_array($result, MYSQL_ASSOC);
    
    $numrows = $row1['numrows'];
    $maxPage = ceil($numrows/$rowsPerPage);
    
    $self = $_SERVER['PHP_SELF'];
    
    if ($pageNum > 1)
    {
        $page = $pageNum - 1;
        $prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
        
        $first = " <a href=\"$self?page=1\">[First Page]</a> ";
    } 
    else
    {
        $prev  = '';
        $first = '';
    }
    
    if ($pageNum < $maxPage)
    {
        $page = $pageNum + 1;
        $next = " <a href=\"$self.php?page=$page\">[Next]</a> ";
        
        $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
    } 
    else
    {
        $next = '';
        $last = '';
    }   
    echo $first . $prev . " Page $pageNum of $maxPage " . $next . $last;
    }
    
    Code (markup):
    if someone can help me to find a bug in this script, please

    Thanks a lot in advance.

    Nita
     
    nita, May 5, 2007 IP
  2. Neoto

    Neoto Active Member

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #2
    Hi,

    I visited your site and couldn't see any problem. For me the results were paginated correctly, I could see the "[First Page] [Prev] Page 3 of 5 [Next] [Last Page]" links and they all worked.


    Neoto
     
    Neoto, May 5, 2007 IP
  3. nita

    nita Member

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    i just got it fixed

    all i had to do is to add $_GET['cat'] to the links,
    it was that simple,

    thanks for your interest Neoto.

    perhaps you will be able to help me with my next problem with the same application. i will post it soon !!

    nita
     
    nita, May 5, 2007 IP