adam84 Posted July 29, 2008 Share Posted July 29, 2008 Alrighty, I have a search page on my site and what I want to do is when the user hits the search button. I want to build my query and all that and print out the total number of records found. But since there could be a ton of records found, I only want to display x records per page, so far no problem. My question is for me to do this would I have to build two different queries, 1. First query to count the total number of records found. 2. A second query to retrieve x amount of records, using LIMIT. Is this pretty much the only way of doing this??? Thanks for your help Link to comment https://forums.phpfreaks.com/topic/117186-solved-what-to-do/ Share on other sites More sharing options...
obsidian Posted July 29, 2008 Share Posted July 29, 2008 This is one of the topics where Google may be more help than a simple answer on here. You have the right of how to approach it, though. Do a search for "paginating mysql results" and you should come up with some pretty significant results to go with. Here is an idea to look at, though: <?php // Number of results per page $limit = 30; // Results page: default to page 1 $page = isset($_GET['p']) && is_numeric($_GET['p']) ? $_GET['p'] : 1; // Set up LIMIT clause $offset = ($limit * $p) - $limit; // I'm just throwing in a very simplistic query $countQ = "SELECT COUNT(*) FROM my_table"; $resQ = "SELECT * FROM my_table LIMIT $offset, $limit"; ?> I'm sure that will give you at least some direction to run with. Good luck! Link to comment https://forums.phpfreaks.com/topic/117186-solved-what-to-do/#findComment-602776 Share on other sites More sharing options...
adam84 Posted July 29, 2008 Author Share Posted July 29, 2008 Yea thats pretty much what I have now. I was just seeing if anyone knew of a more efficent way of doing it. Thanks for you help! Link to comment https://forums.phpfreaks.com/topic/117186-solved-what-to-do/#findComment-602788 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.