分页计算

pageIndex:第几页-从1开始

pageSize:每页多少条数

totalCount:总共多少条数

totalPage:总页数

mysql limit分页

语法:Limit x,y。指筛选出结果的第x行后的y行。如果x不填也是可以的,默认为0
limit (pageIndex-1)*pageSize,pageSize

select * from test limit 0,10
查询从第0行开始后的10条记录

js 对数组分页

例子:

1
2
3
4
5
6
7
8
9
10
11
12
var pageIndex=2;
var pageSize=3;
var arr=[1,2,3,4,5,6,7,8,9,10];
var totalCount=arr.length;
var totalPage=(totalCount + pageSize - 1)/pageSize;
if(pageIndex>totalPage) {
return;
}
var start=(pageIndex-1)*pageSize
var end=start+pageSize;
end=end>totalCount?totalCount:end;
alert(arr.slice(start,end));

输出:
4,5,6

java对数组、list分页

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
String[] arr=new String[]{"1","2","3","4","5","6","7","8","9","10"};
int pageIndex=3;
int pageSize=3;
int totalCount=arr.length;
int totalPage=(totalCount + pageSize - 1)/pageSize;
System.out.println("总页数:"+totalPage);
if(pageIndex>totalPage) {
//java中下标越界会报错
System.out.print("不能超过总页数");
return;
}
int start=(pageIndex-1)*pageSize;
int end=start+pageSize-1;
end=end>=totalCount?totalCount-1:end;
for(int i=start;i<=end;i++) {
System.out.print(arr[i]);
if(i<end) {
System.out.print(",");
}
}

输出:
总页数:4
7,8,9

-------------本文结束感谢您的阅读-------------