Jump to content

[SOLVED] Get this to display 5 links on bottom


peranha

Recommended Posts

How can I get this to display 5 links on the bottom of the page.

 

EG.  I have a query that produces 6 pages.  when I go to page 3, it only displays from page 3 to page 6, and page 4 will display page 4 to page 6.  Can I get it to display 2,3,4,5,6 ?  and so on.

 

<?php
                  for($i =1; $i <= $this->show; $i++) // show ($show) links
                  {
                     
                        if($this->p > $this->totalpages){ // if p is greater then totalpages then display nothing
                            echo "";
                        }
                        else if($_GET["p"] == $this->p){ //if p is equal to the current loop value then dont display that value as link
                            echo $this->p ;
                        }
                        else{
                            echo " <a href=?p=".$this->p."> ".$this->p." </a>"; // else display the rest as links
                        }
            
                        $this->p++; //increment $p  
                  }
?>

Link to comment
Share on other sites

please rephrase your question.  The way I read it, you're saying it only shows current page to last page. But the code you provided looks like it just does 1 to last page.  You then mention something else altogether..something having to do with displaying 5 links?  So what is it you want to show, the current page and the next 4?  2 before current, current, 2 after? 

Link to comment
Share on other sites

Yes, the $this->show I can set to any number, right now I have it at 5.  I want it to show the current page and 2 before and 2 after.  But if it is on page 1, 2 or 3, to show 1-5.  on page page 4 show 2,3,4,5,6 and so on.  right now if I am on page 1 it shows 1,2,3,4,5.  page 2 shows 2,3,4,5,6.  page 3 shows 3,4,5,6.  page 4 shows 4,5,6.  etc.

 

Sorry by links I meant pages.

 

so the ideal thing is

 

current page in the middle unless it is 1,2 or it is one of the last 2 pages.  In my case right now 5,6.

 

Link to comment
Share on other sites

Given your logic, this should work.

 

<?php
			if ($this->p > 3) {
				$this->p = $this->p - 2;
			}

                  for($i =1; $i <= $this->show; $i++) // show ($show) links
                  {
                     
                        if($this->p > $this->totalpages){ // if p is greater then totalpages then display nothing
                            echo "";
                        }
                        else if($_GET["p"] == $this->p){ //if p is equal to the current loop value then dont display that value as link
                            echo $this->p ;
                        }
                        else{
                            echo " <a href=?p=".$this->p."> ".$this->p." </a>"; // else display the rest as links
                        }
            
                        $this->p++; //increment $p  
                  }
?>

Link to comment
Share on other sites

Are you really trying to even get this to work? Honestly it seems like you are expecting us to do all the hard work. The logic is there.

 

<?php
            if ($this->p > 3) {
                      $this->p = $this->p - 2;
            }elseif (($this->p + $this->show) > $this->totalpages) {
		$this->p = $this->p - ($this->totalpages - ($this->show + $this->p)); 
    }elseif ($this->p < 3 && $this->p != 1) {
		$this->p = $this->p - 1;
    }
            
?>

 

That should produce the desired results. I am not 100% sure, but yea. I think you can figure it out from that.

Link to comment
Share on other sites

Eh the edit time went away, found an error with my code:

 

<?php
          if ($this->p > 3 && (($this->p + $this->show) - 2) > $this->totalpages) {
               $this->p = ($this->p -  (($this->show + $this->p) - $this->totalpages)) + 1;
            }elseif ($this->p >= 3) {
			$this->p = $this->p - 2;
		}elseif ($this->p < 3 && $this->p != 1) {
			$this->p = $this->p - 1;
		}
?>

 

That should work like you described.

Link to comment
Share on other sites

Just wanna throw out there that based on my own experience with pagination, it looks like you're redundantly using variables here.  I think you probably assign $_GET["p"] to $this->p somewhere earlier, so there's really no point in using $_GET["p"].  Also, I'm assuming that $this->p is supposed to be your current page, but you end up using it as your counter var instead of using $i in your loop.  This doesn't technically break your code, but it does make it sort of redundant and confusing...

 

Link to comment
Share on other sites

Here is the final code.  Had to change the second if statement as when page 3 was clicked on, the links disappeared

 

Thanks

 

<?php
if ($this->p > 3 && (($this->p + $this->show) - 2) > $this->totalpages) {
               $this->p = ($this->p -  (($this->show + $this->p) - $this->totalpages)) + 1;
            }elseif ($this->p >= 3) {
            $this->p = $this->p - 2;
         }elseif ($this->p < 3 && $this->p != 1) {
            $this->p = $this->p - 1;
         }
?>

Link to comment
Share on other sites

try

<?php
$current_page = 4;
$totalpages = 6;
$show = 5;
$start = (int) max(1, min($current_page - floor(($show - 1)/2), $totalpages - $show + 1));
$end = min($totalpages, $start + $show - 1);
$count = 0;
for ($i = $start; $i <= $end; $i++) {
if($current_page == $i) echo $i."\n"; else echo "<a href=?p=".$i."> ".$i." </a>\n";
}
?>

 

Link to comment
Share on other sites

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.