Need help with pagination.

Discussion in 'PHP' started by zielak, Aug 14, 2007.

  1. #1
    Hey guys
    I have some problems with pagination. This script creates links like:
    PREV5 1 2 3 4 NEXT5

    But when I'm on page 1 link "NEXT5" is pointing to page 1 instead of page 2.
    When I'm on page 2 link "PREV5" is pointing to page 2 instead of page 1 and link "NEXT5" to page 1 instead of page 3.

    I was trying to figure out where's the problem but I can't. So any help will be appreciated.

    <?php
    require_once ('global.php');
    
        $limit          = 5;
        $query_count    = "SELECT * FROM lyrics";
        $result_count   = mysql_query($query_count);
        $totalrows      = mysql_num_rows($result_count);
    
        if(empty($page)){
            $page = 1;
        }
    
    
        $limitvalue = $page * $limit - ($limit);
    
    $result = mysql_query("SELECT * FROM lyrics LIMIT $limitvalue, $limit",$db);
    echo "<b>Manage Lyrics</b><br><br>";
    echo "<TABLE BORDER=2>";
    echo"<TR><TD><B>Artist Name</B><TD><B>Song Name</B><TD><B>Category<TD><B>Options</B></TR>";
    while ($myrow = mysql_fetch_array($result))
    {
    echo "<TR><TD>".$myrow["artist"]."<TD>".$myrow["song"]."</a><TD>".$myrow["category"];
    echo "<TD><a href=\"lyrics_view.php?id=".$myrow["id"]."\">View</a>&nbsp;&nbsp;";
    echo "<a href=\"delete_lyrics.php?id=".$myrow[id]."\">Delete</a> ";
    }
    echo "</TABLE>";
    
    
        if($page != 1){
            $pageprev = $page--;
    
            echo("<a href=\"http://www.example.com/lyrics_manage.php&page=$pageprev\">PREV".$limit."</a> ");
        }else{
            echo("PREV".$limit." ");
        }
    
        $numofpages = $totalrows / $limit;
    
        for($i = 1; $i <= $numofpages; $i++){
            if($i == $page){
                echo($i." ");
            }else{
                echo("<a href=\"http://www.example.com/lyrics_manage.php?page=$i\">$i</a> ");
            }
        }
    
    
        if(($totalrows % $limit) != 0){
            if($i == $page){
                echo($i." ");
            }else{
                echo("<a href=\"http://www.example.com/lyrics_manage.php?page=$i\">$i</a> ");
            }
        }
    
        if(($totalrows - ($limit * $page)) > 0){
            $pagenext = $page++;
    
            echo("<a href=\"http://www.example.com/lyrics_manage.php?page=$pagenext\">NEXT".$limit."</a>");
        }else{
            echo("NEXT".$limit);
        }
    
        mysql_free_result($result);
    
    ?>
    Code (markup):
     
    zielak, Aug 14, 2007 IP
  2. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think all you're missing is a line that gets the clicked page number and stores it in $page.

    Consider the scenario where you click "NEXT 5", which calls the script with $_GET['page'] = 2....

    The line of code which reads....

    
    if(empty($page)){
            $page = 1;
        }
    
    
    PHP:
    Will set $page back to 1..... every time becaue $page has never been initialised.

    It should read something like...

    
    $page=$_GET['page'];
    if(empty($page)){
        $page = 1;
    }
    
    PHP:
     
    ecentricNick, Aug 15, 2007 IP