pagination in php with mysql - php

I wonder how to easily make pagination in PHP with MySQL.. May I ask you a favor of you?
This my code have not error... take this code with your project implement..
<ul class="pagination justify-content-end">
<?php
$sql = "SELECT * FROM json_data";
$rs_result1 = mysqli_query($connect, $sql);
$row = mysqli_num_rows($rs_result1);
//print_r($rs_result1); die();
$limit = 10;
$total_records = $row;
//$total_pages = ceil($total_records / $limit)-220;
// calculate total pages
$total_pages = ceil($row / $limit);
$prev = $page-1;
$next = $page+1;
$pagination_buttons =5;
$last_page = $total_pages;
$half = floor($pagination_buttons/2);
//echo '<ul class="pagination">';
if($page >= 5){
echo '<li>First</li>';
}
if($page < $pagination_buttons AND ($last_page == $pagination_buttons OR $last_page > $pagination_buttons)){
for($i=1; $i<=$pagination_buttons; $i++){
if($i == $page){
echo '<li class="active">'.$i.'</li>';
}
else{
echo '<li>'.$i.'</li>';
}
}
if($last_page > $pagination_buttons){
echo '<li>Next</li>';
}
}
else if($page >= $pagination_buttons AND $last_page > $pagination_buttons){
if(($page+$half) >= $last_page){
echo '<li>Previous</li>';
for ($i=($last_page-$pagination_buttons)+1; $i<=$last_page; $i++) {
if($i == $page){
echo '<li class="active">'.$i.'</li>';
}
else{
echo '<li>'.$i.'</li>';
}
}
}
else if(($page+$half) < $last_page){
echo '<li>Previous</li>';
for ($i=($page-$half); $i<=($page+$half); $i++) {
if($i == $page){
echo '<li class="active">'.$i.'</li>';
}
else{
echo '<li>'.$i.'</li>';
}
}
echo '<li>Next</li>';
}
}
if($page != $total_pages && $total_pages >= 6){
echo '<li>Last</li>';
} ?>
i solve it my code in pagination in php with mysql.. and hope may help this code your pagination project...
I coded pagination code for my site in php and mysql.. I wonder how to easily make pagination in PHP with MySQL.. May I ask you a favor of you? This my code have not error... take this code with your project implement..

Related

php navigation pages issue

I have a strange problem with my navigation next/previous code. Everything works fine but random after I click on the next page, the whole navigation is gone. And when I change the pagina variable from page 4 to 5 for example, does everything work again, but pagina 4 keeps removing the navigation. I don't know why it occurs?
On the top of my page do I have:
if (isset($_GET['pagina']) && $_GET['pagina']!="") {
$page_no = $_GET['pagina'];
}
else {
$page_no = 1;
}
$total_records_per_page = 100;
$offset = ($page_no-1) * $total_records_per_page;
$previous_page = $page_no - 1;
$next_page = $page_no + 1;
$adjacents = "2";
And on the bottom of my page
$taal = htmlspecialchars(addslashes($_GET['taal']));
if($page_no > 1){
echo <a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$previous_page."'>Vorige</a>";
}
if ($total_no_of_pages <= 10){
for ($counter = 1; $counter <= $total_no_of_pages; $counter++){
if ($counter == $page_no) {
echo "<a class='active'>".$counter."</a>";
}
else
{
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$counter."'>".$counter."</a>";
}
}
}
elseif ($total_no_of_pages > 10){
if($page_no <= 4) {
for ($counter = 1; $counter < 8; $counter++){
if ($counter == $page_no) {
echo "<span class='active'><a>".$counter."</a></span>";
}
else {
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$counter."'>".$counter."</a>";
}
}
echo "<a>...</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$second_last."'>".$second_last."</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$total_no_of_pages."'>".$total_no_of_pages."</a>";
}
elseif($page_no > 4 && $page_no < $total_no_of_pages - 4) {
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=1'>1</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=2'>2</a>";
//echo "<li><a>...</a></li>";
for (
$counter = $page_no - $adjacents;
$counter <= $page_no + $adjacents;
$counter++
) {
if ($counter == $page_no) {
echo "<span class='active'><a>".$counter."</a></span>";
}else{
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$counter."'>".$counter."</a>";
}
}
echo "<a>...</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$second_last."'>".$second_last."</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$total_no_of_pages."'>".$total_no_of_pages."</a>";
}
else {
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=1'>1</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=2'>2</a>";
echo "<a>...</a>";
for (
$counter = $total_no_of_pages - 6;
$counter <= $total_no_of_pages;
$counter++
)
{
if ($counter == $page_no) {
echo "<span class='active'><a>".$counter."</a></span>";
}
else{
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$counter."'>".$counter."</a>";
}
}
}
}
if($page_no < $total_no_of_pages) {
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$next_page."'>Volgende</a>";
}
$taal is language,
$page_no is which page where I'm on
$zoekveld is search field to search on
$pagina is pagenumber in url
I think it has something to do with my GET search field (Zoekveld) but why occurs it after a couple of pages?
Thanks in advance

