title: Pagination last page and first page php
body: <p>I am new to pagination and have managed to get this:</p>
  
  <p><a href="https://i.stack.imgur.com/9wa9M.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9wa9M.png" alt="enter image description here"></a></p>
  
  <p>Now i want if the page is = 0 it must go back to page 1, and the last page should just be looping</p>
  
  <p>i want the following :</p>
  
  <p>(1) if no page var is given, default to 1.
  (2) previous page is page - 1
  (3) previous page is page - 1
  (4) next page is page + 
  (5) last page should be equals to $total_pages</p>
  
  <pre><code>   &lt;?php
        $limit = 6;
        if (isset($_GET["page"]))
         {
         $page  = $_GET["page"];
         } else
         {
        $page = 1;
         };
  
       $total_records = $pagination;
       $total_pages = ceil($total_records/$limit);
  
       if($page = 0)
       $page = 1;     
       $prev = $page - 1;                          
       $next = $page + 1;                        
  
        $pagLink = "&lt;ul class='pagination' class='justify-content-center'&gt;";
  
        $pagLink .= "&lt;li class='page-item'&gt;
        &lt;a class='page-link' href='home.php?page=".$prev."'   aria-label='Back'&gt;
        &lt;span aria-hidden='true'&gt;&amp;laquo;&lt;/span&gt;
        &lt;span class='sr-only'&gt;Next&lt;/span&gt;
    &lt;/a&gt;
  &lt;/li&gt;";
  
    for ($i=1; $i&lt;=$total_pages; $i++) {  
  
    $pagLink .= "&lt;li class='page-item'&gt;&lt;a  class='page-link'    href='home.php?page=".$i."'&gt;".$i."&lt;/a&gt;&lt;/li&gt; ";
  
     };
  
  
       $pagLink .= "&lt;li class='page-item'&gt;
    &lt;a class='page-link' href='home.php?page=".$next."' aria-label='Next'&gt;
      &lt;span aria-hidden='true'&gt;&amp;raquo;&lt;/span&gt;
      &lt;span class='sr-only'&gt;Next&lt;/span&gt;
    &lt;/a&gt;
    &lt;/li&gt;";
    echo $pagLink . "&lt;/ul&gt;";
  
     ?&gt;
  </code></pre>