Pagination of custom mysql in wordpress
<p>I'm a novice with php and mysql so please be kind. The problem is I'm calling a database which has various states open, closed, pending etc, all this works fine.</p>

<p>The problem is when I click next on my paging links the page just reloads to the start page with all states visible and the first 6 rows visible.</p>

<p>My code seems to work fine as the url of next, prev page as a different start number for the start of the database query eg (http:/wordpress/?page_id=1072?s=6&amp;np=3).</p>

<p>I was wondering if this could be to with wordpress?</p>

<p>help much appreciated.</p>

<pre><code>$dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) ;
mysql_select_db (DB_NAME) ;

/*state control buttons */

echo "&lt;div id=\"shownew\"&gt;&lt;table class=\"showhide\"&gt;&lt;tr&gt;&lt;td&gt;&lt;p&gt;&lt;form method=\"post\" action=\"".htmlentities($_SERVER['REQUEST_URI'])."\"&gt;&lt;INPUT TYPE=\"submit\" name=\"showopen\" VALUE=\"\" class=\"show_open\"&gt;&lt;/form&gt;&lt;/p&gt;&lt;/td&gt;";
echo "&lt;td&gt;&lt;p&gt;&lt;form method=\"post\" action=\"".htmlentities($_SERVER['REQUEST_URI'])."\"&gt;&lt;INPUT TYPE=\"submit\" name=\"showpending\" VALUE=\"\" class=\"show_pending\"&gt;&lt;/form&gt;&lt;/p&gt;&lt;/td&gt;";
echo "&lt;td&gt;&lt;p&gt;&lt;form method=\"post\" action=\"".htmlentities($_SERVER['REQUEST_URI'])."\"&gt;&lt;INPUT TYPE=\"submit\" name=\"showclosed\" VALUE=\"\" class=\"show_closed\"&gt;&lt;/form&gt;&lt;/p&gt;&lt;/td&gt;";
echo "&lt;td&gt;&lt;p&gt;&lt;form method=\"post\" action=\"".htmlentities($_SERVER['REQUEST_URI'])."\"&gt;&lt;INPUT TYPE=\"submit\" name=\"show_all\" VALUE=\"\" class=\"show_all\"&gt;&lt;/form&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;";


$self = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] ;
/* Status set code */

if (isset($_POST['close'])) {
    $close_row = $_POST['close_row'];
    $close = "UPDATE support_users SET status = 'Closed', date_altered = NOW() WHERE ticket_number=$close_row";
    $closeresult = @mysql_query ($close);
    $viewstate = $_POST['viewstate'];
}

if (isset($_POST['open'])) {
    $close_row = $_POST['close_row'];
    $close = "UPDATE support_users SET status = 'Open', date_altered = NOW() WHERE ticket_number=$close_row";
    $closeresult = @mysql_query ($close);
    $viewstate = $_POST['viewstate'];
}

if (isset($_POST['pending'])) {
    $close_row = $_POST['close_row'];
    $close = "UPDATE support_users SET status = 'Pending', date_altered = NOW() WHERE ticket_number=$close_row";
    $closeresult = @mysql_query ($close);
    $viewstate = $_POST['viewstate'];
}

if (isset($_POST['pending_ami'])) {
    $close_row = $_POST['close_row'];
    $close = "UPDATE support_users SET status = 'Pending AMI', date_altered = NOW() WHERE ticket_number=$close_row";
    $closeresult = @mysql_query ($close);
    $viewstate = $_POST['viewstate'];
}

if (isset($_POST['pending_arp'])) {
    $close_row = $_POST['close_row'];
    $close = "UPDATE support_users SET status = 'Pending ARP', date_altered = NOW() WHERE ticket_number=$close_row";
    $closeresult = @mysql_query ($close);
    $viewstate = $_POST['viewstate'];
}

/* View state set code */

if (isset($_POST['showopen'])) {
    $viewstate='open';
}

if (isset($_POST['showpending'])) {
    $viewstate='pending';
}


if (isset($_POST['showclosed'])) {
    $viewstate='closed';
}

if (isset($_POST['show_all'])) {
    $viewstate='all';
}


$display = 6;