How to create pagination that shows previous and next 3 pages

I want to show a pagination which only shows the recent and next 3 pages, but I can't get it working.
Here's my current code:
$stmt = MySQL::connection3()->prepare("SELECT COUNT(ID) AS TOTAL FROM products");
$stmt->execute();
$row = $stmt->fetch();
$total_pages = ceil(intval($row["TOTAL"]) / $results_per_page);
foreach (range(1, $total_pages) as $i) {
if ($i == $page) {
?>
<li class="page-item active">
<?php echo $i ?>
</li>
<?php
} else {
?>
<li class="page-item">
<?php echo $i ?>
</li>
<?php
}
}
Thanks!
I create this example you can run and adapt with your code:
<style>.active{color:green!important}</style>
<?php
echo get_pagination_links('10','100');
function get_pagination_links($current_page, $total_pages)
{
$links = "";
if ($total_pages >= 1 && $current_page <= $total_pages) {
$i = max(2, $current_page - 3);
for (; $i < min($current_page + 4, $total_pages); $i++) {
if($i==$current_page){
$links .= "<li class='page-item active'><a href='?page=$i;' class='page-link'>$i</a></li>";
}else{
$links .= "<li class='page-item'><a href='?page=$i;' class='page-link'>$i</a></li>";
}
}
return $links;
}
}
?>
Output:
7-8-9-10-11-12-13
obviously I didn't connect a database and therefore I used a static result

Between Pagination

Help me, i was writing my code, all done succesfully but my problem is when the "..." (separator) for my curerent page and last page showed after the last page, not between my current page and my last page
how can that possible ?
this is my script for showing the page :
<?php
$paging2 = mysqli_query($koneksi,"select * from laporan where seksi='{$seksi}'");
$jmldata = mysqli_num_rows($paging2);
$jmlhalaman = ceil($jmldata/$batas);
echo "<ul class='pagination'>";
if ($halaman !== 1){
echo "<li><a href='index.php?load=table_laporan&halaman=1'>First</a></li>";
}
if ($halaman > 1){
echo "<li><a href='index.php?load=table_laporan&halaman=".($halaman-1)."'>Previous</a></li>";
}
for($i =1; $i <= $jmlhalaman; $i++){
if ((($i >= $halaman - 3) && ($i <= $halaman + 3)) || ($i == 1) || ($i ==$jmlhalaman))
{
if ($i == $halaman){
$class="active";
} else{
$class="disable";
} //echo "<b>$i</b>";
echo "<li class='$class'>".$i."<span class='sr-only'>(current)</span></li>";
if($i==$jmlhalaman && $halaman <= $jmlhalaman-5) echo "<li><a>...</a></li>";
if($i==1 && $halaman >= 6) echo "<li><a>...</a></li>";
}
}
if ($halaman >= 1){
echo "<li><a href=index.php?load=table_laporan&halaman=". ($halaman+1)."'>Next</a></li>";
} if ($halaman >= 1){
echo "<li><a href='index.php?load=table_laporan&halaman=". ($jmlhalaman)."'>Last</a></li>";
}
echo "</ul>";
echo "<p>Total Data : <b>$jmldata</b> Data</p>";
?>

Pagination does not work perfectly

