I have a code that helps in pagination.
index.php:
<?php
$per_page = 10; // number of results to show per page
$result = mysqli_query($con,"SELECT * FROM vendor ");
<?
$total_results = mysqli_num_rows($result);
$row = mysqli_fetch_assoc($result);
$total_pages = ceil($total_results / $per_page);//total pages we going to have
//-------------if page is setcheck------------------//
if (isset($_GET['page'])) {
$show_page = $_GET['page']; //it will telles the current page
if ($show_page > 0 && $show_page <= $total_pages) {
$start = ($show_page - 1) * $per_page;
$end = $start + $per_page;
} else {
// error - show first set of results
$start = 0;
$end = $per_page;
}
} else {
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// display pagination
$page = intval($_GET['page']);
$tpages=$total_pages;
if ($page <= 0)
$page = 1;
?>
<div id="tab1" class="tab_content">
<?php
$reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;
echo "<table class='tablesorter' cellspacing='0'> ";
echo "<thead>
<tr>
<th></th>
<th>ID</th>
<th>Shopname</th>
<th>Instagram account</th>
</tr>
</thead>";
for ($i = $start; $i < $end; $i++)
{
if ($i == $total_results)
{
break;
}
mysqli_data_seek($result, $i);
$data = mysqli_fetch_assoc($result);
echo "<tr " . $cls . ">";
echo '<td></td>';
echo '<td>' . $data['id'] . '</td>';
echo '<td>' . $data['fullname_shopname'] . '</td>';
echo '<td>' . $data['username_instagramacnt'] . '</td>';
echo "</tr>";
}
echo "</table>";
echo '<div class="pagination" style="margin-left:300px;" >';
if ($total_pages > 1)
{
echo paginate($reload, $show_page, $total_pages);
}
echo "</ul>
</div>";
?>
</div>
code for paginate.php
<?php
function paginate($reload, $page, $tpages) {
$adjacents = 2;
$prevlabel = "‹ Prev";
$nextlabel = "Next ›";
$out = "";
// previous
if ($page == 1) {
$out.= "<span>" . $prevlabel . "</span>
";
} elseif ($page == 2) {
$out.= "<li><a href=\"" . $reload . "\">" . $prevlabel . "</a>
</li>";
} else {
$out.= "<li><a href=\"" . $reload . "&page=" . ($page - 1) . "\">" . $prevlabel . "</a>
</li>";
}
$pmin = ($page > $adjacents) ? ($page - $adjacents) : 1;
$pmax = ($page < ($tpages - $adjacents)) ? ($page + $adjacents) : $tpages;
for ($i = $pmin; $i <= $pmax; $i++) {
if ($i == $page) {
$out.= "<li class=\"active\"><a href=''>" . $i . "</a></li>
";
} elseif ($i == 1) {
$out.= "<li><a href=\"" . $reload . "\">" . $i . "</a>
</li>";
} else {
$out.= "<li><a href=\"" . $reload . "&page=" . $i . "\">" . $i . "</a>
</li>";
}
}
if ($page < ($tpages - $adjacents)) {
$out.= "<a style='font-size:11px' href=\"" . $reload . "&page=" . $tpages . "\">" . $tpages . "</a>
";
}
// next
if ($page < $tpages) {
$out.= "<li><a href=\"" . $reload . "&page=" . ($page + 1) . "\">" . $nextlabel . "</a>
</li>";
} else {
$out.= "<span style='font-size:11px'>" . $nextlabel . "</span>
";
}
$out.= "";
return $out;
}
Everything is working properly without any problem but I would like to add one more feature.
currently I am getting pagination like this
I would like to add first and last button before prev and after next respectively. These buttons should be present at all times except on first and last page.
can anyone tell how I can do so?