Pagination example

PHP and HTML

<ul class="pagination">
    <li><a href="javascript:void(0);" class="pbutton"></a></li>
<?php 
        if(!empty($_POST['selected'])){$page = $_POST['selected'];}else{$page=1;} //selected page
    $last=6; //number of pages ( total number of articles / number of shown articles on page )
    $top = $page+2; 
    $bottom = $page-2;
    for($x=$bottom;$x<$top;$x++){
        if($top>$last){$top=$last;}
        if($x<=0){$bottom+=1;$top+=1;}                              
    }
                
    for($i=$bottom;$i<=$top;$i++){ ?>
           <li class="<?php echo ($page==$i)?"active":"";?>"><a href="javascript:void(0);" class="pbutton"><?=$i;?></a></li>
<?php 
    }
                        
    if($page!=$last){ ?>
        <li><a href="javascript:void(0);" class="pbutton last_page" id="<?=$last?>" ></a></li>
    <?php } ?>
                            
</ul>

jQuery

$(document).on("click",".pbutton",function() {
        var selected = this.text; //Selected page
        if(selected==""){selected = 1;} //first rows
        if(selected==""){selected = $(".last_page").attr("id");} //last rows
        
        $.ajax({
          url:'admin.php',
          type:'POST',
          data:{ 
            command:"show_regular_quotation", //name of var: value of var  
            selected:selected
          }  
         }).done(function(data){
            $(".content").html(data);
        });     
    });

The code is perfectly compatible with bootstrap.

Leave a Reply

Your email address will not be published. Required fields are marked *