Jump to content

Help with specific pagination !


iPixel

Recommended Posts

Ok long story short !

I've got some regular joe schmoe queries running ... now the totally result of these queries goes well over 2,000 records. Im looking to paginate these in a certain fashion :

 

<< Prev 1  2  3  4  5  6  7  8  9  10  Next >>

 

Each # obviously will display the page ! but Prev and Next will jump to either previous 10 or next 10... so if im on page 4 and i hit next ! i dont want to go to page 5 but rather page 11 and display:

 

<< Prev 11  12    13  14  15  16  17  18  19  20  Next >>

 

I hope this is doable ! if not please give me a better suggestion, a more effective and/or efficient way to be able to browse through all these records.

Link to comment
https://forums.phpfreaks.com/topic/46316-help-with-specific-pagination/
Share on other sites

there are many ways to impliment pagination into your search results. you have to decide what you think would be best for your users, and then decide the best way to code it. this is doable. have you searched for tutorials on pagination? have you come up with any code so far?

So i googled "php pagination" and look and see phpfreaks.com was on the top of the list...

 

http://www.phpfreaks.com/tutorials/43/0.php

 

So im looking at this pagination code and i like it its nice and clean but with 1 issue !

 

The Code below, lets say i have the 2000 records, and my limit is lets say 50 per page,

 

im gonna end up with  <<PREV  1  2  3  4  5 .....  35  36  37  38  39  40  NEXT>>

 

and thats the problem with this code, i do not want 40 pages, this database will grow to well over 30,000 records and i dont want to have 1,000 page shown on the pagination, and im not sure how to minimize it to 10's and have the prev and next move to either the previous 10 or the next 10  !

 

if($page != 1){  
        $pageprev = $page--; 
         
        echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV".$limit."</a> ");  
    }else{ 
        echo("PREV".$limit." "); 
    } 

    $numofpages = $totalrows / $limit;  
     
    for($i = 1; $i <= $numofpages; $i++){ 
        if($i == $page){ 
            echo($i." "); 
        }else{ 
            echo("<a href=\"$PHP_SELF?page=$i\">$i</a> "); 
        } 
    } 


    if(($totalrows % $limit) != 0){ 
        if($i == $page){ 
            echo($i." "); 
        }else{ 
            echo("<a href=\"$PHP_SELF?page=$i\">$i</a> "); 
        } 
    } 

    if(($totalrows - ($limit * $page)) > 0){ 
        $pagenext = $page++; 
          
        echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>");  
    }else{ 
        echo("NEXT".$limit);  
    }

 

Thanks for the help !

~iPixel

			// Build First and Prev links. If on page 1, we won't make links

		if($page > 1){
			$prev = ($page - 1);
			echo ("
					<a href=''>First</a>
					<a href=''>Prev</a>
			");
		} // end if 


		// build the links to the 4 previous and 4 next pages

		for($i = ($page - 3); $i <= ($page + 3); $i++){
		   // only make a link if the prev\next is a valid page number 
		   if (($i >= 1) && ($i <= $total_pages)) {
			 echo ($page == $i) ? "[$i] " : "<a href=''>$i</a> "; 
		   } // end if 
		} // end for		


		// Build Next and Last links. If on last page, we won't make a links

		if($page < $total_pages){
			$next = ($page + 1);
			echo ("
					<a href=''>Next</a>
					<a href=''>Last</a>
			");
		} // end if 

 

Now the part I want you to focus on is the middle:

 

for($i = ($page - 3); $i <= ($page + 3); $i++){

 

That will display only 3 pages each way, for example:

First Prev 1 2 3 [4] 5 6 7  Next Last

 

Hope this helped and sorry for the unorganized coding.

p.s. links were taken out.

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.