database post: pagination
<p>So I am trying to get my page to paginate after 10 posts on a forum.  The pagination buttons appear and function and the variable url is working correctly, however page one displays all the posts in the database instead of only 10 and page two displays the same thing (all posts).</p>

<p>I'm sure its something simple that I am just not seeing...</p>

<p>The code for my main forum page is this:</p>

<pre><code>    $conn = mysql_connect("$host", "$username", "$password");
        if(!$conn) die("Failed to connect to database!");
        $status = mysql_select_db("$db_name", $conn);
        if(!$status) die("Failed to select database!");


    $sql="SELECT * FROM $tbl_name ORDER BY id DESC";
    // OREDER BY id DESC is order result by descending 
    $result=mysql_query($sql);

        include ('pagination.php');
        ?&gt;
    &lt;?php
        $pager = new PS_Pagination($conn, $sql, 10, 5, "param1=valu1&amp;param2=value2");

        /*
         * Enable debugging if you want o view query errors
        */
        $pager-&gt;setDebug(true);

        /*
         * The paginate() function returns a mysql result set
         * or false if no rows are returned by the query
        */
        $rs = $pager-&gt;paginate();
        if(!$rs) die(mysql_error());

    while($rows=mysql_fetch_array($result)){ // Start looping table row 
    ?&gt;

    &lt;tr&gt;
    &lt;td bgcolor="#FFFFFF"&gt;&lt;? echo $rows['id']; ?&gt;&lt;/td&gt;
    &lt;td bgcolor="#FFFFFF"&gt;&lt;font size="3"&gt;&lt;a href="view_topic.php?id=&lt;? echo $rows['id']; ?&gt;"&gt;&lt;? echo $rows['topic']; ?&gt;&lt;/a&gt;&lt;/font&gt;&lt;BR&gt;&lt;/td&gt;
    &lt;td align="center" bgcolor="#FFFFFF"&gt;&lt;? echo $rows['view']; ?&gt;&lt;/td&gt;
    &lt;td align="center" bgcolor="#FFFFFF"&gt;&lt;? echo $rows['reply']; ?&gt;&lt;/td&gt;
    &lt;td align="center" bgcolor="#FFFFFF"&gt;&lt;? echo $rows['datetime']; ?&gt;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;?php
    // Exit looping and close connection 
    }
    mysql_close();
    ?&gt;
    &lt;tr&gt;
    &lt;td colspan="5" align="right" bgcolor="#E6E6E6"&gt;&lt;a href="create_topic.php"&gt;&lt;strong&gt;Create New Topic&lt;/strong&gt; &lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;/table&gt;
    &lt;br /&gt;

    &lt;center&gt;&lt;?php echo $pager-&gt;renderFullNav();

        ?&gt;&lt;/center&gt;
</code></pre>

<p>Then the code for my pagination file is this:</p>

<pre><code>&lt;?php


class PS_Pagination {
    var $php_self;
    var $rows_per_page = 10; //Number of records to display per page
    var $total_rows = 0; //Total number of rows returned by the query
    var $links_per_page = 5; //Number of links to display per page
    var $append = ""; //Paremeters to append to pagination links
    var $sql = "";
    var $debug = false;
    var $conn = false;
    var $page = 1;
    var $max_pages = 0;
    var $offset = 0;

    /**
     * Constructor
     *
     * @param resource $connection Mysql connection link
     * @param string $sql SQL query to paginate. Example : SELECT * FROM users
     * @param integer $rows_per_page Number of records to display per page. Defaults to 10
     * @param integer $links_per_page Number of links to display per page. Defaults to 5
     * @param string $append Parameters to be appended to pagination links 
     */

    function PS_Pagination($connection, $sql, $rows_per_page = 10, $links_per_page = 5, $append = "") {
        $this-&gt;conn = $connection;
        $this-&gt;sql = $sql;
        $this-&gt;rows_per_page = (int)$rows_per_page;
        if (intval($links_per_page ) &gt; 0) {
            $this-&gt;links_per_page = (int)$links_per_page;
        } else {
            $this-&gt;links_per_page = 5;
        }
        $this-&gt;append = $append;
        $this-&gt;php_self = htmlspecialchars($_SERVER['PHP_SELF'] );
        if (isset($_GET['page'] )) {
            $this-&gt;page = intval($_GET['page'] );
        }
    }

