【问题标题】:PHP Pagination DisplayPHP分页显示
【发布时间】:2014-06-17 13:19:10
【问题描述】:

我正在使用引导分页和 PHP/mysql 组合方法来显示我的结果(例如,results.php?results=1 每个页面)。这工作正常,但实际上在我的页面底部显示分页 div,即哪个是活动的,您是否可以单击以下或前面的项目(如果这些页面上确实有结果)

所以我已经为几乎所有我能想到的会影响任何事情的组合做了一个具体的案例,但它肯定不是有效的。任何人都可以提出更好的方法吗?有些情况会很明显,比如如果我在第一页,我不想在分页中显示第 0 页和 -1,其他情况不太明显,比如我在第 3 页,并且有没有更多的页面,我希望第 3 页在中间,因为显示了 5 个数字,但是您应该无法点击 4 和 5。

我正在使用显示here 的插件的精确副本,除了我在第一页/最后一页之外添加了左右增加/减少一个。

    <div id="pagination" style="width: 340px; margin-left: auto; margin-right: auto;">
<? if ($results == 1) {   ?>    
<ul class="pagination">
  <li class="disabled"><a href="#">&nbsp;<i class="fa fa-lg fa-angle-double-left"></i>&nbsp;</a></li>
  <li class="disabled"><a href="#">&nbsp;<i class="fa fa-lg fa-angle-left"></i>&nbsp;</a></li>
  <li class="active"><a href="#"><? echo $results; ?> <span class="sr-only">(current)</span></a></li>
  <li <?if ($num_rows < $num_res) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 1); ?>"><? echo $results + 1; ?></a></li>
  <li <?if ($num_rows < ($num_res * 2)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 2); ?>"><? echo $results + 2; ?></a></li>
  <li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 3); ?>"><? echo $results + 3; ?></a></li>
  <li <?if ($num_rows < ($num_res * 4)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 4); ?>"><? echo $results + 4; ?></a></li>
  <li <?if ($num_rows < ($num_res)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 1); ?>">&nbsp;<i class="fa fa-lg fa-angle-right"></i>&nbsp;</a></li>
  <li <?if ($num_rows < ($num_res)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($last_page); ?>">&nbsp;<i class="fa fa-lg fa-angle-double-right"></i>&nbsp;</a></li>
</ul>
  <? } ?>
  <? if ($results == 2) { ?>    
<ul class="pagination">
  <li><a href="<? echo $url."&results=".(1); ?>">&nbsp;<i class="fa fa-lg fa-angle-double-left"></i>&nbsp;</a></li>
  <li><a href="<? echo $url."&results=".($results - 1); ?>">&nbsp;<i class="fa fa-lg fa-angle-left"></i>&nbsp;</a></li>
  <li><a href="<? echo $url."&results=".($results - 1); ?>"><? echo $results - 1; ?></a></li>
  <li class="active"><a href="<? echo $url."&results=".($results); ?>"><? echo $results; ?> <span class="sr-only">(current)</span></a></li>
  <li <?if ($num_rows < ($num_res * 2)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 1); ?>"><? echo $results + 1; ?></a></li>
  <li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 2); ?>"><? echo $results + 2; ?></a></li>
  <li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 3); ?>"><? echo $results + 3; ?></a></li>
  <li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 1); ?>">&nbsp;<i class="fa fa-lg fa-angle-right"></i>&nbsp;</a></li>
  <li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($last_page); ?>">&nbsp;<i class="fa fa-lg fa-angle-double-right"></i>&nbsp;</a></li>
</ul>
  <? } ?>
  <? if (($results == 3) && (($last_page == 3))) { ?>   
<ul class="pagination">
  <li><a href="<? echo $url."&results=".(1); ?>">&nbsp;<i class="fa fa-lg fa-angle-double-left"></i>&nbsp;</a></li>
  <li><a href="<? echo $url."&results=".($results - 1); ?>">&nbsp;<i class="fa fa-lg fa-angle-left"></i>&nbsp;</a></li>
  <li><a href="<? echo $url."&results=".($results - 2); ?>"><? echo $results - 2; ?> </a></li>
  <li><a href="<? echo $url."&results=".($results - 1); ?>"><? echo $results - 1; ?></a></li>
  <li class="active"><a href="<? echo $url."&results=".($results); ?>"><? echo $results; ?><span class="sr-only">(current)</span></a></li>
  <li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 1); ?>"><? echo $results + 1; ?></a></li>
  <li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 2); ?>"><? echo $results + 2; ?></a></li>
  <li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 1); ?>">&nbsp;<i class="fa fa-lg fa-angle-right"></i>&nbsp;</a></li>
  <li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($last_page); ?>">&nbsp;<i class="fa fa-lg fa-angle-double-right"></i>&nbsp;</a></li>
