【问题标题】:PHP Pagination : Prev and Next button work but table won't show upPHP分页:上一个和下一个按钮工作,但表格不会显示
【发布时间】:2017-02-20 13:02:39
【问题描述】:

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

我设法使NEXTPREVIOUS 工作。单击NEXTPREVIOUS 时,页面的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;
?> 

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

【问题讨论】:

  • this SO 问题的可能重复项。
  • 基于 thisthis 我将代码更改为 $sql = "SELECT UserId, UserNm FROM User LIMIT 10 OFFSET (SELECT COUNT(*) FROM User)-10"; 。该表仍然不会出现。 @JeroenHeier

标签: php paging


【解决方案1】:
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;
?> 

【讨论】:

  • 我试试 $sql = "SELECT UserId, UserNm FROM User ORDER BY UserId ASC LIMIT " . $limit . " OFFSET " . $offset;" " ;$sql = "SELECT UserId, UserNm FROM User LIMIT 10 OFFSET $offset" 。然而,桌子仍然没有出现。 @SaurabhMore
  • 知道@SaurabhMore 吗?
【解决方案2】:
  1. while 函数中的代码具有原始 HTML,并为 PHP 代码打开 &lt;?php 脚本标签。由于您已经在 PHP 上下文中,因此应该相反 - 无需打开和关闭 &lt;?php 标签,并且需要使用 PHP 的 echo 命令打印 HTML。
  2. &lt;table&gt; 标签应该在您的 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>";

【讨论】:

  • 你的意思是echo $result;? @user5855223
  • 好吧,你可以这样做。它会打印您的结果,但它可能看起来不像您希望的那样漂亮...您需要打印一些 html 以及它们所属的$result 中的属性。
  • 对不起@user5855223,我确实回应了他们。查看我更新的代码:)
  • 我明白了。我更改了我的代码,但表格仍然不会显示。我想知道我是否错误地实现了 $offset$limit 。有什么想法@ user5855223?
  • 按照上面的评论执行echo $result;。这给了你什么?
猜你喜欢
  • 1970-01-01
  • 2018-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多