Gokul P P
Gokul P P

Reputation: 962

Pagination displaying full buttons for paginating

Currently, I am having a code for pagination and it is displaying all pages continuously

<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>

Adding a screenshot showing how it will be now displaying: enter image description here

What to do to make this comes in a single row?

Upvotes: 1

Views: 93

Answers (1)

Pranav C Balan
Pranav C Balan

Reputation: 115222

You can implement using your own logic. Generate limited number of links based on the current page and total number of pages.

For example :

<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>

Upvotes: 1

Related Questions