herghost Posted November 13, 2010 Share Posted November 13, 2010 Hi all, Anyone give me a few hints how I can add a next and previous button to my pagination links? I currently have this: <?php } if($total_records > 5) ?> <div id="pg"> <?php { for ($i = 1; $i <= $total_pages; $i++) { echo "<a title='page $i' class='current' href='?catid=" . $catid ."&"; if($subid > 0) { echo "subid="; echo $subid . "&"; } echo "p= $i'>$i</a>"; } } ?> </div> Which displays them nicely, but I would like a next and previous button. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/218591-previousnext-pagination-links/ Share on other sites More sharing options...
requinix Posted November 13, 2010 Share Posted November 13, 2010 previous = if (current page > 1) show a link to page #(current page - 1) next = if (current page Quote Link to comment https://forums.phpfreaks.com/topic/218591-previousnext-pagination-links/#findComment-1133860 Share on other sites More sharing options...
herghost Posted November 13, 2010 Author Share Posted November 13, 2010 Thanks, but how would I find current page? Quote Link to comment https://forums.phpfreaks.com/topic/218591-previousnext-pagination-links/#findComment-1133869 Share on other sites More sharing options...
PaulRyan Posted November 13, 2010 Share Posted November 13, 2010 You could use $_GET. Just append the current page your viewing in the url like "example.com/?page=4" Then determine toe value from $_GET['page'] or something along those lines. Regards, Paul. Quote Link to comment https://forums.phpfreaks.com/topic/218591-previousnext-pagination-links/#findComment-1133870 Share on other sites More sharing options...
herghost Posted November 13, 2010 Author Share Posted November 13, 2010 Thanks again! The silly thing is I am already doing that for another part of the page !!! I am now using the below and still have a problem, $t is showing as the same as the page I am on ($p) $t = $p-1; if($p > 1 ) { echo "<a href=''?catid='" . $catid . "&"; if($subid > 0) { echo "subid="; echo $subid . "&"; echo "p=" . $t . "'><span class='disabled'>« Previous</span></a>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/218591-previousnext-pagination-links/#findComment-1133873 Share on other sites More sharing options...
PaulRyan Posted November 13, 2010 Share Posted November 13, 2010 Just for clarification... $t = the page you are viewing? If this is the case do $t-1 for previous page link and $t+1 for next page link Use the previous method of $total_pages to do the links between the next and prev. Hopefully you get what I mean, if not just let me know Regards, Paul. Quote Link to comment https://forums.phpfreaks.com/topic/218591-previousnext-pagination-links/#findComment-1133876 Share on other sites More sharing options...
herghost Posted November 13, 2010 Author Share Posted November 13, 2010 Hi Paul, Thanks for you reply again! $t is meant to be current page - 1 $p is the current page, however $p-1 shows the same page as I am already viewing as well! Quote Link to comment https://forums.phpfreaks.com/topic/218591-previousnext-pagination-links/#findComment-1133879 Share on other sites More sharing options...
PaulRyan Posted November 13, 2010 Share Posted November 13, 2010 Ohh right I see... So if you are viewing page 10 $t=9 and $p-1=current page Slightly confusing to me lol, but if you like I could re-write your current function and add next/prev links to it? I'll comment so you know whats happening ect... You could try using $p-2 = Prev and $p OR $p+1=Next Tell me how it works out bud Regards, Paul. Quote Link to comment https://forums.phpfreaks.com/topic/218591-previousnext-pagination-links/#findComment-1133882 Share on other sites More sharing options...
herghost Posted November 13, 2010 Author Share Posted November 13, 2010 Well just to test I tried this out <?php $pagnat = 1; $np = $p + $pagnat; $nl = $p - $pagnat; echo $np; echo $nl; { if($p > 1 ) { echo "<a href=''?catid='" . $catid . "&"; if($subid > 0) { echo "subid="; echo $subid . "&"; } echo "p=$nl'>« Previous</a> "; } for ($i = 1; $i <= $total_pages; $i++) { echo "<a title='page $i' class='current' href='?catid=" . $catid ."&"; if($subid > 0) { echo "subid="; echo $subid . "&"; } echo "p=$i'>$i</a> "; } } if($p > 0 ) { echo "<a href=''?catid='" . $catid . "&"; if($subid > 0) { echo "subid="; echo $subid . "&"; echo "p="; echo $np; echo "'> Next »</a> "; } } ?> Now $np and $nl echo out correctly when I just echo them, but when hovering on the next and prev buttons, they show the current page! So when I am on p=2 $np=3 and $nl=1 How the hell is that working! Quote Link to comment https://forums.phpfreaks.com/topic/218591-previousnext-pagination-links/#findComment-1133887 Share on other sites More sharing options...
herghost Posted November 13, 2010 Author Share Posted November 13, 2010 ehmm, time to feel like an idiot! <a href='' one to many ' there me thinks!!!! Thanks for all your help Paul Quote Link to comment https://forums.phpfreaks.com/topic/218591-previousnext-pagination-links/#findComment-1133888 Share on other sites More sharing options...
PaulRyan Posted November 13, 2010 Share Posted November 13, 2010 Ohh wow, I would have never spotted that in a million years LOL. Glad it's working now Paul. Quote Link to comment https://forums.phpfreaks.com/topic/218591-previousnext-pagination-links/#findComment-1133889 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.