使用 PHP 显示“下一页” “前一个”在分页中

发布于 2024-12-04 13:43:18 字数 968 浏览 3 评论 0原文

我正在使用以下代码:

   $result = mysql_query("SELECT * FROM table LEFT JOIN table2 
   ON table.field = table2.field WHERE ( table.field = '$pid' ) 
   AND ( table.field5 LIKE '%$q%' OR table.field3 LIKE '%$q%' 
    OR table2.field2 LIKE '%$q%' )");


     if (empty($what)) {

      $countpls = "0";
      } else {

     $countpls = mysql_num_rows($result);
      }


<?php
if ($countpls > 10) {
    echo '<a id=pgnvg href="' . $_SERVER['PHP_SELF'] . '?pg=' . ($startrow + 20) . '&q=' . ($what) . '">Next</a>';
} else {
    echo "";
}

$prev = $startrow - 20;

//only print a "Previous" link if a "Next" was clicked
if ($prev >= 0) {
    echo '<a id=pgnvg2 href="' . $_SERVER['PHP_SELF'] . '?pg=' . $prev . '&q=' . ($what) . '">Previous</a>';
} else {
    echo "";
}
?>

我希望仅当有更多条目要显示时才显示下一个,只有当有更多条目要循环返回时才显示上一个。尽管没有更多结果可显示,但它在第一页上工作,然后在最后一页下一页显示。

我尝试添加“其他”,但它仍然不起作用。

有什么想法吗?

I am using the following code:

   $result = mysql_query("SELECT * FROM table LEFT JOIN table2 
   ON table.field = table2.field WHERE ( table.field = '$pid' ) 
   AND ( table.field5 LIKE '%$q%' OR table.field3 LIKE '%$q%' 
    OR table2.field2 LIKE '%$q%' )");


     if (empty($what)) {

      $countpls = "0";
      } else {

     $countpls = mysql_num_rows($result);
      }


<?php
if ($countpls > 10) {
    echo '<a id=pgnvg href="' . $_SERVER['PHP_SELF'] . '?pg=' . ($startrow + 20) . '&q=' . ($what) . '">Next</a>';
} else {
    echo "";
}

$prev = $startrow - 20;

//only print a "Previous" link if a "Next" was clicked
if ($prev >= 0) {
    echo '<a id=pgnvg2 href="' . $_SERVER['PHP_SELF'] . '?pg=' . $prev . '&q=' . ($what) . '">Previous</a>';
} else {
    echo "";
}
?>

I want the next to show only if there are more entries to show and previous only if there are more entries to circle back to. It works on the first page bt then on the last page Next shows despite teh fact that there are no more results to show.

I tried adding the 'else' but its still not working.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嘦怹 2024-12-11 13:43:19

我认为如果您按照教程掌握基本概念会对您有好处。它甚至附带了一个示例,可以 1.) 替换当前的分页或 2.) 修复它。

http://www.phpfreaks.com/tutorial/basic-pagination

I think it would do you good if you followed a tutorial to grasp the basic concepts. It even comes with the example that could either 1.) replace your current pagination or 2.) fix it.

http://www.phpfreaks.com/tutorial/basic-pagination

尘世孤行 2024-12-11 13:43:18
if($countpls > 0){
    $pg = $_POST['pg']?$_POST['pg']:1;

    //if it's not the first page...
    if($pg>1){
        echo '<a id="pgnvg" href="'.$_SERVER['PHP_SELF'].'?pg='.($pg-1).'&q='.$what.'">Previous</a>';
    }
    //if you have more registers to show...
    if(($countpls-(($pg-1)*10))>10){
     echo '<a id="pgnvg" href="'.$_SERVER['PHP_SELF'].'?pg='.($pg+1).'&q='.$what.'">Next</a>';
    }
}

为了计算在查询中使用的偏移量,请使用以下命令:

$offset = ($_POST['pg']-1)*10;
if($countpls > 0){
    $pg = $_POST['pg']?$_POST['pg']:1;

    //if it's not the first page...
    if($pg>1){
        echo '<a id="pgnvg" href="'.$_SERVER['PHP_SELF'].'?pg='.($pg-1).'&q='.$what.'">Previous</a>';
    }
    //if you have more registers to show...
    if(($countpls-(($pg-1)*10))>10){
     echo '<a id="pgnvg" href="'.$_SERVER['PHP_SELF'].'?pg='.($pg+1).'&q='.$what.'">Next</a>';
    }
}

In order to calculate your offset to use in queries, use this:

$offset = ($_POST['pg']-1)*10;
迷爱 2024-12-11 13:43:18

如果您提供设置 $countpls 的代码,将会有所帮助。这可能是导致问题的部分。另外,其他的都是不必要的。不过,试试这个:

if($countpls - $startrow > 20)
{
    echo '<a id=pgnvg href="'.$_SERVER['PHP_SELF'].'?pg='.($startrow+20).'&q='.($what).'">Next</a>';
}

It would help if you would provide the code that's setting $countpls. That might be the part that's causing the problem. Also, the else's are unnecessary. However, try this:

if($countpls - $startrow > 20)
{
    echo '<a id=pgnvg href="'.$_SERVER['PHP_SELF'].'?pg='.($startrow+20).'&q='.($what).'">Next</a>';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文