Pagination Problem

Discussion in 'PHP' started by Masterful, Mar 15, 2009.

  1. #1
    I have a simple pagination script on my movie_categories.php page (see below). I have added the following to the page:

    $cat_id = intval($_GET['cat_id']);
    PHP:
    Now, when someone clicks on, say, movie_categories.php?cat_id=horror, they will land on movie_categories.php and all of the horror movies will be presented. Everything works fine, except, when there is more than 1 page of results, the other pages don't work. For example, if there are 7 movies, and movies 1 to 5 are presented on Page 1, movies 6 and 7 are not presented on Page 2. Page 2 just says 'None'. Can anyone see why this is happening?

    movie_categories.php:

    <?php
    
    $conn = mysql_connect('localhost', 'root') or trigger_error("SQL", E_USER_ERROR);  
    mysql_select_db('hidden', $conn) or trigger_error("SQL", E_USER_ERROR);
    
    
    $cat_id = intval($_GET['cat_id']);
    
    
    $query = "SELECT COUNT(*) FROM tbl_movies WHERE cat_id='$cat_id'";
    $result = mysql_query($query, $conn) or trigger_error("SQL", E_USER_ERROR);
    $query_data = mysql_fetch_row($result);
    $numrows = $query_data[0];
    
    
    if (isset($_GET['pageno'])) {
    $pageno = $_GET['pageno'];
    } else {
    $pageno = 1;
    } // end else
    $rows_per_page = 5;
    $lastpage      = ceil($numrows/$rows_per_page);
    $pageno = (int)$pageno;
    if ($pageno > $lastpage) {
    $pageno = $lastpage;
    } // end if
    if ($pageno < 1) {
    $pageno = 1;
    } // end if
    $limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
    
    
    $select = "SELECT . . . WHERE cat_id='$cat_id' $limit";
    $select_results = mysql_query($select, $conn) or trigger_error("SQL", E_USER_ERROR);
    
    
    if (mysql_num_rows($select_results) >= 1) {
    
    
    if ($pageno == 1) {
    echo " ";
    } else {
    echo " <a class=\"pagination\" href='{$_SERVER['PHP_SELF']}?pageno=1'>First</a> ";
    $prevpage = $pageno-1;
    echo " <a class=\"pagination\" href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>Prev</a> ";
    } // end else
    echo " Page $pageno of $lastpage ";
    if ($pageno == $lastpage) {
    echo " ";
    } else {
    $nextpage = $pageno+1;
    echo " <a class=\"pagination\" href='{$_SERVER['PHP_SELF']}?pageno=$nextpage?'>Next</a> ";
    echo " <a class=\"pagination\" href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>Last</a> ";
    } // end else
    
    
    while($row = mysql_fetch_assoc($select_results)) {
    echo "Result . . .";
    
     
    } // end while
    
    } // end first if
    
    
    else 
    {
     echo "None";
    }
    
    ?>
    PHP:
    It's like the script doesn't support URLs with variables in them. When I land on Page 1, the URL in the browser address bar looks like this:

    movie_categories.php?id=horror.

    But when I click to Page 2, it looks like this:

    movie_categories.php?pageno=2.

    Anyone understand what's going on?
     
    Masterful, Mar 15, 2009 IP
  2. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #2
    correct me if I'm wrong here, but don't you need to put the cat_id back into the paging urls as well?
     
    shallowink, Mar 15, 2009 IP
  3. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #3
    Hi, Shallowink.

    That's the first thing I tried (i.e., <a href='{$_SERVER['PHP_SELF']}?cat_id=$cat_id?pageno=1'>First</a>), but it just kept showing Page 1's results on Page 2.

    Any other suggestions? :confused:
     
    Masterful, Mar 15, 2009 IP
  4. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #4
    Well, you might have more than one problem. From the first lines of that code, it looks like cat_id is going to have to be passed. You might try echoing all of the relevant variables to debug it. That's how I would start to track down what's going on.
     
    shallowink, Mar 15, 2009 IP
  5. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #5
    Ah, heck! I'm so retarded! Look at the way I done the URLs:

    <a href='{$_SERVER['PHP_SELF']}?cat_id=$cat_id?pageno=1'>First</a>.

    I put a question mark (?) between the two parameters instead of an ampersand (&). It should've been like this:

    <a href='{$_SERVER['PHP_SELF']}?cat_id=$cat_id&pageno=1'>First</a>.

    Sorry for wasting everyone's time.

    Shallowink, one rep point for you. :)
     
    Masterful, Mar 15, 2009 IP
  6. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #6
    Dude, it didn't let me give you a rep point. :mad: I must have repped you recently for something else. I'll give you one later. ;)
     
    Masterful, Mar 15, 2009 IP
  7. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #7
    Think it won't let you if you have given X(5?) rep already during the day. Thanks though :)
     
    shallowink, Mar 15, 2009 IP
  8. meannn

    meannn Peon

    Messages:
    255
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Good snippet thought.
     
    meannn, Mar 16, 2009 IP
  9. Stylesofts

    Stylesofts Peon

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Hello,

    We are giving a free pagination script so take good care of it..

    http://www.stylesofts.com/portal/pagingScript.php/



    Regards,

    Stylesofts Developing Team
     
    Stylesofts, Mar 17, 2009 IP