Jump to content

[SOLVED] What to do?!?!


adam84

Recommended Posts

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
Share on other sites

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
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.