Hi All,
I recently required to created a custom pagination with the help of Bootstrap in my core PHP project. I thought that my code may help others also who wish to create the same.
The reason why I am using Bootstrap is as it ease our work because it already has inbuilt classes for the pagination. This gives a very nice look to our number shown on the webpage.
Following is the code for it, you may change it as per your requirements:-
1st Step:- Initializing variables.
$finalArray = array(); $html = ""; $recordList = ""; $parameters = array(); $querString = ""; $limit = 5;
2nd Step:- Checking for the page count.
if (isset($_GET['page']) && ($_GET['page'] > 0)) {
$page = $_GET['page'];
} else {
$page = 1;
}
3rd Step:- Code for pagination.
//$recordList is the array of mysql records.
if (isset($recordList) && ($recordList != "no record") && (is_array($recordList))) {
$parameters['company_id'] = $companyId;
foreach($parameters as $key => $parameter){
$querString .= $key . "=" . $parameter . "&";
}
$querString = rtrim($querString, '&');
$startPoint = $limit * ($page - 1);
$endpoint = $startPoint + $limit;
$finalArray = array_slice($recordList, $startPoint, $limit);
$totalContent = count($recordList);
$html .= "<ul class='pagination'>";
if ($page == 1) {
$html .= "<li class='disabled' >";
$html .= "<span>«</span>";
} else {
$next = $page - 1;
$html .= "<li>";
$html .= "<a rel='prev' href='" . BASE_URL . "admin_selection.php?page={$next}&{$querString}'>«</a>";
}
$html .= "</li>";
for ($i = 1, $j = 1; ($i <= $totalContent) && ($j < $totalContent); $i++, $j = ($limit * ($i - 1))) {
if ($i == $page) {
$html .= "<li class='active' >";
$html .= "<span>{$i}</span>";
} else {
$html .= "<li>";
$html .= "<a rel='{$i}' href='" . BASE_URL . "admin_selection.php?page={$i}&{$querString}'>{$i}</a>";
}
$html .= "</li>";
}
if ($endpoint >= $totalContent) {
$html .= "<li class='disabled' >";
$html .= "<span>»</span>";
} else {
$prev = $page + 1;
$html .= "<li>";
$html .= "<a rel='next' href='" . BASE_URL . "admin_selection.php?page={$prev}&{$querString}'>»</a>";
}
$html .= "</li>";
$html .= "</ul>";
}
4th Step:- Displaying pagination.
<div class="text-center"><?php echo $html; ?></div>
0 Comment(s)