我正在尝试创建分页页面,但我已经卡在这里:
<?php
global $limit, $sql, $url, $pagesize, $pagecount, $absolutepage, $recordcount;
// Configuration...
$db = pg_Connect("dbname=Test user=postgres password=1111");
// If current page number, use it
// if not, set one!
if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}
// Define the number of results per page
$max_results = 10;
// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);
// Perform MySQL query on only the current page number's results
$sql = pg_query("SELECT * FROM import.mock_data LIMIT $from OFFSET $max_results"); //LIMIT $pagesize OFFSET $offset
while($row = pg_fetch_array($sql)){
// Build your formatted results here.
echo $row['first_name']."<br />";
}
// Figure out the total number of results in DB:
$total_results = pg_result(pg_query("SELECT COUNT(*) as Num FROM import.mock_data"),0);
// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);
// Build Page Number Hyperlinks
echo "<center>Selecione uma página<br />";
// Build Previous Link
if($page > 1){
$prev = ($page - 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> ";
}
for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo "$i ";
} else {
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
}
}
// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";
}
echo "</center>";
?>
此时我有这个问题: 1-在开始时不显示任何内容,只在第2页和第2页上显示... 2 - 它不是专用的,而是将第3页的下十条记录添加到第2页的前10条记录中。
答案 0 :(得分:1)
问题在于第一个SELECT
查询
$sql = pg_query("SELECT * FROM import.mock_data LIMIT $from OFFSET $max_results");
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
当您从一个页面跳到另一个页面时,使用当前逻辑,您的OFFSET
将相同,LIMIT
将从0,10,20增加,依此类推。而实际上它应该是另一种方式。因此,您的SELECT
查询应该是这样的:
$sql = pg_query("SELECT * FROM import.mock_data LIMIT $max_results OFFSET $from");
答案 1 :(得分:0)
你有太多错误。
首先你必须像这样计算偏移量
if(isset($_GET['page']))
{
$page = $_GET{'page'} + 1;
$from = $max_results * $page ;
}
else
{
$page = 0;
$from = 0;
}
请点击此链接 https://www.tutorialspoint.com/php/mysql_paging_php.htm