Uploading all data at once is a bad idea, regardless of whether you save it on the front end or back end, always limit your results, in the case of "database" the best solution is to use LIMIT
(I believe that in SQL Server we use OFFSET
instead of LIMIT
), so you should only send the requested page data to the front-end.
I don’t know which bank you’re using, but the syntax is very similar in most banks, it works like this:
SELECT ... LIMIT [offset,] row_count
The offset
is the position in the bank, the row_count
is the result limit that you will display, note that if you use it like this:
SELECT ... LIMIT 10
He will do the offset
be equal to 1
because he was omitted and 10 will be the row_count
.
Usually for each pagination we use a limit of 15 to 30 (this varies according to each one).
In your code there’s a <select>
in the option sLengthMenu
of $.dataTable
, it should send the selected value to the back-end, in case it selected to show 10, it should send a request to the server and should run the query like this (http://localhost/page.php?limit=10
):
Note: I don’t know if you are using mysql or other type of database, but the logic is the same
$offset = 1;
$row_count = $_GET['limit'];
$stmt = $mysqli->prepare('SELECT ... LIMIT ?, ?');
$stmt->bind_param('i', $offset);
$stmt->bind_param('i', $row_count);
//query: SELECT ... LIMIT 1, 10;
If you want to go to page 2, you must multiply the $offset
, by the amount of lines that will display (http://localhost/page.php/?page=2&limit=10
):
$offset = 1;
if (isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] > 1) {
$offset = ($_GET['page'] - 1) * $_GET['limit'];
}
$row_count = $_GET['limit'];
$stmt = $mysqli->prepare('SELECT ... LIMIT ?, ?');
$stmt->bind_param('i', $offset);
$stmt->bind_param('i', $row_count);
//query: SELECT ... LIMIT 10, 10;
Page 3 (http://localhost/page.php.php?page=3&limit=10
) will generate:
SELECT ... LIMIT 20, 10;
Page 4 (http://localhost/page.php.php?page=4&limit=10
) will generate:
SELECT ... LIMIT 30, 10;
Page 5 (http://localhost/page.php.php?page=5&limit=10
) will generate:
SELECT ... LIMIT 40, 10;
To use with WHERE
, do something like:
SELECT * FROM tabela WHERE foo='abc' OR foo='xyz' LIMIT 1, 10
And so on and so forth.
Documentation
Ever thought of using
LIMIT ?, ?
and paging? Your question is more for SQL than javascript itself or I’m mistaken?– Guilherme Nascimento
i have a record with 30k+ lines, in php I simply gave a select * from data and had it printed in html, there in html I have a datatable that organizes all this data. The problem is it’s taking a long time to load everything. the idea is to print all the data so that the person can consult anything via client-side.
– João
Yes, but no one loads all data at once, it is totally unfeasible, so there is paging and so I suggested the
LIMIT
.– Guilherme Nascimento
Dude I made one with 10k already gave problem. Try to bring less data in the query. The browser hangs with this much data.
– Marconi
@João noticed that all your previous questions have answers, but you did not accept any, if any of those answers solved your other problems mark it as correct, if you do not know how to do this make a tour: http://answall.com/tour
– Guilherme Nascimento