分页后无法正常工作

时间:2019-08-30 11:46:13

标签: php html

然后我将创建一个分页

b

我总共有240行:

$num_row = mysqli_num_rows($res);

我想先1 2 3 4 5然后下一个按钮,所以我尝试了

if($num_row>5){
    $count=0;
    $index=1;
    while($count < $num_row && $index < 5){ ?>
        <button id="<?php echo $count; ?>" class="index_b"><?php echo $index; ?></button>
    <?php
        $count = $count + 5;
        $index++;
    }
}

现在我的问题是第4个分页号之后,没有数字可用。

2 个答案:

答案 0 :(得分:1)

请勿使用mysqli_num_rows()!理想情况下,应仅提取所需的数据,然后运行2个独立的查询。一种用于获取全部计数,一种用于获取每页的项目:

$db = new PDO(...);

$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
$offset = ($page-1) * 50;
$count = $db->query("SELECT COUNT(*) FROM table")->fetchColumn();

$trans = $db->prepare("SELECT * FROM table LIMIT :offset, 50");
$trans->bindValue(':offset', $offset, PDO::PARAM_INT);
$trans->execute();


while ($row = $trans->fetchRow()) {
   // echo data
}

//Add footer here. Use $page variable and $count to determine how many pages are available.

答案 1 :(得分:0)

import { Component } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';


@Component({
  selector: 'about',
  templateUrl: './about.component.html'
})

export class About{
  title: string = "About Page.";
    public href: String = '';

    constructor(private router: Router, private route:ActivatedRoute)
{ }
  openNewTab() {
    window.open('' + this.href + '?show=false', '_blank').focus();
    document.getElementById('navigation').outerHTML = '';
  }

  ngOnInit() {
    this.href = decodeURI(this.router.url.replace(/\/.*\//g, ''));
    this.route.queryParams.subscribe(params => {
      if (params.show === 'false') {
        document.getElementById('navigation').outerHTML = '';
      }
    });
  }
}

这种情况意味着当$ index等于5时,循环就停止了

因此,永远不会显示带有5的按钮。

尝试使用小于或等于,而不是使用小于

    while($count < $num_row && $index < 5){ ?>

或更妙的是,切换到@FrankerZ的解决方案

相关问题