Adding Page Numbers (Pagination)

3 posts by 2 authors in: Forums > CMS Builder
Last Post: June 26, 2020   (RSS)

By Codee - June 25, 2020

Yes, I have some code below I use on a particular site that does what you are asking. It's a product listing page. There is probably a shorter, more succinct way to write it out in code but I'm not the greatest coder...but I try to be effective. Notice a couple important things: style is handled by css, there are pieces where the "else" shows nothing (but not notated) and is intentional. I'll include a screenshot of what it looks like visually.

<!--begin page number notation-->
<div class="pagenumber">

<?php if ($productsMetaData['invalidPageNum']): ?>

Results page '<?php echo $productsMetaData['page']?>' not found, <a href="<?php echo $productsMetaData['firstPageLink'] ?>">start over &gt;&gt;</a>.<br><br>

<?php elseif (!$productsRecords): ?>

<br>
Those records are not currently available. Please click BACK in your browser.<br><br>

<?php endif ?>

<?php if ($productsMetaData['prevPage']): ?>

<a href="<?php echo $productsMetaData['prevPageLink'] ?>">&lt;&lt;&nbsp;previous&nbsp;</a>

<?php else: ?>

<?php endif ?>

<?php if (@!$_GET['page']): $current_page = "1";

else: $current_page = $_GET['page'];

endif; ?>

<?php foreach (range(1,$productsMetaData['totalPages']) as $page): ?>

<?php if ($page == $current_page): ?>

<span class="bordertext">&nbsp;page&nbsp;<?php echo $page; ?>&nbsp;</span>

<?php else: ?>

<a href="?<?php echo http_build_query(array_merge($_REQUEST, array( 'page' => $page ))) ?>"><?php echo $page; ?></a>

<?php endif ?>

<?php endforeach; ?>

<?php if ($productsMetaData['nextPage']): ?>

<a href="<?php echo $productsMetaData['nextPageLink'] ?>">&nbsp;next&nbsp;&gt;&gt;</a>

<?php else: ?>

<?php endif ?>

</div>
<!--end page number notation-->

The css I used for what you see:


.pagenumber {
font-style: normal;
font-weight: 600;
padding: 10px 0px;
text-align: center;
}

.bordertext {
border-color: #808080;
border-style: solid;
border-width: 2px;
border-radius: 8px;
}

I hope that works, or helps get you in the right direction.

Attachments:

screenshot_numbers.png 122K

By mark99 - June 26, 2020

That's exactly what I was looking for, thanks so much :) .