</ul>
  <? } ?>
    <? if (($results > 2) && ($results < ($last_page - 1))) { ?>    
<ul class="pagination">
  <li><a href="<? echo $url."&results=".(1); ?>">&nbsp;<i class="fa fa-lg fa-angle-double-left"></i>&nbsp;</a></li>
  <li><a href="<? echo $url."&results=".($results - 1); ?>">&nbsp;<i class="fa fa-lg fa-angle-left"></i>&nbsp;</a></li>
  <li><a href="<? echo $url."&results=".($results - 2); ?>"><? echo $results - 2; ?> </a></li>
  <li><a href="<? echo $url."&results=".($results - 1); ?>"><? echo $results - 1; ?></a></li>
  <li class="active"><a href="<? echo $url."&results=".($results); ?>"><? echo $results; ?><span class="sr-only">(current)</span></a></li>
  <li><a href="<? echo $url."&results=".($results + 1); ?>"><? echo $results + 1; ?></a></li>
  <li><a href="<? echo $url."&results=".($results + 2); ?>"><? echo $results + 2; ?></a></li>
  <li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 1); ?>">&nbsp;<i class="fa fa-lg fa-angle-right"></i>&nbsp;</a></li>
  <li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($last_page); ?>">&nbsp;<i class="fa fa-lg fa-angle-double-right"></i>&nbsp;</a></li>
</ul>
  <? } ?>
    <? if (($results > 2) && ($results == ($last_page - 1))) { ?>   
<ul class="pagination">
  <li><a href="<? echo $url."&results=".(1); ?>">&nbsp;<i class="fa fa-lg fa-angle-double-left"></i>&nbsp;</a></li>
  <li><a href="<? echo $url."&results=".($results - 1); ?>">&nbsp;<i class="fa fa-lg fa-angle-left"></i>&nbsp;</a></li>
  <li><a href="<? echo $url."&results=".($results - 3); ?>"><? echo $results - 3; ?></a></li>
  <li><a href="<? echo $url."&results=".($results - 2); ?>"><? echo $results - 2; ?></a></li>
  <li><a href="<? echo $url."&results=".($results - 1); ?>"><? echo $results - 1; ?></a></li>
  <li class="active"><a href="<? echo $url."&results=".($results ); ?>"><? echo $results ?><span class="sr-only">(current)</span></a></li>
  <li><a href="<? echo $url."&results=".($results + 1); ?>"><? echo $results + 1; ?></a></li>
  <li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 1); ?>">&nbsp;<i class="fa fa-lg fa-angle-right"></i>&nbsp;</a></li>
  <li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($last_page); ?>">&nbsp;<i class="fa fa-lg fa-angle-double-right"></i>&nbsp;</a></li>
</ul>
  <? } ?>
   <? if (($results > 2) && ($results == $last_page) && ($results != 3)){ ?>    
<ul class="pagination">
  <li><a href="<? echo $url."&results=".(1); ?>">&nbsp;<i class="fa fa-lg fa-angle-double-left"></i>&nbsp;</a></li>
  <li><a href="<? echo $url."&results=".($results - 1); ?>">&nbsp;<i class="fa fa-lg fa-angle-left"></i>&nbsp;</a></li>
  <li><a href="<? echo $url."&results=".($results - 4); ?>"><? echo $results - 4; ?> </a></li>
  <li><a href="<? echo $url."&results=".($results - 3); ?>"><? echo $results - 3; ?></a></li>
  <li><a href="<? echo $url."&results=".($results - 2); ?>"><? echo $results - 2; ?></a></li>
  <li><a href="<? echo $url."&results=".($results - 1); ?>"><? echo $results - 1; ?></a></li>
  <li class="active"><a href="<? echo $url."&results=".($results ); ?>"><? echo $results ?><span class="sr-only">(current)</span></a></li>
  <li class="disabled"><a href="#">&nbsp;<i class="fa fa-lg fa-angle-right"></i>&nbsp;</a></li>
  <li class="disabled"><a href="#">&nbsp;<i class="fa fa-lg fa-angle-double-right"></i>&nbsp;</a></li>
</ul>
  <? } ?>
</div>

如果没有人愿意在第一部分提供帮助,我理解,就我所知,它可以正常工作。

但我也想知道是否有办法在我的class="disabled" 上删除href?还是我只需要对每个人都添加一个 if 语句?谢谢。

编辑:基于@Scopey 的建议。我更改了一些我认为合适的东西,以及几个变量名。

$numberOfPages = ceil($num_rows / $num_res);
$numberOfPages = (int)$numberOfPages;