if (isset($_GET['np'])) {
    $num_pages = $_GET['np'];

} else {

        if ($viewstate=='open') {   
        $query = "SELECT ticket_number, first_name, surname, email, product, retailer, DATE_FORMAT(dop, '%d %M %Y') AS dop, message, address, DATE_FORMAT(created, '%d %M %Y %r') AS created, status FROM support_dev WHERE status='Open' ORDER BY ticket_number ASC";
        $query_result = mysql_query ($query); 
        $num_records = @mysql_num_rows ($query_result);

        if ($num_records &gt; $display) {
    $num_pages = ceil ($num_records/$display);
} else {
    $num_pages = 1;
    }
}


    elseif ($viewstate=='pending') {
        $query = "SELECT ticket_number, first_name, surname, email, product, retailer, DATE_FORMAT(dop, '%d %M %Y') AS dop, message, address, DATE_FORMAT(created, '%d %M %Y %r') AS created, status FROM support_dev WHERE status LIKE 'Pending%' ORDER BY ticket_number ASC";
        $query_result = mysql_query ($query); 
        $num_records = @mysql_num_rows ($query_result);

        if ($num_records &gt; $display) {
    $num_pages = ceil ($num_records/$display);
} else {
    $num_pages = 1;
}
    }

elseif ($viewstate=='closed') {
        $query = "SELECT ticket_number, first_name, surname, email, product, retailer, DATE_FORMAT(dop, '%d %M %Y') AS dop, message, address, DATE_FORMAT(created, '%d %M %Y %r') AS created, status FROM support_dev WHERE status='Closed' ORDER BY ticket_number ASC";
        $query_result = mysql_query ($query); 
        $num_records = @mysql_num_rows ($query_result);

        if ($num_records &gt; $display) {
    $num_pages = ceil ($num_records/$display);
} else {
    $num_pages = 1;
    }

}
else {
        $query = "SELECT ticket_number, first_name, surname, email, product, retailer, DATE_FORMAT(dop, '%d %M %Y') AS dop, message, address, DATE_FORMAT(created, '%d %M %Y %r') AS created, status FROM support_dev ORDER BY ticket_number ASC";
        $query_result = mysql_query ($query); 
        $num_records = @mysql_num_rows ($query_result);

        if ($num_records &gt; $display) {
    $num_pages = ceil ($num_records/$display);
} else {
    $num_pages = 1;
    }
}
}