    /**
     * Executes the SQL query and initializes internal variables
     *
     * @access public
     * @return resource
     */
    function paginate() {
        //Check for valid mysql connection
        if (! $this-&gt;conn || ! is_resource($this-&gt;conn )) {
            if ($this-&gt;debug)
                echo "MySQL connection missing&lt;br /&gt;";
            return false;
        }

        //Find total number of rows
        $all_rs = @mysql_query($this-&gt;sql );
        if (! $all_rs) {
            if ($this-&gt;debug)
                echo "SQL query failed. Check your query.&lt;br /&gt;&lt;br /&gt;Error Returned: " . mysql_error();
            return false;
        }
        $this-&gt;total_rows = mysql_num_rows($all_rs );
        @mysql_close($all_rs );

        //Return FALSE if no rows found
        if ($this-&gt;total_rows == 0) {
            if ($this-&gt;debug)
                echo "Query returned zero rows.";
            return FALSE;
        }

        //Max number of pages
        $this-&gt;max_pages = ceil($this-&gt;total_rows / $this-&gt;rows_per_page );
        if ($this-&gt;links_per_page &gt; $this-&gt;max_pages) {
            $this-&gt;links_per_page = $this-&gt;max_pages;
        }

        //Check the page value just in case someone is trying to input an aribitrary value
        if ($this-&gt;page &gt; $this-&gt;max_pages || $this-&gt;page &lt;= 0) {
            $this-&gt;page = 1;
        }

        //Calculate Offset
        $this-&gt;offset = $this-&gt;rows_per_page * ($this-&gt;page - 1);

        //Fetch the required result set
        $rs = @mysql_query($this-&gt;sql . " LIMIT {$this-&gt;offset}, {$this-&gt;rows_per_page}" );
        if (! $rs) {
            if ($this-&gt;debug)
                echo "Pagination query failed. Check your query.&lt;br /&gt;&lt;br /&gt;Error Returned: " . mysql_error();
            return false;
        }
        return $rs;
    }

    /**
     * Display the link to the first page
     *
     * @access public
     * @param string $tag Text string to be displayed as the link. Defaults to 'First'
     * @return string
     */
    function renderFirst($tag = 'First') {
        if ($this-&gt;total_rows == 0)
            return FALSE;

        if ($this-&gt;page == 1) {
            return "$tag ";
        } else {
            return '&lt;a href="' . $this-&gt;php_self . '?page=1&amp;' . $this-&gt;append . '"&gt;' . $tag . '&lt;/a&gt; ';
        }
    }

    /**
     * Display the link to the last page
     *
     * @access public
     * @param string $tag Text string to be displayed as the link. Defaults to 'Last'
     * @return string
     */
    function renderLast($tag = 'Last') {
        if ($this-&gt;total_rows == 0)
            return FALSE;

        if ($this-&gt;page == $this-&gt;max_pages) {
            return $tag;
        } else {
            return ' &lt;a href="' . $this-&gt;php_self . '?page=' . $this-&gt;max_pages . '&amp;' . $this-&gt;append . '"&gt;' . $tag . '&lt;/a&gt;';
        }
    }

    /**
     * Display the next link
     *
     * @access public
     * @param string $tag Text string to be displayed as the link. Defaults to '&gt;&gt;'
     * @return string
     */
    function renderNext($tag = '&amp;gt;&amp;gt;') {
        if ($this-&gt;total_rows == 0)
            return FALSE;

        if ($this-&gt;page &lt; $this-&gt;max_pages) {
            return '&lt;a href="' . $this-&gt;php_self . '?page=' . ($this-&gt;page + 1) . '&amp;' . $this-&gt;append . '"&gt;' . $tag . '&lt;/a&gt;';
        } else {
            return $tag;
        }
    }

    /**
     * Display the previous link
     *
     * @access public
     * @param string $tag Text string to be displayed as the link. Defaults to '&lt;&lt;'
     * @return string
     */
    function renderPrev($tag = '&amp;lt;&amp;lt;') {
        if ($this-&gt;total_rows == 0)
            return FALSE;

        if ($this-&gt;page &gt; 1) {
            return ' &lt;a href="' . $this-&gt;php_self . '?page=' . ($this-&gt;page - 1) . '&amp;' . $this-&gt;append . '"&gt;' . $tag . '&lt;/a&gt;';
        } else {
            return " $tag";
        }
    }

    /**
     * Display the page links
     *
     * @access public
     * @return string
     */
    function renderNav($prefix = '&lt;span class="page_link"&gt;', $suffix = '&lt;/span&gt;') {
        if ($this-&gt;total_rows == 0)
            return FALSE;

        $batch = ceil($this-&gt;page / $this-&gt;links_per_page );
        $end = $batch * $this-&gt;links_per_page;
        if ($end == $this-&gt;page) {
            //$end = $end + $this-&gt;links_per_page - 1;
        //$end = $end + ceil($this-&gt;links_per_page/2);
        }
        if ($end &gt; $this-&gt;max_pages) {
            $end = $this-&gt;max_pages;
        }
        $start = $end - $this-&gt;links_per_page + 1;
        $links = '';

        for($i = $start; $i &lt;= $end; $i ++) {
            if ($i == $this-&gt;page) {
                $links .= $prefix . " $i " . $suffix;
            } else {
                $links .= ' ' . $prefix . '&lt;a href="' . $this-&gt;php_self . '?page=' . $i . '&amp;' . $this-&gt;append . '"&gt;' . $i . '&lt;/a&gt;' . $suffix . ' ';
            }
        }

        return $links;
    }

    /**
     * Display full pagination navigation
     *
     * @access public
     * @return string
     */
    function renderFullNav() {
        return $this-&gt;renderFirst() . '&amp;nbsp;' . $this-&gt;renderPrev() . '&amp;nbsp;' . $this-&gt;renderNav() . '&amp;nbsp;' . $this-&gt;renderNext() . '&amp;nbsp;' . $this-&gt;renderLast();
    }

    /**
     * Set debug mode
     *
     * @access public
     * @param bool $debug Set to TRUE to enable debug messages
     * @return void
     */
    function setDebug($debug) {
        $this-&gt;debug = $debug;
    }
}
?&gt;
</code></pre>
