murfitUK Posted August 18, 2006 Share Posted August 18, 2006 I probably can work this out for myself but at the moment I'm going round in circles and can't see the wood for the trees.Once a query has been executed and I know how many results there are, I wish to display them 10 to a page with links for "Previous 10", "Next 10" etc.How is the best way of doing this? Maybe there is a simple tutorial someone could point me to.My main problem is working out what should go in the link to call the next (or previous) 10 and does it mean I have to re-run the query to get the next lot (maybe using the LIMIT function).Any help gratefully received.Thanks. Link to comment Share on other sites More sharing options...
justsomeguy Posted August 18, 2006 Share Posted August 18, 2006 Yeah, limit is probably the best way to do it. You could do something like this: $first = intval($_GET['num']); // will default to 0 because of intval, and stop sql injectionif ($first < 0) $first = 0;$limit = 10;$sql = "SELECT * FROM table LIMIT $first, $limit"; <a href="page.php?num=<?php=($first - $limit)?>">Previous</a><a href="page.php?num=<?php=($first + $limit)?>">Next</a> Link to comment Share on other sites More sharing options...
dcole.ath.cx Posted August 18, 2006 Share Posted August 18, 2006 On my message board http://dcole.ath.cx/mb/I used $_GET to get the page from the url, then took that number times 10, then subtracted 10... that was the starting post. Then took the page number agin and took it times 10, to get the ending post... then took all the posts though a for statement from that start number to the end number. That gave me just 10 posts at a time! Link to comment Share on other sites More sharing options...
Guest C.O.G.T Posted August 21, 2006 Share Posted August 21, 2006 Yeah, limit is probably the best way to do it. You could do something like this:$first = intval($_GET['num']); // will default to 0 because of intval, and stop sql injectionif ($first < 0) $first = 0;$limit = 10;$sql = "SELECT * FROM table LIMIT $first, $limit"; <a href="page.php?num=<?php=($first - $limit)?>">Previous</a><a href="page.php?num=<?php=($first + $limit)?>">Next</a> This would be correct Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now