#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    12
    Rep Power
    0

    Talking Extra set of eyes (Pagination)


    Hey all, I have been working on pagination for a website I am building. I have the pagination done, but I am having an issue with the way the links on the bottom appear. For example, If I am on localhost/site/index.php?currentpage=1 The links at the bottom properly show that the current page is indeed 1, however, if I am on localhost/site/index.php?currentpage=2 the link shows that the page is page 1 but If I am on localhost/site/index.php?currentpage=3 it show's as page 3. I would appreciate assistance, in finding the bug if at all possible. I'll display all the code for the pagination for you, but I think the issue I am having is isolated to the to the
    PHP Code:
    for($x = ($currentpage -$range)) 
    part of the script.
    PHP Code:
    <?php
                         
    // GET COUNT OF DATA IN DB
                        
    $sqlcount "SELECT COUNT(*) FROM news WHERE type = 'news'";
                        
    $countquery $conn->prepare($sqlcount);
                        
    $countquery->execute(array(":news" => "type"));
                        
    $total_data $countquery->fetchColumn();
                        
    $data_per_page 6;
                        
    $page_total ceil($total_data/$data_per_page);
                        
                        if(isset(
    $_GET['currentpage']) && is_numeric($_GET['currentpage']))
                        {
                            
    // cast var as int
                            
    $currentpage = (int) $_GET['currentpage'];
                        }
                        else
                        {
                            
    // Default page number
                            
    $currentpage 1;
                        }
                        
    // If current page is greater than total pages
                        
    if($currentpage $page_total)
                        {
                            
    $currentpage $page_total;    
                        }
                        
    // If current page is less that first page
                        
    if($currentpage $page_total)
                        {
                            
    $currentpage 1;
                        }
                        
    // ensure user can't put in a page number that doesn't exist
                        
    $offset = ($currentpage 1) * $data_per_page;
                        
                        
    // Run the actual query
                        
    try
                        {
                            
    $sql "SELECT * FROM news WHERE type = 'news' ORDER BY ID DESC LIMIT $offset$data_per_page";
                            
    $query $conn->prepare($sql);
                            
    $query->execute(array(":news" => "type"));
                            while(
    $row $query->fetch(PDO::FETCH_ASSOC))
                            {
                                echo
    "<h3>"; echo $row['title']; echo "</h3>"; echo "<br>";
                                echo 
    $row['subject']; echo "<br>";    
                            }
                        }
                        catch(
    PDOException $e
                    {
                        echo 
    "Update from Database has failed! ERROR 00010!";
                    }
                    
    // Build Pagination links
                    
    if($currentpage 1)
                    {
                        echo 
    "<a href='{$_SERVER['PHP_SELF']}?currentpage=1'><</a>";
                    }
                    else
                    {
                        
    $prevpage $currentpage -1;
                        echo 
    "<a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'>< </a>";
                    }
                    
    $range 3;
                    
    // Loop to shw links to range of current pages
                    
    for($x = ($currentpage $range); $x < (($currentpage $range) + 1); $x++)
                    {
                        
    // If Valid Page
                        
    if(($x 0) && ($x <= $page_total))
                        {
                            if(
    $x == $currentpage)
                            {
                                echo 
    " [<b>$x</b>] ";
                            }
                            else
                            {
                                echo 
    "<a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x </a>";    
                            }
                        }
                    }
                    
    // If not last page, show forward and last page links
                    
    if($currentpage != $page_total)
                    {
                        
    $nextpage $currentpage 1;
                        echo 
    "<a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'> ></a>";
                        
                        echo 
    "<a href='{$_SERVER['PHP_SELF']}?currentpage=$page_total'>></a>";
                    }
                    
    ?>
    Any assistance in solving this would be great
  2. #2
  3. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,047
    Rep Power
    487
    Hello
    Is this even needed?

    // ensure user can't put in a page number that doesn't exist
    $offset = ($currentpage - 1) * $data_per_page;

    I ask because you have checks to ensure if $currentpage > $pagetotal or if < 1 already.

    Other then that, try adding some debugging (echo out $currentpage) at various points to see where it's getting messed up
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984

IMN logo majestic logo threadwatch logo seochat tools logo