{"id":2695,"date":"2015-05-26T07:48:23","date_gmt":"2015-05-26T07:48:23","guid":{"rendered":"http:\/\/www.vishmax.com\/en\/?p=2695"},"modified":"2015-05-26T07:49:21","modified_gmt":"2015-05-26T07:49:21","slug":"php-pagination-code-example-using-mysql-database","status":"publish","type":"post","link":"https:\/\/www.vishmax.com\/en\/labs\/php-pagination-code-example-using-mysql-database\/","title":{"rendered":"php pagination code example using MySql database"},"content":{"rendered":"<p><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.vishmax.com\/en\/innovattive-cms\/uploads\/2015\/05\/php-pagination-screenshot.jpg\" alt=\"php-pagination-screenshot\" width=\"1000\" height=\"513\" class=\"alignnone size-full wp-image-2697\" srcset=\"https:\/\/www.vishmax.com\/en\/wp-content\/uploads\/2015\/05\/php-pagination-screenshot.jpg 1000w, https:\/\/www.vishmax.com\/en\/wp-content\/uploads\/2015\/05\/php-pagination-screenshot-400x205.jpg 400w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/p>\n<p>Here is a working code for pagination using PHP and MySql database. Its is tested and working fine on servers. you can set the  limit of display items on page by editing the limit line. Below is the screenshot of the webpage which looks likes after implementing the code.<\/p>\n<h2>Pagination code<\/h2>\n<pre lang=\"php\">\r\n<?php\r\n\r\ninclude(\"connect-db.php\"); \/\/ Add your database connections\r\n\r\n\r\n\t$sql5 = mysql_query(\"select * from table where ID='$tag'\");\r\n\t$row5=mysql_fetch_array($sql5);\r\n\t$flag=0;\r\n\t$adjacents = 3;\r\n\t$query = \"SELECT COUNT(*) as num FROM table where Is_approve='1'\"; \/\/ you need to change this query according to your requirements\r\n\t$total_pages = mysql_fetch_array(mysql_query($query));\r\n\t$total_pages = $total_pages[num];\r\n\t$targetpage = \"search.php\"; \t\r\n\t$limit =15;\t\t\r\n\t\t\t\t\t\t\r\n$page = $_GET['page'];\r\n\r\nif($page) \r\n\t$start = ($page - 1) * $limit; \t\t\t\r\nelse\r\n\t$start = 0;\t\t\r\n$sql = \"SELECT * FROM sell_property where sell_property.Is_approve='1'  ORDER BY Date DESC LIMIT $start, $limit\"; \/\/ you need to change this query according to your requirements\r\n$result = mysql_query($sql);\r\n\tif ($page == 0) \r\n\t$page = 1;\t\t\t\t\t\r\n\t$prev = $page - 1;\t\t\t\t\t\t\t\r\n\t$next = $page + 1;\t\t\t\t\t\t\t\r\n\t$lastpage = ceil($total_pages\/$limit);\t\t\r\n\t$lpm1 = $lastpage - 1;\t\t\t\t\t\t\r\n\t$pagination = \"\";\r\n\r\nif($lastpage > 1){\t\r\n\t$pagination .= \"<div class=\\\"pagination\\\">\";\r\n\tif ($page > 1) \r\n\t$pagination.= \"<a href=\\\"$targetpage?page=$prev\\\">&laquo; previous<\/a>\";\r\nelse\r\n\t$pagination.= \"<span class=\\\"disabled\\\">&laquo; previous<\/span>\";\t\r\n\tif ($lastpage < 7 + ($adjacents * 2))\t\r\n{\t\r\nfor ($counter = 1; $counter <= $lastpage; $counter++){ \r\n\tif ($counter == $page)\r\n\t$pagination.= \"<span class=\\\"current\\\">$counter<\/span>\";\r\n\telse\r\n\t$pagination.= \"<a href=\\\"$targetpage?page=$counter\\\">$counter<\/a>\";\t\r\n}\r\n}\r\nelseif($lastpage > 5 + ($adjacents * 2)){\r\nif($page < 1 + ($adjacents * 2))\t\t\r\n{\r\n\tfor ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++){\r\n\tif ($counter == $page)\r\n\t$pagination.= \"<span class=\\\"current\\\">$counter<\/span>\";\r\n\telse\r\n\t$pagination.= \"<a href=\\\"$targetpage?page=$counter\\\">$counter<\/a>\";\t\t\t\t\t\r\n}\r\n\t$pagination.= \"...\";\r\n\t$pagination.= \"<a href=\\\"$targetpage?page=$lpm1\\\">$lpm1<\/a>\";\r\n\t$pagination.= \"<a href=\\\"$targetpage?page=$lastpage\\\">$lastpage<\/a>\";\t\t\r\n}\r\nelseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)){\r\n\t$pagination.= \"<a href=\\\"$targetpage?page=1\\\">1<\/a>\";\r\n\t$pagination.= \"<a href=\\\"$targetpage?page=2\\\">2<\/a>\";\r\n\t$pagination.= \"...\";\r\n\tfor ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)\r\n\t{\r\n\tif ($counter == $page)\r\n\t$pagination.= \"<span class=\\\"current\\\">$counter<\/span>\";\r\n\telse\r\n\t$pagination.= \"<a href=\\\"$targetpage?page=$counter\\\">$counter<\/a>\";\t\t\t\t\t\r\n\t}\r\n\t$pagination.= \"...\";\r\n\t$pagination.= \"<a href=\\\"$targetpage?page=$lpm1\\\">$lpm1<\/a>\";\r\n\t$pagination.= \"<a href=\\\"$targetpage?page=$lastpage\\\">$lastpage<\/a>\";\t\t\r\n\t}else\r\n\t{$pagination.= \"<a href=\\\"$targetpage?page=1\\\">1<\/a>\";\r\n\t$pagination.= \"<a href=\\\"$targetpage?page=2\\\">2<\/a>\";\r\n\t$pagination.= \"...\";\r\n\tfor ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)\r\n\t{\r\n\tif ($counter == $page)\r\n\t$pagination.= \"<span class=\\\"current\\\">$counter<\/span>\";\r\n\telse\r\n\t$pagination.= \"<a href=\\\"$targetpage?page=$counter\\\">$counter<\/a>\";\t\t\t\t\t\r\n\t}}}\r\n\tif ($page < $counter - 1) \r\n\t$pagination.= \"<a href=\\\"$targetpage?page=$next\\\">next &raquo;<\/a>\";\r\n\telse\r\n\t$pagination.= \"<span class=\\\"disabled\\\">next &raquo;<\/span>\";\r\n\t$pagination.= \"<\/div>\\n\";\t\t\r\n\t}\r\n?>\r\n\r\n\r\n<?php  echo $pagination;?>\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here is a working code for pagination using PHP and MySql database. Its is tested and working fine on servers. you can set the limit of display items on page by editing the limit line. Below is the screenshot of the webpage which looks likes after implementing the code. Pagination code<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[44],"tags":[72,48],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.2.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>php pagination code example using MySql database - Vishmax.com<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vishmax.com\/en\/labs\/php-pagination-code-example-using-mysql-database\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"php pagination code example using MySql database - Vishmax.com\" \/>\n<meta property=\"og:description\" content=\"Here is a working code for pagination using PHP and MySql database. Its is tested and working fine on servers. you can set the limit of display items on page by editing the limit line. Below is the screenshot of the webpage which looks likes after implementing the code. Pagination code\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vishmax.com\/en\/labs\/php-pagination-code-example-using-mysql-database\/\" \/>\n<meta property=\"og:site_name\" content=\"Vishmax.com\" \/>\n<meta property=\"article:published_time\" content=\"2015-05-26T07:48:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-05-26T07:49:21+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.vishmax.com\/en\/innovattive-cms\/uploads\/2015\/05\/php-pagination-screenshot.jpg\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.vishmax.com\/en\/labs\/php-pagination-code-example-using-mysql-database\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.vishmax.com\/en\/labs\/php-pagination-code-example-using-mysql-database\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"http:\/\/www.vishmax.com\/en\/#\/schema\/person\/785fa94b56eb17ae7706d30777d6ab93\"},\"headline\":\"php pagination code example using MySql database\",\"datePublished\":\"2015-05-26T07:48:23+00:00\",\"dateModified\":\"2015-05-26T07:49:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.vishmax.com\/en\/labs\/php-pagination-code-example-using-mysql-database\/\"},\"wordCount\":58,\"publisher\":{\"@id\":\"http:\/\/www.vishmax.com\/en\/#organization\"},\"keywords\":[\"Code Library\",\"PHP\"],\"articleSection\":[\"Labs\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vishmax.com\/en\/labs\/php-pagination-code-example-using-mysql-database\/\",\"url\":\"https:\/\/www.vishmax.com\/en\/labs\/php-pagination-code-example-using-mysql-database\/\",\"name\":\"php pagination code example using MySql database - Vishmax.com\",\"isPartOf\":{\"@id\":\"http:\/\/www.vishmax.com\/en\/#website\"},\"datePublished\":\"2015-05-26T07:48:23+00:00\",\"dateModified\":\"2015-05-26T07:49:21+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vishmax.com\/en\/labs\/php-pagination-code-example-using-mysql-database\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vishmax.com\/en\/labs\/php-pagination-code-example-using-mysql-database\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vishmax.com\/en\/labs\/php-pagination-code-example-using-mysql-database\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/www.vishmax.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"php pagination code example using MySql database\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/www.vishmax.com\/en\/#website\",\"url\":\"http:\/\/www.vishmax.com\/en\/\",\"name\":\"Vishmax.com\",\"description\":\"Software Company in Calicut for Web Design and eCommerce\",\"publisher\":{\"@id\":\"http:\/\/www.vishmax.com\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/www.vishmax.com\/en\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/www.vishmax.com\/en\/#organization\",\"name\":\"Vishmax.com\",\"url\":\"http:\/\/www.vishmax.com\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.vishmax.com\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vishmax.com\/en\/wp-content\/uploads\/2023\/03\/logo-vishmax.png\",\"contentUrl\":\"https:\/\/www.vishmax.com\/en\/wp-content\/uploads\/2023\/03\/logo-vishmax.png\",\"width\":150,\"height\":50,\"caption\":\"Vishmax.com\"},\"image\":{\"@id\":\"http:\/\/www.vishmax.com\/en\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"http:\/\/www.vishmax.com\/en\/#\/schema\/person\/785fa94b56eb17ae7706d30777d6ab93\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.vishmax.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e218268870406c917d20b442936d9e68?s=96&d=monsterid&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e218268870406c917d20b442936d9e68?s=96&d=monsterid&r=g\",\"caption\":\"admin\"},\"url\":\"https:\/\/www.vishmax.com\/en\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"php pagination code example using MySql database - Vishmax.com","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vishmax.com\/en\/labs\/php-pagination-code-example-using-mysql-database\/","og_locale":"en_US","og_type":"article","og_title":"php pagination code example using MySql database - Vishmax.com","og_description":"Here is a working code for pagination using PHP and MySql database. Its is tested and working fine on servers. you can set the limit of display items on page by editing the limit line. Below is the screenshot of the webpage which looks likes after implementing the code. Pagination code","og_url":"https:\/\/www.vishmax.com\/en\/labs\/php-pagination-code-example-using-mysql-database\/","og_site_name":"Vishmax.com","article_published_time":"2015-05-26T07:48:23+00:00","article_modified_time":"2015-05-26T07:49:21+00:00","og_image":[{"url":"http:\/\/www.vishmax.com\/en\/innovattive-cms\/uploads\/2015\/05\/php-pagination-screenshot.jpg"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.vishmax.com\/en\/labs\/php-pagination-code-example-using-mysql-database\/#article","isPartOf":{"@id":"https:\/\/www.vishmax.com\/en\/labs\/php-pagination-code-example-using-mysql-database\/"},"author":{"name":"admin","@id":"http:\/\/www.vishmax.com\/en\/#\/schema\/person\/785fa94b56eb17ae7706d30777d6ab93"},"headline":"php pagination code example using MySql database","datePublished":"2015-05-26T07:48:23+00:00","dateModified":"2015-05-26T07:49:21+00:00","mainEntityOfPage":{"@id":"https:\/\/www.vishmax.com\/en\/labs\/php-pagination-code-example-using-mysql-database\/"},"wordCount":58,"publisher":{"@id":"http:\/\/www.vishmax.com\/en\/#organization"},"keywords":["Code Library","PHP"],"articleSection":["Labs"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.vishmax.com\/en\/labs\/php-pagination-code-example-using-mysql-database\/","url":"https:\/\/www.vishmax.com\/en\/labs\/php-pagination-code-example-using-mysql-database\/","name":"php pagination code example using MySql database - Vishmax.com","isPartOf":{"@id":"http:\/\/www.vishmax.com\/en\/#website"},"datePublished":"2015-05-26T07:48:23+00:00","dateModified":"2015-05-26T07:49:21+00:00","breadcrumb":{"@id":"https:\/\/www.vishmax.com\/en\/labs\/php-pagination-code-example-using-mysql-database\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vishmax.com\/en\/labs\/php-pagination-code-example-using-mysql-database\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vishmax.com\/en\/labs\/php-pagination-code-example-using-mysql-database\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/www.vishmax.com\/en\/"},{"@type":"ListItem","position":2,"name":"php pagination code example using MySql database"}]},{"@type":"WebSite","@id":"http:\/\/www.vishmax.com\/en\/#website","url":"http:\/\/www.vishmax.com\/en\/","name":"Vishmax.com","description":"Software Company in Calicut for Web Design and eCommerce","publisher":{"@id":"http:\/\/www.vishmax.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/www.vishmax.com\/en\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/www.vishmax.com\/en\/#organization","name":"Vishmax.com","url":"http:\/\/www.vishmax.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.vishmax.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/www.vishmax.com\/en\/wp-content\/uploads\/2023\/03\/logo-vishmax.png","contentUrl":"https:\/\/www.vishmax.com\/en\/wp-content\/uploads\/2023\/03\/logo-vishmax.png","width":150,"height":50,"caption":"Vishmax.com"},"image":{"@id":"http:\/\/www.vishmax.com\/en\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"http:\/\/www.vishmax.com\/en\/#\/schema\/person\/785fa94b56eb17ae7706d30777d6ab93","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.vishmax.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e218268870406c917d20b442936d9e68?s=96&d=monsterid&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e218268870406c917d20b442936d9e68?s=96&d=monsterid&r=g","caption":"admin"},"url":"https:\/\/www.vishmax.com\/en\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.vishmax.com\/en\/wp-json\/wp\/v2\/posts\/2695"}],"collection":[{"href":"https:\/\/www.vishmax.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.vishmax.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.vishmax.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vishmax.com\/en\/wp-json\/wp\/v2\/comments?post=2695"}],"version-history":[{"count":0,"href":"https:\/\/www.vishmax.com\/en\/wp-json\/wp\/v2\/posts\/2695\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.vishmax.com\/en\/wp-json\/wp\/v2\/media?parent=2695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vishmax.com\/en\/wp-json\/wp\/v2\/categories?post=2695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vishmax.com\/en\/wp-json\/wp\/v2\/tags?post=2695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}