Jump to content

Recommended Posts

 

 

Thanks to tons of great help by some of you one here, I've finally been successful in my first pagination project...well almost.

 

My script works wonderfully except one tiny hitch. There are 40 "rows' page. When there are no more "rows" to display, the pagination continues to paginate blank pages.

 

To be more specific. page 13 is the last page with rows on it, but you can still hit next and go onto blank pages.

 

The solution is obvious - to have the "next" button disappear if there are no rows left, but for some reason all of the if statements I've tried are failing to do so.

 

Here is the query:

 

$rowsperpage = 40;  // THERE ARE 40 AIRWAVES PER PAGE
            $currentpage = (int) $_GET['currentpage']; // This is getting which "page" the user wants to see, and putting it in $currentpage
            if($currentpage > 0) { // If $currentpage is greater than 0, we'll need to compensate by subtracting 1, so that we pull the correct set of results. Otherwise, we'll just start at 0 (see the else)
            $offset = ($currentpage - 1) * $rowsperpage;
            }else{
            $offset = 0;
            }

            $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 links:

 

// find out how many rows are in the table 
                                $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;


                                // 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
                                $prevpage = $currentpage - 1;
                                   echo "<div id='all_page_turn'>
                                         <ul>
                                         <li class='PreviousPageBlog round_10px'>
                                 <a href='http://www.cysticlife.org/Airwave_build.php?currentpage=$prevpage'>Previous</a></li><ul></div>";



} // end if 


                                // 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 id='all_page_turn'>
                                             <ul>
                                             <li class='PreviousPageBlog round_10px'><a href='http://www.cysticlife.org/Airwave_build.php?currentpage=$nextpage'>Next</a></li></ul></div> ";

                                }

Thanks in advance

Wrap a if statement around the following lines which checks to see if $nextpage does not equal to $totalpages ($nextpage != $totalpages)

                                        // echo forward link for next page 
                                       echo "   <div id='all_page_turn'>
                                                 <ul>
                                                 <li class='PreviousPageBlog round_10px'><a href='http://www.cysticlife.org/Airwave_build.php?currentpage=$nextpage'>Next</a></li></ul></div> ";

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.