如何在PHP / SQL中添加分页

时间:2015-08-07 09:00:19

标签: php sql oracle pagination

CGSize kbSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    NSTimeInterval duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    CGFloat height = UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]) ? kbSize.width : kbSize.height;

    height += 10.0;//it needs to be slightly higher so the textField is clearly visible.
    originalEdgeInset = self.contentInset;
    __weak typeof (self) block = self;
    [UIView animateWithDuration:duration animations:^{
        UIEdgeInsets edgeInsets = block.contentInset;
        edgeInsets.bottom += height;
        block.contentInset = edgeInsets;
        edgeInsets = block.scrollIndicatorInsets;
        edgeInsets.bottom += height;
        block.scrollIndicatorInsets = edgeInsets;
    }];

我使用PHP和SQL(oracle)DB来获取详细信息。任何人都可以共享细节,在我的页面中添加一个寻呼机,每页只显示10个项目。

$query = "SELECT DISTINCT TITLE, PID, TYPE, SUM(DAYCOUNT) AS tot, ROUND(SUM(DAYCOUNT)/(
        SELECT SUM(DAYCOUNT) FROM REPORT_LIST_VIEW), 4) AS per
        FROM REPORT_LIST_VIEW
        WHERE DAYCOUNT > '0'
        GROUP BY TITLE, PID, TYPE
        ORDER BY TITLE ASC";
        $res = db_query($query) // drupal 7;

2 个答案:

答案 0 :(得分:1)

<强>的MySQL

$query = "SELECT DISTINCT TITLE, PID, TYPE, SUM(DAYCOUNT) AS tot, ROUND(SUM(DAYCOUNT)/(
SELECT SUM(DAYCOUNT) FROM REPORT_LIST_VIEW), 4) AS per
FROM REPORT_LIST_VIEW
WHERE DAYCOUNT > '0'
GROUP BY TITLE, PID, TYPE
ORDER BY TITLE ASC
LIMIT $start, 10
";

现在将$ start设置为10 * $pageNo

实施例: 上面的sql代码。 PHP代码:

// show activ page + 5 pages before active page and +5 after that page.
// all in all 11 page numbers:
$maxpagenumber = ...; // thats the tricky part. see below.
$activepage = (int)$_GET['page'];
$startWith = max($activepage-5,1);
$endsWith = min($activepage+5,$maxpagenumber);

// output
echo "first page | prev | ";
for($i=$startswith;$i<=$endsWith;$i++)
  echo $i." | ";
echo "next | last page";

您必须添加上述页面的链接。在每个链接中,您将参数page附加到($i-1)

现在是带有$ maxpagenumber的部分:在sql语句中使用SQL_CALC_FOUND_ROWS。

多数民众赞成:)

答案 1 :(得分:0)

Oracle Pre-12c发布

您可以将子查询中的ROWNUM用作 pagination query

例如,

SQL> SELECT empno, sal
  2  FROM   (SELECT empno, sal, ROWNUM AS rnum
  3          FROM   (SELECT empno, sal
  4                  FROM   emp
  5                  ORDER BY sal)
  6          WHERE ROWNUM <= 8)
  7  WHERE  rnum >= 5;

     EMPNO        SAL
---------- ----------
      7654       1250
      7934       1300
      7844       1500
      7499       1600

SQL>

Oracle 12c发布

您可以使用前N行限制功能。

例如,

SQL> SELECT empno, sal
  2  FROM   emp
  3  ORDER BY sal
  4  OFFSET 4 ROWS FETCH NEXT 4 ROWS ONLY;

     EMPNO        SAL
---------- ----------
      7654       1250
      7934       1300
      7844       1500
      7499       1600

SQL>