Need help fixing my pagination!

Discussion in 'Programming' started by Kurt Whittingham, May 7, 2012.

  1. #1
    I am trying to put pagination into pages of my site.. this is what i have got so far

    Page i want pagination on (the pagination boxes show up just dont work)=
    <?php
    
        require('connect.php');
    
        $page   = intval($_GET['page']);
        $tpages = ($_GET['tpages']) ? intval($_GET['tpages']) : 10; // 20 by default
        $adjacents  = intval($_GET['adjacents']);
        if($page<=0)  $page  = 1;
        if($adjacents<=0) $adjacents = 4;
        $reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages . "&amp;adjacents=" . $adjacents;
        
        $result = mysql_query("SELECT * FROM articles WHERE category_id='1' ORDER BY article_id DESC")
        or die(mysql_error());  
       
        echo "<table width='100%' cellspacing='10'>";
        // keeps getting the next row until there are no more to get
        while($row = mysql_fetch_array( $result )) {
        // Print out the contents of each row into a table
        echo "<tr><td>";
        // arrow
        echo ("<img src='arrow.gif'>"); echo "&nbsp;&nbsp;";
        // article name
        echo '<a style="font-size: 20px" href="'. $row['url'] .'">'. $row['article_name'] .'</a>';
        echo "</td></tr><tr><td>";
        // author + date
        echo $row['author']; echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"; echo $row['date'];
        echo "</td></tr><tr><td>";
        // Description
        echo "Brief Decription : "; echo "&nbsp;&nbsp;"; echo $row['description'];
        echo "<hr /></td></tr>";
    }
    
    
                echo "<tr><td>";
                
                include("pagination3.php");
                echo paginate_three($reload, $page, $tpages, $adjacents);
                
                echo "</td></tr>";
    ?>
    PHP:
    Pagination page =
    <?php
    /*************************************************************************
    php easy :: pagination scripts set - Version Three
    ==========================================================================
    Author:      php easy code, www.phpeasycode.com
    Web Site:    http://www.phpeasycode.com
    Contact:     webmaster@phpeasycode.com
    *************************************************************************/
    function paginate_three($reload, $page, $tpages, $adjacents) {
        
        $prevlabel = "&lsaquo; Prev";
        $nextlabel = "Next &rsaquo;";
        
        $out = "<div class=\"pagin\">\n";
        
        // previous
        if($page==1) {
            $out.= "<span>" . $prevlabel . "</span>\n";
        }
        elseif($page==2) {
            $out.= "<a href=\"" . $reload . "\">" . $prevlabel . "</a>\n";
        }
        else {
            $out.= "<a href=\"" . $reload . "&amp;page=" . ($page-1) . "\">" . $prevlabel . "</a>\n";
        }
        
        // first
        if($page>($adjacents+1)) {
            $out.= "<a href=\"" . $reload . "\">1</a>\n";
        }
        
        // interval
        if($page>($adjacents+2)) {
            $out.= "...\n";
        }
        
        // pages
        $pmin = ($page>$adjacents) ? ($page-$adjacents) : 1;
        $pmax = ($page<($tpages-$adjacents)) ? ($page+$adjacents) : $tpages;
        for($i=$pmin; $i<=$pmax; $i++) {
            if($i==$page) {
                $out.= "<span class=\"current\">" . $i . "</span>\n";
            }
            elseif($i==1) {
                $out.= "<a href=\"" . $reload . "\">" . $i . "</a>\n";
            }
            else {
                $out.= "<a href=\"" . $reload . "&amp;page=" . $i . "\">" . $i . "</a>\n";
            }
        }
        
        // interval
        if($page<($tpages-$adjacents-1)) {
            $out.= "...\n";
        }
        
        // last
        if($page<($tpages-$adjacents)) {
            $out.= "<a href=\"" . $reload . "&amp;page=" . $tpages . "\">" . $tpages . "</a>\n";
        }
        
        // next
        if($page<$tpages) {
            $out.= "<a href=\"" . $reload . "&amp;page=" . ($page+1) . "\">" . $nextlabel . "</a>\n";
        }
        else {
            $out.= "<span>" . $nextlabel . "</span>\n";
        }
        
        $out.= "</div>";
        
        return $out;
    }
    ?>
    PHP:
    But the pagination doesnt work, i was trying with 5 entries a page, then it would go to page2 etc etc.. but couldnt get it to work.

    any help would be awesome
     
    Kurt Whittingham, May 7, 2012 IP