Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2009
    Location
    MA
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts

    categories pagination + for loop issues

    Hey all,
    I am hoping there is a simple solution behind this, but I've been trying for awhile now and cannot figure it out.

    I have a website, where there are categories, which people will be able to click on a category. It then goes to: pages.php?action=get_entries&cat_id=categorynamegoeshere - This all works fine. But I cannot for the life of me figure out why the pagination isn't working properly on the category page. I have it to where it works to a point, not 100%. The page #'s are not being displayed for some reason. When I click "Next" it does go to the next page fine, but "First" does not become a link to go back to the first page.

    If I take off the " WHERE `category` = '$cat_id'" part in the $sql, it displays pages, except it displays all of the pages, not just for that one category lol. I believe that WHERE statement needs to be there.. but again, the #'s will not display properly..

    This pagination script is working fine, except on categories... from my testing it seems that the problem is the FOR LOOP (you'll see that alittle further down where $x variable is). Any help would be appreciated!

    PHP Code:
    function pagination_catid()
        {
        
    $connection db_connect();

        
    /****** pagination ******/
        // find out how many rows are in the table
        
    $cat_id $_REQUEST['cat_id'];
        
    $sql "SELECT COUNT(*) FROM `posts` WHERE `category` = '$cat_id'";
        
    $result mysql_query($sql$connection);
        
    $r mysql_fetch_row($result);
        
    $numrows $r[0];

        
    // number of rows to show per page
        
    $rowsperpage 10;
        
    //find out total pages
        
    $totalpages ceil($numrows $rowsperpage);

        
    // get the current page or set a default
        
    if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage']))
            {
            
    // cast var as int
            
    $currentpage $_GET['currentpage'];
            }
        else
            {
            
    // default page num
            
    $currentpage 1;
            }
                
        
    // if current page is greater than total pages...
        
    if ($currentpage $totalpages)
            {
            
    // set current page to last page
            
    $currentpage $totalpages;
            }

        
    // if current page is less than first page...
        
    if ($currentpage 1)
            {
            
    // set current page to first page
            
    $currentpage 1;
            }

        
    // the offset of the list, based on current page
        //$offset = ($currentpage - 1) * $rowsperpage;

        /******  build the pagination links ******/
        // if not on page 1, don't show back links
        
    if ($currentpage 1)
            {
            
    // show << link to go back to page 1
            
    echo "<div class='paginleft'><a href='pages.php?action=get_entries&cat_id=$cat_id&currentpage=1'>First</a></div>";
            }
        else
            {
            echo 
    "<div class='paginleft'>First</div>";
            }

        
    // if not on last page, show forward and last page links
        
    if ($currentpage != $totalpages)
            {
            
    // get next page
            
    $nextpage $currentpage 1;
            
    // echo forward link for next page
            
    echo "<div class='paginright'><a href='pages.php?action=get_entries&cat_id=$cat_id&currentpage=$nextpage'>Next</a></div>";
            }
        else
            {
            echo 
    "<div class='paginright'>Next</div>";
            }
        
        
    // if not on page 1, don't show back links
        
    if ($currentpage 1)
            {
            
    // get previous page num
            
    $prevpage $currentpage 1;
            
    // show < link to go back to 1 page
            
    echo "<div class='paginright'>|</div>";
            echo 
    "<div class='paginright'><a href='pages.php?action=get_entries&cat_id=$cat_id&currentpage=$prevpage'>Previous</a></div>";
            }
        else
            {
            echo 
    "<div class='paginright'>|</div>";
            echo 
    "<div class='paginright'>Previous</div>";
            }

        
    // range of num links to show
        
    $range 5;
        
    $range2 4;

        
    // loop to show links to range of pages around current page
        
    for ($x = ($currentpage $range); $x < (($currentpage $range2) + 1); $x++)
            {
            
    // if it's a valid page number...
            
    if (($x 0) && ($x <= $totalpages))
                {
                
    // if we're on current page...
                
    if ($x == $currentpage)
                    {
                    
    // highlight it but don't make a link
                    
    echo "<div class='paginx'><b>$x</b></div>";
                    }
                
    // if not current page
                
    else
                    {
                    
    // make it a link
                    
    echo "<div class='paginx'><a href='pages.php?action=get_entries&cat_id=$cat_id&currentpage=$x'>$x</a></div>";
                    }
                }
            }

        if (
    $currentpage != $totalpages)
            {
            if (
    $totalpages 5)
                {
                if (
    $totalpages < (($currentpage $range2) + 1))
                    {
                    echo 
    "";
                    }
                else
                    {
                    echo 
    "<div class='paginx'> ... </div> ";
                    echo 
    "<div class='paginx'><a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>$totalpages</a></div> ";
                    }
                }
            
    // echo forward link for lastpage
            
    echo "<div class='paginlast'><a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>Last</a></div> ";
            }
        else
            {
            echo 
    "<div class='paginlast'>Last</div>";
            }
        } 
    Last edited by n4te02; 10-14-2009 at 12:49 AM.

  • #2
    New Coder
    Join Date
    Jun 2009
    Location
    MA
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts
    bump ^^

    anyone?

  • #3
    New Coder
    Join Date
    Jun 2009
    Location
    MA
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I could really use some help on this ;x
    anyone have any insight at all?


  •  

    Posting Permissions

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