$url = explode('&', $_SERVER['REQUEST_URI']);
function myFilter($string) {
return strpos($string, 'results=') === false;
}

$url = array_filter($url, 'myFilter');
$url = implode('&', $url);

?> 
<ul class="pagination">
     <li<?php if($results === $numberOfPages): ?> class="disabled"<?php endif; ?>><a href="<?php
if($results !== $numberOfPages){
    echo $url.'&results=' . 1;
} else { echo '#'; } 
?>">&nbsp;<i class="fa fa-lg fa-angle-double-left"></i>&nbsp;</a></li>
    <li<?php if($results === 1): ?> class="disabled"<?php endif; ?>><a href="<?php
if($results !== 1){
    echo $url.'&results=' . ($results - 1);
} else { echo '#'; } 

?>">&nbsp;<i class="fa fa-lg fa-angle-left"></i>&nbsp;</a></li>

<?php

// Print the pagination...
// Minimum of 5 pages in the pagination, even if there aren't 5 pages...
$pageCount = ($numberOfPages < 5) ? 5 : $numberOfPages;

// Loop through from page 1 until the last page ($pageCount)
for($i = 1; $i <= $pageCount; $i++)
{
     // Echo out just the beginning of the <li> tag as we don't yet
     // know if this needs to be disabled...
     echo '<li';
     // Added to show if current page is active
     if($i === $results) {
          echo ' class="active"';
     }
     // Check if:
     //  - This current page is greater than the amount of pages of
     //    results we have
     //  - OR, this is the currently selected page
     if($i > $numberOfPages || $results === $i)
     {
          // <li> tag needs the disabled class....
          echo ' class="disabled"';
     }
     // Finish the <li> tag and start generating the link
     echo '><a href="';
     // Opposite logic from above... Only if this is a page we know about
     // AND this is not the current page.
     if($i < $numberOfPages && $results !== $i)
     {
          // The link will be your page, with the get param, and the
          // current page number we're printing
          echo $url.'&results=' . $i;
     }
     else
     {
          // Otherwise just a # (no link)
          echo '#';
     }
     // Finish this page... Print the page number ($i)
     echo '">' . $i . '</a></li>';
}
?>
    <li<?php if($results === $numberOfPages): ?> class="disabled"<?php endif; ?>><a href="<?php
if($results !== $numberOfPages){
    echo $url.'&results=' . ($results + 1);
} else { echo '#'; } 
?>">&nbsp;<i class="fa fa-lg fa-angle-right"></i>&nbsp;</a></li>
 <li<?php if($results === $numberOfPages): ?> class="disabled"<?php endif; ?>><a href="<?php
if($results !== $numberOfPages){
    echo $url.'&results=' . $numberOfPages;
} else { echo '#'; } 
?>">&nbsp;<i class="fa fa-lg fa-angle-double-right"></i>&nbsp;</a></li>

<?php

@Scobey,这很有效,符合预期,与我原来的帖子一样,除了几件事。在我的示例中,我能够在 .例如,3 | 4 | 5 >>,这就是为什么我有这么多不同的语句 +1 和 -1、+2、-2 等。

其次,我的意图是始终显示 5 个页面选项,而不仅仅是至少 5 个。所以如果我在第 6 页,我只会看到 6 | 7 | 8 >>,而不是 1-8。可能没有必要这样做,这很好。但是,我不想在分页中有超过 50 个不同的页码。

【问题讨论】:

  • 怎么会有人写出那样的代码呢?或阅读。
  • 这就是我在这里寻求帮助的原因。就阅读而言,它只是完全相同的 UL 中的 6 个,结果不同。
  • 好吧,你不需要几乎相同的块,一个函数\循环会将其减少到总共 6 行
  • 我知道它真的是拼凑起来的,因为我有点从头开始,并根据需要不断添加,但我不知道我怎么可能把它减少到 6 行。而且我不想争论,正如你从我的声誉中看到的那样,我不是专家。使用循环,我在这里看到太多可能的变量,改变页码,启用/禁用按钮。我试图让当前页面位于中间,除非您在第一页或第二页,或者倒数第二页或最后一页。如果我的分页只显示 3 个数字会更容易。
  • 您真的总是想要 5 个结果吗?所以没有超过 5 个或正好 1 个结果的案例?或者您是否选择了五个,因为它在引导程序的示例中?在大多数分页用例中,您只显示可用的页面。

标签: php twitter-bootstrap pagination


【解决方案1】:

通常动态分页仅在您有一些东西时才起作用

  1. 所有可能结果的计数(总计)
  2. 您希望每页显示的结果数量
  3. 当前页码
  4. 应显示在此页面上的结果信息。

