Page 1 of 1
Splitting a DB into 2 web pages.
Posted: Wed Oct 11, 2006 7:48 am
by impulse()
If I have a DB that over 300 entries and I want a page that prints them all out, but the page is starting to take a while to load, what steps can I take to split this DB into 2 web pages?
I imagine something along the lines of counting all the rows in the DB, using a IF statement to check if the amount of rows are above 100 (for example) and if they are above 100 to overwrite the current MySQL query to something like
Code:
SELECT * FROM somewhere WHERE id < '100'
And build on it all from there?
If you can think of a more efficiant way to do it, then please share.
Posted: Wed Oct 11, 2006 7:53 am
by volka
first page: SELECT * FROM somewhere LIMIT 0,100
second page: SELECT * FROM somewhere LIMIT 100
Posted: Wed Oct 11, 2006 8:04 am
by GM
One way to do this would be to use a parameter in the URL to store the page number you are at:
eg:
http://www.mydomain.com/mypage.php?page=4
Then, use this $_GET parameter in your page to change the LIMIT value of the query:
Code: Select all
if(!isset($_GET['page'])) {
$page_num = 1;
} else {
$page_num = $_GET['page']; //don't forget to clean/sanitise this input!!!
}
define("RECORDS_PER_PAGE", 100); // can be done elsewhere
$starting_offset = ($page-1) * RECORDS_PER_PAGE;
$query = "SELECT * FROM my_table LIMIT ".$starting_offset.", ".RECORDS_PER_PAGE;
//execute the query, display the results etc.
You can put forward and backwards links on the page that reload the page, incrementing and decrementing the page number.
Posted: Wed Oct 11, 2006 8:20 am
by impulse()
GM wrote:One way to do this would be to use a parameter in the URL to store the page number you are at:
eg:
http://www.mydomain.com/mypage.php?page=4
Then, use this $_GET parameter in your page to change the LIMIT value of the query:
Code: Select all
if(!isset($_GET['page'])) {
$page_num = 1;
} else {
$page_num = $_GET['page']; //don't forget to clean/sanitise this input!!!
}
define("RECORDS_PER_PAGE", 100); // can be done elsewhere
$starting_offset = ($page-1) * RECORDS_PER_PAGE;
$query = "SELECT * FROM my_table LIMIT ".$starting_offset.", ".RECORDS_PER_PAGE;
//execute the query, display the results etc.
You can put forward and backwards links on the page that reload the page, incrementing and decrementing the page number.
Looking at this part ($starting_offset = ($page-1) * RECORDS_PER_PAGE; ):
Where is the value defined for "$page-1" ?
I like the idea of how this works though, it's interesting.
Posted: Wed Oct 11, 2006 8:22 am
by GM
sorry - you are right, should have been "$page_num-1"
I wrote it off the top of my head, so it's not tested or anything.
Posted: Wed Oct 11, 2006 8:33 am
by impulse()
If it's off the top of your head I'll question a little further as there's something else I don't understand.
$query = "SELECT * FROM my_table LIMIT ".$starting_offset.", ".RECORDS_PER_PAGE;
If RECORDS_PER_PAGE is an unchangable 100, when it creates a link on page 2 to goto page 3, wont the query look like:
Code: Select all
SELECT * FROM my_table LIMIT 200, 100
To the server, causing it to display records between 200-100?
Regards,
Posted: Wed Oct 11, 2006 8:36 am
by GM
LIMIT x,y means start at record x, and display y records.
LIMIT 100 is the same as LIMIT 0,100
LIMIT 200,100 means "start at record 200, and show me the next 100 records".
'tis all in the Manual.
Posted: Wed Oct 11, 2006 8:38 am
by impulse()
You'll have to excuse my presumptions.
Thank you for the help.
Stephen,
Posted: Wed Oct 11, 2006 8:43 am
by GM
No probs

Posted: Wed Oct 11, 2006 9:14 am
by impulse()
That has worked an absolute treat!
I'll be using that in the future.