PHP 分页:上一个和下一个按钮可以工作,但表格不会显示

问题描述 投票:0回答:2

我正在关注互联网上一些使用 ODBC 进行 PHP 分页的示例,并根据我自己的使用情况进行更改。

我已经成功做到了

NEXT
并且
PREVIOUS
成功了。单击
NEXT
PREVIOUS
时,页面的 URL 会发生变化。例如,当我单击
NEXT
时,这是 URL
localhost/mypage.php?page=2

这里的问题是数据表不会显示。

这是我的代码:

function inv_rows($r1)  
{
ob_start(); 
(int)$number = odbc_result_all($r1);
ob_clean(); 
return $number;
}

$page = isset($_GET["page"]) ? $_GET["page"] : 1;  

if(empty($page)){$page = 1; 
}           
$query = "SELECT UserId, UserNm FROM User"; 
$num_result = odbc_exec($db, $query);
$numrows = inv_rows($num_result);   

$limit = 10;
$offset = ($page - 1) * $limit;

$sql = odbc_exec($conn,"SELECT UserId, UserNm FROM User LIMIT $limit OFFSET $offset"); 


$result = odbc_exec($db, $sql);

echo "<table style='width: 600;'>";

while(odbc_fetch_row($result))
{
echo "<tr>
<td style='width: 300; height: 15px;>";
echo odbc_result_all($result, "UserId");
echo "</td>
<td style='width: 300; height: 15px;'>";
echo odbc_result_all($result, "UserNm");
echo "</td>
</tr>";
}

echo "</table>";
if($page !=1)
{
$pageprev = $page - 1;
echo "<a href='paging2.php?page=$pageprev' style='text-decoration: none'>&nbsp; PREVIOUS&nbsp;</a>";
}
else
{ 
echo "&nbsp; PREVIOUS &nbsp;"; 
}

if(($numrows - ($limit * $page)) > 0)
{
$pagenext = $page + 1;
echo "<a href='paging2.php?page=$pagenext' style='text-decoration: none'>NEXT</a>"; }
else
{ 
echo "&nbsp; NEXT &nbsp;"; 
}

odbc_free_result($result);            
exit;
?> 

我确信我错过了什么。请指导我。谢谢你。

php pagination
2个回答
0
投票
You must try and use the $offset variable in your query.

<?php
function inv_rows($r1)  
{
ob_start(); 
(int)$number = odbc_result_all($r1);
ob_clean(); 
return $number;
}

$page = isset($_GET["page"]) ? $_GET["page"] : 1;  

if(empty($page)){$page = 1; 
}           
$query = "SELECT UserId, UserNm FROM User"; 
$num_result = odbc_exec($db, $query);
$numrows = inv_rows($num_result);   

$limit = 10;
$offset = ($page - 1) * $limit;

$sql = "SELECT UserId, UserNm FROM User LIMIT 10 OFFSET $offset"; 


$result = odbc_exec($db, $sql);

echo "<table style='width: 600;'>";

while(odbc_fetch_row($result))
{
echo "<tr>
<td style='width: 300; height: 15px;>";
echo odbc_result_all($result, "UserId");
echo "</td>
<td style='width: 300; height: 15px;'>";
echo odbc_result_all($result, "UserNm");
echo "</td>
</tr>";
}

echo "</table>";
if($page !=1)
{
$pageprev = $page - 1;
echo "<a href='paging2.php?page=$pageprev' style='text-decoration: none'>&nbsp; PREVIOUS&nbsp;</a>";
}
else
{ 
echo "&nbsp; PREVIOUS &nbsp;"; 
}

if(($numrows - ($limit * $page)) > 0)
{
$pagenext = $page + 1;
echo "<a href='paging2.php?page=$pagenext' style='text-decoration: none'>NEXT</a>"; }
else
{ 
echo "&nbsp; NEXT &nbsp;"; 
}

odbc_free_result($result);            
exit;
?> 

0
投票
  1. while 函数中的代码具有原始 HTML,并为 PHP 代码打开
    <?php
    脚本标签。 由于您已经在 PHP 上下文中,所以应该是相反的方式 - 不需要打开和关闭
    <?php
    标签,并且需要使用 PHP 的
    echo
    命令打印 HTML。
  2. <table>
    标签应该位于 while 函数之外。 您不想为每一行打印一个表格。

更新:

  1. $conn
    的定义正确吗?
    $db
    的定义正确吗?
  2. $result
    实际上包含您感兴趣的行吗?

这应该打印表格的标题行。 如果没有打印行数据,请检查您的查询结果。

echo "<table style='width: 600;'>";
echo "<thead><tr><th>User ID</th><th>User Name</th></tr></thead><tbody>";

while(odbc_fetch_row($result))
{
    echo "<tr><td style='width: 300; height: 15px';>".odbc_result($result, 'UserId')."</td><td style='width: 300; height: 15px;'>".odbc_result($result, 'UserNm')."</td></tr>";
}

echo "</tbody></table>";
© www.soinside.com 2019 - 2024. All rights reserved.