Jump to content

Final PHP pagination step


RalphLeMouf

Recommended Posts

Thanks to great help from some of you, I am about two steps away from completing my pagination project. I have finally been able to get it to limit and paginate correctly but here is the problem.

 

When I go to the initial page. it is blank with NO results on it except the pagination display ( number of pages and arrows ) it is not attaching "currentpage=1" but when you go to the next page, results 41-80 are on there. So all I need to do is get it to pull the results of 1-40 on the first page. I've attached pages 1 and 2 as screen shots to give a better example.

 

Here is the code:

 

First query that pulls the airwaves and limits them:

 

$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 the code that paginates everything:

 

$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=1']) && is_numeric($_GET['currentpage=1'])) {
		               // cast var as int
		               $currentpage = (int) $_GET['currentpage=1'];
		            } 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
		               echo "<div id='all_page_turn'><ul><li class='PreviousPageBlog> <a href='http://www.cysticlife.org/Airwave_build.php?currentpage=1'><<</a></li></ul></div> ";
		               // 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 ******/

 

thanks in advance

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/239359-final-php-pagination-step/
Share on other sites

Firstly, you are using $_GET incorrectly. Your if statement is also wrong because you are saying "if $_GET['currentpage'] is numeric, make it an int".

 

This:

// get the current page or set a default
if (isset($_GET['currentpage=1']) && is_numeric($_GET['currentpage=1'])) {
     // cast var as int
     $currentpage = (int) $_GET['currentpage=1'];
} else {
     // default page num
     $currentpage = 1;
} // end if

 

Should be this:

// get the current page or set a default
$currentpage = (isset($_GET['currentpage'])) ? intval($_GET['currentpage']) : 1;

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.