peranha Posted November 9, 2008 Share Posted November 9, 2008 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/132049-solved-get-this-to-display-5-links-on-bottom/ Share on other sites More sharing options...
.josh Posted November 9, 2008 Share Posted November 9, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/132049-solved-get-this-to-display-5-links-on-bottom/#findComment-686262 Share on other sites More sharing options...
peranha Posted November 10, 2008 Author Share Posted November 10, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/132049-solved-get-this-to-display-5-links-on-bottom/#findComment-686333 Share on other sites More sharing options...
premiso Posted November 10, 2008 Share Posted November 10, 2008 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/132049-solved-get-this-to-display-5-links-on-bottom/#findComment-686339 Share on other sites More sharing options...
peranha Posted November 10, 2008 Author Share Posted November 10, 2008 Does not work correctly for pages under 3. If you go to page 2, then the link for page 1 disappears. Also when on page 5, it only displays 3,4,5,6 not 2,3,4,5,6. Quote Link to comment https://forums.phpfreaks.com/topic/132049-solved-get-this-to-display-5-links-on-bottom/#findComment-686347 Share on other sites More sharing options...
premiso Posted November 10, 2008 Share Posted November 10, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/132049-solved-get-this-to-display-5-links-on-bottom/#findComment-686354 Share on other sites More sharing options...
premiso Posted November 10, 2008 Share Posted November 10, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/132049-solved-get-this-to-display-5-links-on-bottom/#findComment-686359 Share on other sites More sharing options...
.josh Posted November 10, 2008 Share Posted November 10, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/132049-solved-get-this-to-display-5-links-on-bottom/#findComment-686361 Share on other sites More sharing options...
peranha Posted November 10, 2008 Author Share Posted November 10, 2008 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/132049-solved-get-this-to-display-5-links-on-bottom/#findComment-686366 Share on other sites More sharing options...
sasa Posted November 10, 2008 Share Posted November 10, 2008 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/132049-solved-get-this-to-display-5-links-on-bottom/#findComment-686381 Share on other sites More sharing options...
peranha Posted November 10, 2008 Author Share Posted November 10, 2008 Thanks sasa, that will work for any number of links that I specify. Quote Link to comment https://forums.phpfreaks.com/topic/132049-solved-get-this-to-display-5-links-on-bottom/#findComment-686665 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.