Reputation: 147
I have a pagination navigation and I want pagination button to be active when user is on that page. Here is how I use pagination code:
<?php if ($totalPages > 1) { ?>
<ul class="pagination justify-content-center mb-4">
<li class="page-item">
<?php echo "<a class='page-link' href='index.php?page=1'>First page</a>"?>
</li>
<?php
for ($i=2; $i < $totalPages; $i++){
echo "<a class='page-link' href='index.php?page=".$i."'>".$i."</a>";
}
?>
<li class="page-item ">
<?php echo "<a class='page-link' href='index.php?page=$totalPages'>Last page</a>"?>
</li>
</ul>
<?php } else { ?>
How can I achieve that?
Upvotes: 1
Views: 1824
Reputation: 796
for ($i=2; $i < $totalPages; $i++){
echo "<a class='page-link ".($i == $currentPageNo ? "active" : "" ). "' href='index.php?page=".$i."'>".$i."</a>"; }
Try this
Upvotes: 2
Reputation: 31
Before displaying pagination link check if that page no and current page no is same or not. If it is same then add class active to that.For e.g.:
for ($i=2; $i < $totalPages; $i++){
if ($i == $currentPageNo) {
echo "<a class='page-link active' href='index.php?page=".$i."'>".$i."</a>";
} else {
echo "<a class='page-link' href='index.php?page=".$i."'>".$i."</a>";
}
}
Upvotes: 0
Reputation: 2775
If you are passing a collection like this:
$data = Model::paginate(10);
Then, in your view, you can use the following to create a pagination with numbers and auto active
class in Laravel.
{{ $data->links() }}
OR
You can pass a variable from your controller like:
$active = true;
and in your view you can do something like this:
<a href="somelocation" class="{{ @if($active) acctive @endif }}"> #Link </a>
Upvotes: 0