小编典典

分页显示SQL数据的相同部分

sql

我有一个来自SQL的非常大的数据集,需要分页。

我的分页代码有问题。该代码 确实 在URL 显示了页码,并且 确实
在表格底部提供了分页超链接。但是,我单击的任何页面都将输出sql数据表的相同部分。

另外,我也在wordpress中这样做。

// define how many results you want per page
$results_per_page = 10;

// find out the number of results stored in database
$sql='SELECT * FROM ETF';
$result = mysqli_query($con, $sql);
$number_of_results = mysqli_num_rows($result);

// determine number of total pages available
$number_of_pages = ceil($number_of_results/$results_per_page);

// determine which page number visitor is currently on
if (!isset($_GET['page'])) {
  $page = 1;
} else {
  $page = $_GET['page'];
}

// determine the sql LIMIT starting number for the results on the displaying page
$this_page_first_result = ($page-1)*$results_per_page;

// retrieve selected results from database and display them on page
$sql='SELECT * FROM ETF LIMIT ' . $this_page_first_result . "," .$results_per_page;

$result = mysqli_query($con, $sql);

while($row = mysqli_fetch_array($result)) {
  echo $row['ETF'] . ' ' . $row['ETF NAME']. '<br>';
}


// display the links to the pages
for ($page=1;$page<=$number_of_pages;$page++) {
  echo '<a href="index.php/stocks/sec-forms/?page=' . $page . '">' . $page. '</a>';
}

阅读 182

收藏
2021-04-15

共1个答案

小编典典

尝试:

$sql='SELECT * FROM ETF LIMIT ' . $results_per_page . ' OFFSET ' . $this_page_first_result;

另外,如前所述,您应该使用“ ORDER BY”对某列进行排序,以获得一致的结果。

2021-04-15