Previous/Next Pagination??

Discussion in 'PHP' started by medialab, Dec 6, 2011.

  1. #1
    Hey Everyone!

    I am looking for some help, and I am not really sure where to start. I am pretty new to PHP and I know this is a pain but if someone can help me out I'd be thoroughly greatful!

    I built my own "blog" to say in php and css, I have added many items but I am now faced with a "Pagination Issue" all items are showing up on the same page, which is fine for now, but in a week it will be like 20 items.

    I want to have a simple, "next" and "previous" system. I don't want page numbers or anything, just a next button on page 1, previous and next on page 2, and only previous on the last page.

    Any advice on how to easily accomplish this would be great.

    I have already connected to the database, selected all rows, got the data, I assume I need to find out how many items somehow, put a per cap page, I am just not sure how to get everything to actually work.

    Thanks in advance!!!
     
    medialab, Dec 6, 2011 IP
  2. proactiv3

    proactiv3 Peon

    Messages:
    55
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    0
    #2
    <?php
    // Get limit and offset for the query
    $offset		= isset($_GET['offset']) ? (int) $_GET['offset'] : 0;
    
    // Get total of entries on you blog
    $sqlRows	= "SELECT COUNT(id) AS number_posts FROM posts";
    $qryRows	= $db->query($sqlRows);
    $numRows	= $qryRows['number_posts'];
    
    // Query for posts
    $sqlPosts	= "SELECT * FROM posts ORDER BY id DESC LIMIT 20, {$offset}";
    $qryPosts	= $db->query($sqlPosts);
    
    // Display posts
    foreach($qryPosts as $post) {
    	...
    }
    
    // Display pagination
    for($i = 0; $i < (ceil($numRows / 20)); $i++) {
    	echo  '<a href="blog.php?offset="' . ($i * 20) . '">' . ($i + 1) . '</a>';
    }
    PHP:
    The code is untested and has some logic flaws (entries will be skipped and/or will be missing). It's just a quick example of a possible algorithm to solve your problem.

    An alternative would be to use a pre-built class (there are a lot around, as this is a very common problem):

    http://www.google.pt/search?gcx=c&sourceid=chrome&ie=UTF-8&q=pagination+class+php

    Good luck!
     
    proactiv3, Dec 6, 2011 IP
  3. medialab

    medialab Well-Known Member

    Messages:
    366
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    138
    Digital Goods:
    1
    #3
    thank you!
     
    medialab, Dec 7, 2011 IP
  4. kasun0777

    kasun0777 Well-Known Member

    Messages:
    355
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    138
    #4
    Pagination with numbers, first and last links.
    
    	<?php
    			// find out how many rows are in the table 
    			$sql = "SELECT COUNT(*) FROM table";
    			$result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);
    			$r = mysql_fetch_row($result);
    			$numrows = $r[0];
    	  		// number of rows to show per page
    			$rowsperpage = 20;
    			// 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;
    ?>
    			
    
    
    <?php
    //View Data
     $query = "SELECT * 
                    FROM table 
                    ORDER BY id DESC 
                    LIMIT $offset, $rowsperpage
                    ";
        $row = mysql_query($query);
        while($result = mysql_fetch_array($row)){
        //Viewing Details of $results
        }
        ?>
    
    <?php if($numrows > $rowsperpage){ ?>
          <?php
          // Pagination
    		$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='{$_SERVER['PHP_SELF']}?currentpage=1'> First Page </a> ";
    		   // get previous page num
    		   $prevpage = $currentpage - 1;
    		   // show < link to go back to 1 page
    		   echo " <a href='{$_SERVER['PHP_SELF']}?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='{$_SERVER['PHP_SELF']}?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='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'> > </a> ";
    		   // echo forward link for lastpage
    		   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'> Last Page </a> ";
    		} // end if
    		/****** end build pagination links ******/
    		?>
            <?php } ?>
    
    Code (markup):
     
    kasun0777, Dec 8, 2011 IP
  5. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #5
    MySQL limit takes 2 arguments - where to start from and how many rows to return.

    LIMIT offset , row_count

    So if you want articles 1-10, use
    LIMIT 10
    (it defaults to the first row). Now for the next page of 10, use
    LIMIT 11, 10
    For the next page
    LIMIT 21, 10
    etc.

    You can keep track of the starting article number in the client and send it to the server for next, previous, first or last requests. (See the PHP examples for jqGrid for a good example of paging using LIMIT.)
     
    Rukbat, Dec 10, 2011 IP