假设您有一个名为 Results 的数据库表,它在分页页面上有一大堆您想要的结果

1.所有可能结果的计数

这可以通过一个简单的 SQL 语句来实现,例如:

SELECT count(*) FROM Results

2。每页金额

这是您自己决定的价值。

3.当前页码

这通常默认为1,但是当分页改变时,它作为get参数传递。

4.表格中的一部分结果

可通过以下方式实现:

SELECT * FROM Results LIMIT 0, 20

此示例检索结果表中的前 20 个结果...

5.分页

现在,使用 20 个结果作为默认的“每页计数”

$countPerPage = 20;

// Get the total number of results
$result = pg_query('SELECT count(*) FROM Results'); // I'm using PostgreSQL for this example
$totalResultCount = (int)pg_fetch_result($result, 0, 0);

// The ceil function will round floats up.
$numberOfPages = ceil($totalResultCount / $countPerPage);

// Check if we have a page number in the _GET parameters
if(!empty($_GET) && isset($_GET['page']))
{
    $page = (int)$_GET['page'];
}
else
{
    $page = 1;
}

// Check that the page is within our bounds
if($page < 0)
{
    $page = 1;
}
elseif($page > $numberOfPages)
{
    $page = $numberOfPages;
}

// Build the query for the results...
$query = 'SELECT * FROM Results LIMIT ' . ($page - 1) * $countPerPage . ', ' . $countPerPage;

$results = pg_fetch_all(pg_query($query));

// Deal with printing your results etc...

?>
<ul class="pagination">
    <li<?php if($page === 1): ?> class="disabled"<?php endif; ?>><a href="<?php
if($page !== 1){
    echo 'page.php?page=' . $page - 1;
} else { echo '#'; } 
?>">&laquo;</a></li>
<?php

// Print the pagination...
// Minimum of 5 pages in the pagination, even if there aren't 5 pages...
$pageCount = ($numberOfPages < 5) ? 5 : $numberOfPages;

// Loop through from page 1 until the last page ($pageCount)
for($i = 1; $i < $pageCount; $i++)
{
     // Echo out just the beginning of the <li> tag as we don't yet
     // know if this needs to be disabled...
     echo '<li';

     // Check if:
     //  - This current page is greater than the amount of pages of
     //    results we have
     //  - OR, this is the currently selected page
     if($i > $numberOfPages || $page === $i)
     {
          // <li> tag needs the disabled class....
          echo ' class="disabled"';
     }
     // Finish the <li> tag and start generating the link
     echo '><a href="';
     // Opposite logic from above... Only if this is a page we know about
     // AND this is not the current page.
     if($i < $numberOfPages && $page !== $i)
     {
          // The link will be your page, with the get param, and the
          // current page number we're printing
          echo 'page.php?page=' . $i;
     }
     else
     {
          // Otherwise just a # (no link)
          echo '#';
     }
     // Finish this page... Print the page number ($i)
     echo '">' . $i . '</a></li>';
}
?>
<ul class="pagination">
    <li<?php if($page === $numberOfPages): ?> class="disabled"<?php endif; ?>><a href="<?php
if($page !== $numberOfPages){
    echo 'page.php?page=' . $page + 1;
} else { echo '#'; } 
?>">&raquo;</a></li>
<?php

免责声明

我只是坐在这里写了这个,我没有测试过,但大致的想法就在那里。希望这是有道理的。我的时间不多了,没有时间测试或改进它,但如果你有兴趣,我可能会稍后修复它。

希望这会有所帮助!

【讨论】:

  • 感谢您的全面回复!!我可能在我的原始帖子中没有说清楚,实际的分页、显示和解析结果工作正常。我有一个获取页码的变量 ($results)。我的 php 顶部还有 $num_res = 10; $offset = ($results - 1) * $num_res;,它决定了我的 MySQL 查询 LIMIT$num_res 是用户可选择的选项(10 个结果、25、100 个)我将尝试将您的循环转换为在我的页面上工作。
  • 我在循环中添加了一些 cmets,希望能更清楚地了解发生了什么。我只是想建议您废弃您在原始帖子中所写的内容。我不想听起来刺耳,但最好的代码是可扩展的。你想写点东西,而不是被用户输入破坏。想象一下,在您的示例中,如果您允许一个普通的 Joe 添加更多结果……您将不得不在每次需要另一个页面时更新您的分页。
  • 好的,我已经修改了您的代码以使用我的代码。如果您有时间,请在我的原始帖子中查看我对 cme​​ts 的编辑。再次感谢您的回复。
猜你喜欢
  • 1970-01-01
  • 2015-03-05
  • 1970-01-01
  • 2013-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多