I hope I do not write a duplicate, but I did not find anything that could answer my question. (Although it seems to me that this is a fairly common problem.)
The problem arises in almost every web project: you have a table with many entries and want them to appear on separate pages.
Now I am wondering what is the best way to calculate the number of pages needed for a particular set of table rows.
Here are some approaches I've been thinking about. I would like to receive a response to how effective they are. I will give examples specific to PHP, but I am sure that similar methods exist in other languages.
Probably the best way is to keep the number of pages statically and change the value each time you add a new record. (However ... I'm looking for a dynamic solution :-))
Draw SELECT COUNT(*)on the lines of interest and calculate the page number each time the page is displayed.
SELECT COUNT(*)
Make the usual selection to get a result set for all rows. Now do not load the lines, calling mysql_fetch_rowor so, but get the number of lines with mysql_num_rows. (Since I have no idea how this is implemented, I cannot say whether it is effective or not. Who knows?) Then I could conveniently move the pointer to the result set. (For mysqliexists mysql_data_seek, but the native MySQL extension does not have a similar function. Therefore, I assume that this is just some buffering behavior mysqli)
mysql_fetch_row
mysql_num_rows
mysqli
mysql_data_seek
, ( )?
2
select count(*) from [Table] where [Expressions]
select [Columns] from [Table] where [Expressions] limit [Pagesize] offset [Pagenum*Pagesize-Pagesize]
, .
CMS , , ..
- WTF, , , /.
, , ;).
SELECT COUNT, . , , .
, , . . , . ?
, , , . "page N of M" - , .
, ( ), " " (fetchsize, top ..), . , , , .
.
, SQL-, LIMIT. , SELECT * FROM myTable LIMIT 0,20 20 . PHP script 2 SQL- SELECT * FROM myTable LIMIT 20,40, 20 40. , ?
PHP script, COUNT (*)/rowsPerPage , . LIMIT lastLimit, TotalRowCount.
: , , , 1. TotalRows/MaxPerPage = howManyPagesNeeded
2 :
, , , .
- SQL_CALC_FOUND_ROWS
SQL_CALC_FOUND_ROWS
:
SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name WHERE id > 100 LIMIT 10; SELECT FOUND_ROWS();
, 10 , , , .
, FOUND_ROWS() MySQL
FOUND_ROWS()
In fact, if you populate a webpage with PHP, it’s easier to just get the MySQL table, put the data into an array and use a for-loop to loop through data like this
for($i=0;$i<count($arrayvar);$i++){}
With even a simple template engine, you can declare a block inside another block and parse all the data in one pipeline line.
Hope the type of answer you are looking for. I do not quite understand what you mean by your question, but I think it is.
Source: https://habr.com/ru/post/1704920/More articles:лучший способ представить эту таблицу поиска в С# - c#Search design template to create - c #ProgressBar does not update when switching to maximum binding - progress-barКак сообщить о прогрессе при выполнении выражения LINQ в наборе данных большого числа - linqHow to handle writing an XML file as GString and groovy still “rate” it? - groovyCSS Parser in ColdFusion or Java? - javaDon't change the color of the header text In Gridview with CSS - c #"Sys" undefined "error when starting an ASP.NET MVC application in IIS7 - asp.net-mvcHow can you select text in a div (for copy to clipboard) from javascript? - javascriptHow can I follow and fully report on a chain of symbolic links? - javaAll Articles