Find the page Field id of the current id: value 1 ~ 27
Pagination: eight data entries are displayed on each page.
Find the number of pages where this id is located
My ideas:
$ Arr = array (); for (int I = 0; I
Db-> getAll ($ SQL);} $ arr = {[0] => (1 ~ 8) [1] => (9 ~ 16) [2] => (17 ~ 24) [3] => (25 ~ 27)} compare the id with the value of the array $ arr. if the value is the same, + 1 is the page where the key is obtained.
Reply to discussion (solution)
If the id is continuous and the page is generated in ascending order of id
$ Page number = ceil ($ id/8 );
If the id is continuous and the page is generated in ascending order of id
$ Page number = ceil ($ id/8 );
The id is not consecutive and the user may delete the data or be filtered by the where clause.
Is there any other method?
Discontinuous? You need to know the id distribution.
Discontinuous? You need to know the id distribution.
Id distribution?
IDs are not consecutive.
You can also check all database IDs, compare values, and obtain keys. ceil (key + 1)/8) is the page number of the database.
If there is too much data, it will be slow.
If an id is known, calculate the number of pages and locations of the id in the positive Order of the discontinuous id (assuming that each page is divided into X)
Eg
1: known id = 5 id {1, 2, 5, 9, 10}, X = 2 so the known id is in the first position of the second page
2: known id = 9 id {1, 2, 3, 7, 9, 10} X = 2 so the known id is in the first position of the third page
This can only be used to retrieve all IDs,
And then numbered in order,
Then obtain the id of the specified id,
$ Page number = ceil ($ No./8 );
With the id distribution, you can calculate it.
// Known id = 5 id {1, 2, 5, 9, 10}, X = 2 so the known id is in the first position of the second page $ id = 5; $ a = array (1, 2, 5, 9, 10); $ x = 2; $ p = array_search ($ id, $ a); echo ceil ($ p/$ x) + 1; // 2 // known id = 9 id {1, 2, 3, 7, 9, 10} X = 2 so the known id is located at the first position on the third page $ id = 9; $ a = array (1, 2, 5, 9, 10); $ x = 2; $ p = array_search ($ id, $ a); echo ceil ($ p/$ x) + 1; // 3
If the id is out of order, it can also be calculated.
$id = 9;$a = array(1,5,9,2,10);$x = 2;$p = array_search($id, $a);echo ceil($p / $x) + 1; //2
This can only be used to retrieve all IDs,
And then numbered in order,
Then obtain the id of the specified id,
$ Page number = ceil ($ No./8 );
This method is not good if there is too much data
Or I can find the first page of the maximum page ~ Obtain the first id on the maximum page. compare the known id with the first id to find the page number.
This can only be used to retrieve all IDs,
And then numbered in order,
Then obtain the id of the specified id,
$ Page number = ceil ($ No./8 );
This method is not good if there is too much data
Or I can find the first page of the maximum page ~ Obtain the first id on the maximum page. compare the known id with the first id to find the page number.
The key is your requirement. id is not continuous. how do you know the first id of each page?
Sequential numbers are required,
But simply put, only select 0 ~ If the id is specified, it will be OK.
Id
1. query all IDs at a time.
2. from the first page to the largest Page
Determine whether the first id on the first page is <已知id 是的话第二页第一个id 是否<已知id ........
In this way, the data found seems to be less, but the db is used more often.
I still use 1 to implement the function first. if there is a good way to tell me, let me refer to it.
To query the database, you can do this.
Select id, ceil (@ s/number of rows per page) as page, @ s: = @ s + 1 from table, (select @ s: = 1) t HAVING id = $ id
The page column in the query result is the page number corresponding to the id.
A problem.
Select id, page from (select id, ceil (@ s/number of lines per page) as page, @ s: = @ s + 1 from table, (select @ s: = 1) t) t1 where id = $ id
A problem.
Select id, page from (select id, ceil (@ s/number of lines per page) as page, @ s: = @ s + 1 from table, (select @ s: = 1) t) t1 where id = $ id
@ Is generally used to indicate a defined variable.
": =" Is mainly used to pass external parameters to assign values to variables in the statement.
@ S: = @ s + 1 @ s: = is the value of the 1 variable assigned twice?
(Select @ s: = 1) t) t1 t, t1 does not understand
This kind of skillful writing should be understood.
The normal method is
Set @ s: = 1; select id, ceil (@ s/number of lines per page) as page, @ s: = @ s + 1 from table
However, to prevent SQL attacks, php cannot execute two or more commands at the same time. Therefore, Cartesian connections are used.
(Select @ s: = 1) t is only used to assign an initial value to the user variable @ s