if (isset($_GET['s'])) {
    $start = $_GET['s'];
} else {
    $start = 0;
}

    $query = "SELECT ticket_number, first_name, surname, email, product, retailer, DATE_FORMAT(dop, '%d %M %Y') AS dop, message, address, DATE_FORMAT(created, '%d %M %Y %r') AS created, status FROM support_dev ORDER BY ticket_number ASC LIMIT $start, $display";
    if ($viewstate=='open') {

        $query = "SELECT ticket_number, first_name, surname, email, product, retailer, DATE_FORMAT(dop, '%d %M %Y') AS dop, message, address, DATE_FORMAT(created, '%d %M %Y %r') AS created, status FROM support_dev WHERE status='Open' ORDER BY ticket_number ASC LIMIT $start, $display";
        }
            elseif ($viewstate=='pending') {
        $query = "SELECT ticket_number, first_name, surname, email, product, retailer, DATE_FORMAT(dop, '%d %M %Y') AS dop, message, address, DATE_FORMAT(created, '%d %M %Y %r') AS created, status FROM support_dev WHERE status LIKE 'Pending%' ORDER BY ticket_number ASC LIMIT $start, $display";
        }
        elseif ($viewstate=='closed') {
        $query = "SELECT ticket_number, first_name, surname, email, product, retailer, DATE_FORMAT(dop, '%d %M %Y') AS dop, message, address, DATE_FORMAT(created, '%d %M %Y %r') AS created, status FROM support_dev WHERE status='Closed' ORDER BY ticket_number ASC LIMIT $start, $display";}
        $result = @mysql_query ($query);
        $num = mysql_num_rows ($result);

    if ($num &gt; 0) {

    if ($num_pages &gt; 1) {
        echo '&lt;p&gt;';
        $current_page = ($start/$display) + 1;

        if ($current_page != 1) {
            echo '&lt;a href="?page_id=1072?s=' . ($start - $display) . '&amp;np=' . $num_pages . '"&gt;Previous&lt;/a&gt; ';
        }
        for ($i = 1; $i &lt;= $num_pages; $i++) {
            if ($i != $current_page) {
                echo '&lt;a href="?page_id=1072?s=' . (($display * ($i - 1))) . '&amp;np=' . $num_pages . '"&gt;' .$i .'&lt;/a&gt; ';
            } else {
                echo $i . ' ';
            }
        }
        if ($current_page !=$num_pages) {
            echo '&lt;a href="' . $query_result . '?s=' . ($start + $display) . '&amp;np=' . $num_pages . '"&gt;Next&lt;/a&gt; ';
        }
        echo '&lt;/p&gt;&lt;br /&gt;'; 

    }


     if ($result) {
         echo '
    &lt;div id="supviewwrapperheader"&gt;
    &lt;div id="supviewticket"&gt;&lt;p&gt;Ticket&lt;/p&gt;&lt;/div&gt; 
    &lt;div id="supviewfirst"&gt;&lt;p&gt;First Name&lt;/p&gt;&lt;/div&gt;
    &lt;div id="supviewlast"&gt;&lt;p&gt;Last Name&lt;/p&gt;&lt;/div&gt;
    &lt;div id="supviewemail"&gt;&lt;p&gt;Email&lt;/p&gt;&lt;/div&gt;
    &lt;div id="supviewproduct"&gt;&lt;p&gt;Product&lt;/p&gt;&lt;/div&gt;
    &lt;div id="supviewretailer"&gt;&lt;p&gt;Retailer&lt;/p&gt;&lt;/div&gt;
    &lt;div id="supviewdop"&gt;&lt;p&gt;D.O.P.&lt;/p&gt;&lt;/div&gt;
    &lt;div id="supviewmessage"&gt;&lt;p&gt;Message&lt;/p&gt;&lt;/div&gt;
    &lt;div id="supviewaddress"&gt;&lt;p&gt;Address&lt;/p&gt;&lt;/div&gt;
    &lt;div id="supviewcreated"&gt;&lt;p&gt;Date created&lt;/p&gt;&lt;/div&gt;
    &lt;div id="supviewstatus"&gt;&lt;p&gt;Status&lt;/p&gt;&lt;/div&gt;
    &lt;div id="supviewbuttons"&gt;&lt;p&gt;&lt;/p&gt;&lt;/div&gt;
    &lt;/div&gt;
    ';

    while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
        echo "
        &lt;div id=\"supviewwrapper\" class=\"".($row[10])."\"&gt;
        &lt;div id=\"supviewticket\"&gt;&lt;p&gt;&lt;a name=\"a".$row[0]."\"&gt;&lt;/a&gt;$row[0]&lt;/p&gt;&lt;/div&gt;
        &lt;div id=\"supviewfirst\"&gt;&lt;p&gt;$row[1]&lt;/p&gt;&lt;/div&gt;
        &lt;div id=\"supviewlast\"&gt;&lt;p&gt;$row[2]&lt;/p&gt;&lt;/div&gt;
        &lt;div id=\"supviewemail\"&gt;&lt;p&gt;$row[3]&lt;/p&gt;&lt;/div&gt;
        &lt;div id=\"supviewproduct\"&gt;&lt;p&gt;$row[4]&lt;/p&gt;&lt;/div&gt;
        &lt;div id=\"supviewretailer\"&gt;&lt;p&gt;$row[5]&lt;/p&gt;&lt;/div&gt;
        &lt;div id=\"supviewdop\"&gt;&lt;p&gt;$row[6]&lt;/p&gt;&lt;/div&gt;
        &lt;div id=\"supviewmessage\"&gt;&lt;p&gt;$row[7]&lt;/p&gt;&lt;/div&gt;
        &lt;div id=\"supviewaddress\"&gt;&lt;p&gt;$row[8]&lt;/p&gt;&lt;/div&gt;
        &lt;div id=\"supviewcreated\"&gt;&lt;p&gt;$row[9]&lt;/p&gt;&lt;/div&gt;
        &lt;div id=\"supviewstatus\"&gt;&lt;p&gt;$row[10]&lt;/p&gt;&lt;/div&gt;
        &lt;div id=\"supviewbuttons\"&gt;&lt;p&gt;&lt;form method=\"post\" action=\"".htmlentities($_SERVER['REQUEST_URI'])."#a".$row[0]."\"&gt;&lt;input type=\"hidden\" name=\"close_row\" value=\"".($row[0])."\" /&gt;&lt;input type=\"hidden\" name=\"viewstate\" value=\"".$viewstate."\" /&gt;&lt;INPUT TYPE=\"submit\" name=\"open\" VALUE=\"\" class=\"submit_open\"&gt;&lt;br /&gt;&lt;INPUT TYPE=\"submit\" name=\"pending\" VALUE=\"\" class=\"submit_pending\"&gt;&lt;br /&gt;&lt;INPUT TYPE=\"submit\" name=\"pending_ami\" VALUE=\"\" class=\"submit_pendingami\"&gt;&lt;br /&gt;&lt;INPUT TYPE=\"submit\" name=\"pending_arp\" VALUE=\"\" class=\"submit_pendingarp\"&gt;&lt;br /&gt;&lt;INPUT TYPE=\"submit\" name=\"close\" VALUE=\"\" class=\"submit_closed\"&gt;&lt;/form&gt;&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;";
    }
     }
    mysql_free_result ($result);

     }  


if (is_null($viewstate)) {
    $viewstate='all';
}



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