分页显示分页的完整按钮

时间:2016-12-27 05:21:54

标签: php html css pagination

目前,我正在使用分页代码,并且它会连续显示所有页面

<div class="col-md-12">
  <nav aria-label="Page navigation">
    <ul class="pagination">
      <?php for ($i=0 ; $i < $products->totalPages; $i++) : ?>
      <li <?php if ($products->number == $i) echo "class='active'"; ?>>
        <a <?php if ($products->number != $i): ?> href="index.php?page=<?php echo $i . (isset($_GET['cid']) ? '&cid=' . $_GET['cid'] : ''); ?>#prdct" <?php endif; ?>><?php echo($i + 1); ?></a>
      </li>
      <?php endfor; ?>
    </ul>
  </nav>
</div>

添加屏幕截图,显示现在将如何显示: enter image description here

怎么做才能让它排成一排?

1 个答案:

答案 0 :(得分:1)

您可以使用自己的逻辑来实现。根据当前页面和总页数生成有限数量的链接。

例如:

<div class="col-md-12">
    <nav aria-label="Page navigation">
        <ul class="pagination">
            <li <?php if ($products->number == 0) echo "class='active'"; ?>>
                <a <?php if ($products->number != 0): ?> href="index.php?page=0<?php echo(isset($_GET['cid']) ? '&cid=' . $_GET['cid'] : ''); ?>#prdct" <?php endif; ?>>1</a>
            </li>

            <?php if ($products->totalPages > 2) { ?>
                <?php if ($products->number > 3): ?><li><a href="javascript:void(0)">...</a></li><?php endif; ?>
                <?php for ($i = ($products->number == 0 ? 1 : $products->number-1); $i < $products->number+2  && $i < $products->totalPages - 1; $i++) { ?>
                    <li <?php if ($products->number == $i) echo "class='active'"; ?>>
                        <a <?php if ($products->number != $i): ?> href="index.php?page=<?php echo $i . (isset($_GET['cid']) ? '&cid=' . $_GET['cid'] : ''); ?>#prdct" <?php endif; ?>><?php echo($i + 1); ?></a>
                    </li>
                <?php } ?>
                <?php if ($products->number < $products->totalPages - 3): ?><li><a href="javascript:void(0)">...</a></li><?php endif; ?>
            <?php } ?>

            <?php if ($products->totalPages > 1): ?>
                <li <?php if ($products->number == $products->totalPages) echo "class='active'"; ?>>
                    <a <?php if ($products->number != $products->totalPages - 1): ?> href="index.php?page=<?php echo ($products->totalPages - 1) . (isset($_GET['cid']) ? '&cid=' . $_GET['cid'] : ''); ?>#prdct" <?php endif; ?>><?php echo $products->totalPages; ?></a>
                </li>
            <?php endif; ?>
        </ul>
    </nav>
</div>