Splitting a DB into 2 web pages.
Moderator: General Moderators
-
impulse()
- Forum Regular
- Posts: 748
- Joined: Wed Aug 09, 2006 8:36 am
- Location: Staffordshire, UK
- Contact:
Splitting a DB into 2 web pages.
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.
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.
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:
You can put forward and backwards links on the page that reload the page, incrementing and decrementing the page number.
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.-
impulse()
- Forum Regular
- Posts: 748
- Joined: Wed Aug 09, 2006 8:36 am
- Location: Staffordshire, UK
- Contact:
Looking at this part ($starting_offset = ($page-1) * RECORDS_PER_PAGE; ):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:
You can put forward and backwards links on the page that reload the page, incrementing and decrementing the page number.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.
Where is the value defined for "$page-1" ?
I like the idea of how this works though, it's interesting.
-
impulse()
- Forum Regular
- Posts: 748
- Joined: Wed Aug 09, 2006 8:36 am
- Location: Staffordshire, UK
- Contact:
If it's off the top of your head I'll question a little further as there's something else I don't understand. 
To the server, causing it to display records between 200-100?
Regards,
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:$query = "SELECT * FROM my_table LIMIT ".$starting_offset.", ".RECORDS_PER_PAGE;
Code: Select all
SELECT * FROM my_table LIMIT 200, 100Regards,