Simple page navigation in php

  • 3 years-4 months

    Simple page navigation in php

    • Category : php
    • Viewed : 71 times
    • Posted On :
    • Posted by : Guley
    $num_rec_per_page=4;
        if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
        $start_from = ($page-1) * $num_rec_per_page; 
    $q="select * from tablename LIMIT $start_from, $num_rec_per_page";
    $res=mysql_query($q);
    while($r=mysql_fetch_array($res))
    {
    //Enter Your code
    }
      <ul class="pagination">
            <?php 
             $sql = "SELECT * FROM `tablename`"; 
               $rs_result = mysql_query($sql); //run the query
               $total_records = mysql_num_rows($rs_result);  //count number of records
               $total_pages = ceil($total_records / $num_rec_per_page); 
             echo "<li><a href='&page=1'>".'&laquo;'."</a></li> "; // Goto 1st page  
            for ($i=1; $i<=$total_pages; $i++) { 
              echo "<li><a href='&page=".$i."'>".$i."</a></li> "; 
          }; 
             echo  "<li><a href='&page=$total_pages'>".'&raquo;'."</a></li> "; // Goto last page
    ?>
    </ul>
    

Report this or Ask your own question

Your Answer

When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.