Ian Haney
Ian Haney

Reputation: 87

php mysql pagination active class

I want to be able to add a active class to a pagination so I know which page I am on but am unsure how to add it into the current code I have, below is what I currently have

$per_page=6;
if (isset($_GET["page"])) {

$page = $_GET["page"];

}

else {

$page=1;

}

// Page will start from 0 and Multiple by Per Page
$start_from = ($page-1) * $per_page;

if ($result = $mysqli->query("SELECT * FROM TABLENAME ORDER BY COLUMNNAME ASC LIMIT $start_from, $per_page"))

// Count the total records
$total_records = mysqli_num_rows($result);

//Using ceil function to divide the total records on per page
$total_pages = ceil($total_records / $per_page);

//Going to first page
echo "<div class='btn-group' style='margin:0 auto;display:table;'>";
echo "<br><br><center><a href='view-all-customers.php?page=1' class='btn btn-primary float-button- 
light' style='color:#FFFFFF;padding:6px 15px;'>".'First Page'."</a>";

for ($i=1; $i<=$total_pages; $i++) {

echo "<a href='view-all-customers.php?page=".$i."' class='btn btn-primary float-button-light' 
style='color:#FFFFFF;padding:6px 15px;'>".$i."</a>";
};
// Going to last page
echo "<a href='view-all-customers.php?page=$total_pages' class='btn btn-primary float-button-light' 
style='color:#FFFFFF;padding:6px 15px;'>".'Last Page'."</a></center>";
echo "</div>";

I have left out the data table rows as thought it's better to show the pagination coding more than showing the table tr and td rows outputting the data

Upvotes: 0

Views: 270

Answers (1)

arun
arun

Reputation: 4815

//check page and add active class

for ($i=1; $i<=$total_pages; $i++) {
    $isActive = '';
    if($i == $page){
        $isActive = 'active';
    }

    echo "<a href='view-all-customers.php?page=".$i."' class='".$isActive."btn btn-primary float-button-light' style='color:#FFFFFF;padding:6px 15px;'>".$i."</a>";
};

Upvotes: 1

Related Questions