I have the following code:
$max = 4;
$page = isset($_GET['page']) ? ($_GET['page']) : '1';
$init = $page - 1;
$init= $max * $init;
$strCount = "SELECT COUNT(*) AS 'total_mytable' FROM mytable";
$varstrCount = $crud->viewdatas($strCount);
$total = 0;
if(count($varstrCount)){
foreach ($varstrCount as $row) {
$total = $row["total_mytable"];
}
}
$result = "SELECT * FROM mytable ORDER BY id_mytable LIMIT $init,$max";
$varresult = $crud->viewdatas($result);
content of page:
<?php
if(count($varresult)){
foreach ($varresult as $res) {
?>
<h5><?php echo $res['title'] ?></h5>
<?php
}
}
?>
<?php
$max_links = 10;
$previous = $page - 1;
$next = $page + 1;
$pgs = ceil($total / $max);
if($pgs > 1 ){
if($previous > 0){
echo "<li><a href='".BASE_URL."/category/$previous' aria-label='Previous'><span aria-hidden='true'>«</span></a></li>";
} else{
}
for($i=$page-$max_links; $i <= $pgs-1; $i++) {
if ($i <= 0){
}else{
if($i != $page{
if($i == $pgs){ //if end insert 3 dots
echo "<li><a href='".BASE_URL."/category/".($i)."'>".$i."</a></li> ...";
}else{
echo "<li><a href='".BASE_URL."/category/".($i)."'>".$i."</a></li>";
}
} else{
if($i == $pgs){ //if end insert 3 dots
echo "<li>".$i."</li> ...";
}else{
echo "<li>".$i."</li>";
}
}
}
}
if($next <= $pgs){
echo "<li><a href='".BASE_URL."/category/$next' aria-label='Next'><span aria-hidden='true'>»</span></a></li>";
}else{
}
}
?>
The result:
And I not understand the reason for the active number stay right off the paging menu
In the code I defined max-links for 10, but no show number 5 and if I define max links for 3 changes nothing , displays the same result as the image.
Thanks for any help
echo "<li>".$i."</li>";
on the above line add up a href, seems like you have css formatting for li>a because of which you are not getting the required formatting on only li. so for getting better formatting for all paging links current, prev, next. you need to add up a tags.
echo "<li><a>".$i."</a></li>"; //in your else part

Href format using master page php

I saw a similar post on stackoverflow but it failed to anwser my question.
I am using pagination to display results and the link is coming up broken when I add the master links. Most likely as I am incorrectly using the correct format.
Have tried a few formats to try to get it working based on other posts
<a href='../admin/admin.master.php?page=list_products.php&page=' .$j. ' id='page_a_link'>Next</a></span>
or
../admin/admin.master.php?page=list_products.php&page=$j
but neither work.
PHP PAGINATION SHOWING PAGE NUMBERS
<?php
if(isset($page))
{
$result = mysql_query("SELECT COUNT(*) As Total FROM products");
$rows = mysql_num_rows($result);
if($rows)
{
$rs = mysql_fetch_array($result);
$total = $rs["Total"];
}
$totalPages = ceil($total / $perpage);
if($page <=1 )
{
echo "<span id='page_links' style='font-weight:bold;'>Pre</span>";
}
else
{
$j = $page - 1;
echo "<span><a id='page_a_link' href='../admin/admin.master.php?page=list_products.php&page=$j'>< Pre</a></span>";
}
for($i=1; $i <= $totalPages; $i++)
{
if($i<>$page)
{
echo "<span><a href='../admin/admin.master.php?page=list_products.php&page=' .$i. ' id='page_a_link'>$i</a></span>";
}
else
{
echo "<span id='page_links' style='font-weight:bold;'>$i</span>";
}
}
if($page == $totalPages )
{
echo "<span id='page_links' style='font-weight:bold;'>Next ></span>";
}
else
{
$j = $page + 1;
echo "<span><a href='../admin/admin.master.php?page=list_products.php?page=' .$j. ' id='page_a_link'>Next</a></span>";
}
}
?>
You are mixing double quotes with single quotes, that's why it breaks. Try this:
<?php
if(isset($page))
{
$result = mysql_query("SELECT COUNT(*) As Total FROM products");
$rows = mysql_num_rows($result);
if($rows)
{
$rs = mysql_fetch_array($result);
$total = $rs["Total"];
}
$totalPages = ceil($total / $perpage);
if($page <=1 )
{
echo '<span id="page_links" style="font-weight:bold;">Pre</span>';
}
else
{
$j = $page - 1;
echo '<span><a id="page_a_link" href="../admin/admin.master.php?page=list_products.php&page=' . $j . '">< Pre</a></span>';
}
for($i=1; $i <= $totalPages; $i++)
{
if($i<>$page)
{
echo '<span>' . $i . '</span>';
}
else
{
echo '<span id="page_links" style="font-weight:bold;">' . $i . '</span>';
}
}
if($page == $totalPages )
{
echo '<span id="page_links" style="font-weight:bold;">Next ></span>';
}
else
{
$j = $page + 1;
echo '<span>Next</span>';
}
}
?>
Also, the HTML code you generate isn't valid, as you assign the same ID to multiple elements. Consider changing id="page_links" and id="page_a_link" to class="page_link" and class="page_a_link" respectively, then change #page_links and #page_a_link in your CSS to .page_links and .page_a_link
You are assigning two values to page in the URL '../admin/admin.master.php?**page**=list_products.php&**page**=' .$j. ' try replacing page = $j with page_num = $j or something of your choice and replace it in the and in the code.

Categories