Pagination using twitter bootstrap styles


/ Published in: PHP
Save to your folder(s)

this can be adapted by simply changing the urls. starts with an array for incoming data.


Copy this code and paste it in your HTML
  1. <?php
  2. // Set the current page at 1 if it's not already set
  3. if(!isset($_GET['p'])){$_GET['p']=1;}
  4.  
  5. $per_page = 100;
  6. $count_transactions = count($transactions);
  7. $pages = ceil($count_transactions / $per_page);
  8. $current_page = (isset($_GET['p'])) ? $_GET['p'] : 1;
  9. $clean_query_string = (isset($_GET['user_id'])) ? str_ireplace("p=".@$_GET['p']."&", "", $_SERVER['QUERY_STRING']) : str_ireplace("p=".@$_GET['p'], "", $_SERVER['QUERY_STRING']);
  10. if(substr($clean_query_string,-1)=="&"){$clean_query_string = substr_replace($clean_query_string, "", -1);}
  11.  
  12. $transactions = array_slice($transactions, ($current_page - 1) * $per_page, $per_page);
  13.  
  14. ?>
  15.  
  16. <?php if($user->is_admin() && ($count_transactions > $per_page || isset($_GET['p'])) && $pages > 1):?>
  17. <div class="pagination">
  18. <ul>
  19. <?php if($pages <= 12):?>
  20.  
  21. <li class="prev <?php if($_GET['p']==1 || !isset($_GET['p'])):?>disabled<?php endif;?>"><?php if($_GET['p']==1 || !isset($_GET['p'])):?><a href="#">&larr; Previous</a><?php else:?><a href="reports.php?p=<?php echo $_GET['p']-1;?>&<?php echo $clean_query_string;?>">&larr; Previous</a><?php endif;?></li>
  22. <?php for($i=1;$i<=$pages;$i++):?>
  23. <li class="<?php if($i==$_GET['p']) echo "active";?>"><?php if($i!=$_GET['p']):?><a href="reports.php?p=<?php echo $i;?>&<?php echo $clean_query_string;?>"><?php echo $i;?></a><?php else:?><a href="#"><?php echo $i;?></a><?php endif;?></li>
  24. <?php endfor;?>
  25. <li class="next <?php if($_GET['p']==$pages):?>disabled<?php endif;?>"><?php if($_GET['p']==$pages):?><a href="#">Next &rarr;</a><?php else:?><a href="reports.php?p=<?php echo $_GET['p']+1;?>&<?php echo $clean_query_string;?>">Next &rarr;</a><?php endif;?></li>
  26.  
  27. <?php else: ?>
  28.  
  29. <?php if($current_page <= 5):?>
  30. <li class="prev <?php if($_GET['p']==1 || !isset($_GET['p'])):?>disabled<?php endif;?>"><?php if($_GET['p']==1 || !isset($_GET['p'])):?><a href="#">&larr; Previous</a><?php else:?><a href="reports.php?p=<?php echo $_GET['p']-1;?>&<?php echo $clean_query_string;?>">&larr; Previous</a><?php endif;?></li>
  31. <?php for($i=1;$i<=$current_page+5;$i++):?>
  32. <li class="<?php if($i==$_GET['p']) echo "active";?>"><?php if($i!=$_GET['p']):?><a href="reports.php?p=<?php echo $i;?>&<?php echo $clean_query_string;?>"><?php echo $i;?></a><?php else:?><a href="#"><?php echo $i;?></a><?php endif;?></li>
  33. <?php endfor;?>
  34. <li class=""><a href="#">...</a></li>
  35. <li class=""><a href="reports.php?p=<?php echo $pages;?>&<?php echo $clean_query_string;?>"><?php echo $pages;?></a></li>
  36. <li class="next <?php if($_GET['p']==$pages):?>disabled<?php endif;?>"><?php if($_GET['p']==$pages):?><a href="#">Next &rarr;</a><?php else:?><a href="reports.php?p=<?php echo $_GET['p']+1;?>&<?php echo $clean_query_string;?>">Next &rarr;</a><?php endif;?></li>
  37. <?php endif;?>
  38.  
  39. <?php if($current_page > 5 && $current_page < $pages-5):?>
  40. <li class="prev <?php if($_GET['p']==1 || !isset($_GET['p'])):?>disabled<?php endif;?>"><?php if($_GET['p']==1 || !isset($_GET['p'])):?><a href="#">&larr; Previous</a><?php else:?><a href="reports.php?p=<?php echo $_GET['p']-1;?>&<?php echo $clean_query_string;?>">&larr; Previous</a><?php endif;?></li>
  41. <?php if($current_page>=7):?>
  42. <li class=""><a href="reports.php?p=1&<?php echo $clean_query_string;?>">1</a></li>
  43. <li class=""><a href="#">...</a></li>
  44. <?php endif;?>
  45. <?php for($i=$current_page-5;$i<=$current_page+5;$i++):?>
  46. <li class="<?php if($i==$_GET['p']) echo "active";?>"><?php if($i!=$_GET['p']):?><a href="reports.php?p=<?php echo $i;?>&<?php echo $clean_query_string;?>"><?php echo $i;?></a><?php else:?><a href="#"><?php echo $i;?></a><?php endif;?></li>
  47. <?php endfor;?>
  48. <?php if($current_page<=$pages-6):?>
  49. <li class=""><a href="#">...</a></li>
  50. <li class=""><a href="reports.php?p=<?php echo $pages;?>&<?php echo $clean_query_string;?>"><?php echo $pages;?></a></li>
  51. <?php endif;?>
  52. <li class="next <?php if($_GET['p']==$pages):?>disabled<?php endif;?>"><?php if($_GET['p']==$pages):?><a href="#">Next &rarr;</a><?php else:?><a href="reports.php?p=<?php echo $_GET['p']+1;?>&<?php echo $clean_query_string;?>">Next &rarr;</a><?php endif;?></li>
  53. <?php endif;?>
  54.  
  55. <?php if($current_page >= $pages-5):?>
  56. <li class="prev <?php if($_GET['p']==1 || !isset($_GET['p'])):?>disabled<?php endif;?>"><?php if($_GET['p']==1 || !isset($_GET['p'])):?><a href="#">&larr; Previous</a><?php else:?><a href="reports.php?p=<?php echo $_GET['p']-1;?>&<?php echo $clean_query_string;?>">&larr; Previous</a><?php endif;?></li>
  57. <li class=""><a href="reports.php?p=1&<?php echo $clean_query_string;?>">1</a></li>
  58. <li class=""><a href="#">...</a></li>
  59. <?php for($i=$current_page-5;$i<=$pages;$i++):?>
  60. <li class="<?php if($i==$_GET['p']) echo "active";?>"><?php if($i!=$_GET['p']):?><a href="reports.php?p=<?php echo $i;?>&<?php echo $clean_query_string;?>"><?php echo $i;?></a><?php else:?><a href="#"><?php echo $i;?></a><?php endif;?></li>
  61. <?php endfor;?>
  62. <li class="next <?php if($_GET['p']==$pages):?>disabled<?php endif;?>"><?php if($_GET['p']==$pages):?><a href="#">Next &rarr;</a><?php else:?><a href="reports.php?p=<?php echo $_GET['p']+1;?>&<?php echo $clean_query_string;?>">Next &rarr;</a><?php endif;?></li>
  63. <?php endif;?>
  64.  
  65. <?php endif;?>
  66. </ul>
  67. </div>
  68. <?php endif;?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.