开发者

Pagination Help

*EDIT***

I've made great progress by combining the main query that pulls the airwaves with some variables from the pagination query and have finally gotten the pagination to work. The only problem now is that the first page is blank and page two is correct starting with result 41-80

Here is the query that limits and pulls the airwaves

        $rowsperpage = 40;
        $currentpage = (int) $_GET['currentpage'];
        $offset = ($currentpage - 1) * $rowsperpage;
        $query = "SELECT * FROM `CysticAirwaves` WHERE `FromUserID` = `ToUserID` AND `status` = 'active' ORDER BY `date` DESC, `time` DESC LIMIT $offset, $rowsperpage" ;
        $request = mysql_query($query,$connection);
        $counter = 0;
        while($result = mysql_fetch_array($request)) {

and here is the pagination code:

$query = "SELECT COUNT(*) FROM `CysticAirwaves`";
                        $result = mysql_query($query, $connection) or trigger_error("SQL", E_USER_ERROR);
                        $r = mysql_fetch_row($result);
                        $numrows = $r[0];

                        // number of rows to show per page
                        $rowsperpage = 40;
                        // 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 = (int) $_GET['currentpage'];
                        } else {
                           // default page num
                           $currentpage = 1;
                        } // end if

                        // if current page is greater than total pages...
                        if ($currentpage > $totalpages) {
                           // set current page to last page
                           $currentpage = $totalpages;
                        } // end if
                        // if current page is less than first page...
                        if ($currentpage < 1) {
                           // set current page to first page
                           $currentpage = 1;
                        } // end if

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

                        // get the info from the db 
                        $query2 = "SELECT `id` FROM `CysticAirwaves` LIMIT $offset, $rowsperpage";
                        $result = mysql_query($query2, $connection) or trigger_error("SQL", E_USER_ERROR);

                        // while there are rows to be fetched...
                        while ($list = mysql_fetch_assoc($result)) {
                           // echo data
                           echo $list['id'] . " : " . $list['number'] . "<br />";
                        } // end while

                        /******  build the pagination links ******/
                        // range of num links to show
                        $range = 3;

                        // if not on page 1, don't show back links
                        if ($currentpage > 1) {
                           // show << link to go back to page 1
                           echo " <a href='http://www.cysticlife.org/Airwave_build.php?currentpage=1'><<</a> ";
                           // get previous page num
                           $prevpage = $currentpage - 1;
                           // show < link to go back to 1 page
                           echo " <a href='http://www.cysticlife.org/Airwave_build.php?currentpage=$prevpage'><</a> ";
                        } // end if 

                        // loop to show links to range of pages around current page
                        for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 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 " [<b>$x</b>] ";
                              // if not current page...
                              } else {
                                 // make it a link
                                 echo " <a href='http://www.cysticlife.org/Airwave_build.php?currentpage=$x'>$x</a> ";
                              } // end else
                           } // end if 
                        } // end for

                        // 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 " <a href='http://www.cysticlife.org/Airwave_build.php?currentpage=$nextpage'>></a> ";
                           // echo forward link for lastpage
                           echo " <a href='http://www.cysticlife.org/Airwave_build.php?currentpage=$totalpages'>>></a> ";
                        } // end if
                        /****** end build pagination links ******/
                        ?>

So long story short, I just am not able to pull the first 40 results but after that its fine


This is a better way to go about pagination:

SELECT SQL_CALC_FOUND_ROWS id FROM `CysticAirwaves` LIMIT $offset, $rowsperpage

Select the data you want to display using the example query above which will return the same result as your query2

Then execute another SQL query:

SELECT FOUND_ROWS()

This will return the number of rows found without using a limit. See more here:

http://dev.mysql.com/doc/refman/5.0/en/limit-optimization.html

This would be the fastest way to manage pagination.

You would then loop through the results of your first query and display then. All you need to keep track of is what page you are on, and how many rows to show per page.

If you page number * $rowsperpage > the result of the second select (FOUND_ROWS) then you do not need to calculate or show the 'next' pages options/links.

For 'next' pages, loop 3 times incrementing the page number or next page * $rowsperpage > total rows.

For 'previous' pages, loop 3 times decrementing the page number or previous page = 1 (break out of loop in this case).

That should do the trick.


Probably you code so much, that it's hard to read what you've once written. You seem to put a comment next to each line of code, which might underline that. So you probably should reduce the code to reduce this problem.

This is just a suggestion, you wrote:

        // if current page is greater than total pages...
        if ($currentpage > $totalpages) {
           // set current page to last page
           $currentpage = $totalpages;
        } // end if
        // if current page is less than first page...
        if ($currentpage < 1) {
           // set current page to first page
           $currentpage = 1;
        } // end if

All these if clauses you need to deal with! All these comments to read! Isn't that a burden?

        // currentpage must be within 1 and total of pages.
        $currentpage = max(1,min($totalpages, $currentpage));

How about that? If you have problems to understand it, you can put some comments on top that explain it as you do as well in your code - in your words if mine are not fitting, it's just a suggestion.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