Pagin in php. How to do it?

Discussion in 'PHP' started by dre, Oct 14, 2006.

  1. #1
    I have a site that has alot of pages, but it navigation is becoming too big. At the more its like " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32". Now I want it to be like << 1 2 [3] 4 5 >> and lets say you was on page 20 it would be << 16 17 [20] 21 22 >>. I have a basic pagin system at the moment how would i add this? The site is www.ps3.sc
     
    dre, Oct 14, 2006 IP
  2. streety

    streety Peon

    Messages:
    321
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Something like the following should work

    $current_page = $_GET['p'];
    
    //Page display width - number of pages on each side of the current
    $page_width = 2;
    
    //Echo code for previous page
    
    echo "<a href='http://www.ps3.sc/?p=" . ($current_page - 1) . "'> < </a>";
    
    $x = $current_page - $page_width;
    
    while ($x <= ($current_page + $page_width)) {
    	
    	if ($x == $current_page) {
    		echo " [" . $x "] ";
    	} else {
    		echo " <a href='http://www.ps3.sc/?p=" . $x . "'>" . $x . "</a> ";
    	}
    	
    	$x++;
    
    }
    
    
    //Echo code for next page
    
    echo "<a href='http://www.ps3.sc/?p=" . ($current_page + 1) . "'> > </a>";
    PHP:
     
    streety, Oct 14, 2006 IP
    dre likes this.
  3. Triexa

    Triexa Active Member

    Messages:
    580
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Also note that this question has been asked (and answered) PLENTY of times... just do a search and you'll get a plethora of answers!
     
    Triexa, Oct 14, 2006 IP
  4. dre

    dre Peon

    Messages:
    1,367
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Also note that I did search... I just didnt find what i was looking for, so I starte a new thread.
     
    dre, Oct 15, 2006 IP
  5. dre

    dre Peon

    Messages:
    1,367
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Streety, Thanks I've got it working. But its like this << Prev -3 -2 [ -1 ] 0 1 Next >>, see the minus numbers? I have tried several ways to remove them when the page is one or below but no luck, any help?
     
    dre, Oct 16, 2006 IP
  6. streety

    streety Peon

    Messages:
    321
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #6
    No problem, try this . . .

    <?php
    
    if(isset($_GET['p'])) {
    	$current_page = $_GET['p'];
    }
    else {
    	$current_page = 1;
    }
    
    
    //Page display width - number of pages on each side of the current
    $page_width = 2;
    
    //Maximum and minimum page numbers
    $min_page = 1;
    $max_page = 10;
    
    //Echo code for previous page
    if (($current_page - 1) >= $min_page) {
    	echo "<a href='http://www.ps3.sc/?p=" . ($current_page - 1) . "'> < </a>";
    }
    	
    
    
    $x = $current_page - $page_width;
    
    while ($x <= ($current_page + $page_width)) {
    
    	if ($x == $current_page) {
    		echo " [" . $x . "] ";
    	} elseif (($x < $min_page) || ($x > $max_page)) {
    		//Do nothing
    	}
    	else {
    		echo " <a href='http://www.ps3.sc/?p=" . $x . "'>" . $x . "</a> ";
    	}
    
    	$x++;
    	
    }
    
    //Echo code for next page
    if (($current_page + 1) <= $max_page) {
    	echo "<a href='http://www.ps3.sc/?p=" . ($current_page + 1) . "'> > </a>"; 
    }
    
    ?>
    PHP:
    You would get the same problem at high numbers (going to pages you haven't created yet) but this script should fix that side of things as well.

    I don't know how you store your pages so I've just put a value (10) in for the moment. You could either manually update it each time you add a page or set it dynamically by counting the rows in your database table or files in your directory depending on what you use.
     
    streety, Oct 16, 2006 IP