Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Pagination Display

  1. #1
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Pagination Display

    I recently created a pagination script and its working nicely, but I am having issues displaying it where I want it to be. Here is the thing, I had to put the code above the where my data is being echoed because I have a while statement that needs the query to run before it. Because of this, the page links are displayed above the rest of my code. I am hoping there is a way that I can display the pagination at the end of my code instead. Here is the pagination code I am using in case it is needed:
    PHP Code:
    if($_GET['page']) { // Is the page defined?

        
    $page $_GET['page']; // Set page defined
        
    } else {

        
    $page 1// Default page is 1
        
    }
    $max 1// Max results on a page
    $cur = (($page $max) - $max); // Works out what results to show

    $sql "SELECT * FROM `users` ORDER BY `id` DESC LIMIT $cur$max";
    $result mysql_query($sql) or die ('Error selecting users from the database!' .mysql_error());
    $count_total mysql_query("SELECT * FROM `users` "); // Get data from database
    $count_total mysql_num_rows($count_total); // Count lines in database

    $total_pages ceil($count_total $max); // Total results divided by max to display

    if($page 1) { // Is the page # more than one?

        
    $prev = ($page 1); // If yes, take one away from current page
        
    echo '<a href="?page='.$prev.'">Previous</a>'// Echo link to previous page


    for(
    $i == 1$i <= $total_pages$i++) // For each page number

        
    if($page == $i) { // If page = current page
        
            
    echo '<b>'.$i.'</b>'// Echo page in bold
        
    } else {
        
            echo 
    ' <a href="?page='.$i.'">'.$i.'</a> '// Echo link to the page
        
    }
        
    if(
    $page $total_pages) { // Is there another page?

        
    $next = ($page 1); // If so, add 1 to current page
        
    echo '<a href="?page='.$next.'">Next>></a>'// Echo next page link

    I appreciate any help on this

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Instead of echoing the links, try assigning them to variables, then echo the variables at the bottom of the script.

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by thetestingsite View Post
    Instead of echoing the links, try assigning them to variables, then echo the variables at the bottom of the script.

    Hope this helps.
    I tried that, but am not really sure how to do it. Could I get a point in the right direction? Thanks

  4. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Code:
    if($_GET['page']) { // Is the page defined?
    
        $page = $_GET['page']; // Set page defined
        
    } else {
    
        $page = 1; // Default page is 1
        
    }
    $max = 1; // Max results on a page
    $cur = (($page * $max) - $max); // Works out what results to show
    
    $sql = "SELECT * FROM `users` ORDER BY `id` DESC LIMIT $cur, $max";
    $result = mysql_query($sql) or die ('Error selecting users from the database!' .mysql_error());
    $count_total = mysql_query("SELECT * FROM `users` "); // Get data from database
    $count_total = mysql_num_rows($count_total); // Count lines in database
    
    $total_pages = ceil($count_total / $max); // Total results divided by max to display
    
    if($page > 1) { // Is the page # more than one?
    
        $prev = ($page - 1); // If yes, take one away from current page
        $prevTxt = '<a href="?page='.$prev.'">Previous</a>'; 
    } 
    
    for($i == 1; $i <= $total_pages; $i++) // For each page number
    
        if($page == $i) { // If page = current page
        
            $curPage = '<b>'.$i.'</b>'; // Echo page in bold
        } else {
        
            $thePages = '<a href="?page='.$i.'">'.$i.'</a> '; // Echo link to the page
        }
        
    if($page < $total_pages) { // Is there another page?
    
        $next = ($page + 1); // If so, add 1 to current page
        $nextTxt = '<a href="?page='.$next.'">Next>></a>'; // Echo next page link
    }
    Then where you want the text at, add the following:

    Code:
    echo $prevTxt.$curPage.$thePages.$nextTxt;
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  5. #5
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    You must "echo" after defining the variables though, they must be down lower on the page, then the original code defining the variables.
    - Mike

  6. #6
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    That kinda worked, but the display is messed up. For example, on page 3 the page numbers look like this: Previous 3 2. And on page 1 it is: 1 3. Any idea what to do?

  7. #7
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Try changing $curPage to $thePages in your for loop. and where you echoed the variables, remove $curPage. See if that fixes it.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  8. #8
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by thetestingsite View Post
    Try changing $curPage to $thePages in your for loop. and where you echoed the variables, remove $curPage. See if that fixes it.
    Still no good . I am not sure what else to do

  9. #9
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Ok, having looked at it a little further and modifying the code a bit, try the following:

    Code:
    if($_GET['page']) { // Is the page defined?
    
        $page = $_GET['page']; // Set page defined
        
    } else {
    
        $page = 1; // Default page is 1
        
    }
    $max = 1; // Max results on a page
    $cur = (($page * $max) - $max); // Works out what results to show
    
    $sql = "SELECT * FROM `users` ORDER BY `id` DESC LIMIT $cur, $max";
    $result = mysql_query($sql) or die ('Error selecting users from the database!' .mysql_error());
    $count_total = mysql_query("SELECT * FROM `users` "); // Get data from database
    $count_total = mysql_num_rows($count_total); // Count lines in database
    
    $total_pages = ceil($count_total / $max); // Total results divided by max to display
    
    if($page > 1) { // Is the page # more than one?
    
        $prev = ($page - 1); // If yes, take one away from current page
        $prevTxt = '<a href="?page='.$prev.'">Previous</a>'; 
    } 
    
    $nav = ' ';
    
    for($i == 1; $i <= $total_pages; $i++) // For each page number
    
        if($page == $i) { // If page = current page
        
            $nav .= '<b>'.$i.'</b>'; // Echo page in bold
        } else {
        
            $nav .= '<a href="?page='.$i.'">'.$i.'</a> '; // Echo link to the page
        }
        
    if($page < $total_pages) { // Is there another page?
    
        $next = ($page + 1); // If so, add 1 to current page
        $nextTxt = '<a href="?page='.$next.'">Next>></a>'; // Echo next page link
    }
    
    echo $prevTxt . $nav . $nextTxt;
    That should work. Hope it helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  10. #10
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks, it works great that way. Only one more question. In the part where the previous page link is made, if I make the text for the link "<<Previous", it only shows up as "<". Is there any reason for this, like maybe the <<?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •