Posted By

brownrl on 08/23/13


Tagged

php pagination sublime


Versions (?)

Sublime Snippet: pageit


 / Published in: PHP
 

URL: http://www.itsgotto.be/cv.php

This is a quick sublime snippet to quickly setup a DB table for pagination. Ideally from here you will be into a smarty template.

  1. <snippet>
  2. <content><![CDATA[
  3.  
  4. \$offset = 0;
  5. \$page = 1;
  6. \$pp = 6;
  7. \$table = "users";
  8. \$field = "user_created_timestamp";
  9. \$dir = "DESC";
  10.  
  11. if( isset( \$_GET['p'] ) ) {
  12. \$page = intval( \$_GET['p'] );
  13. \$offset = ( \$page - 1 ) * \$pp;
  14. }
  15.  
  16. // get the total
  17. \$q = "SELECT COUNT( * ) AS total FROM \$table";
  18. \$qr = dbQuery( \$q );
  19. \$qrow = dbFetchArray( \$qr );
  20. \$total = \$qrow['total'];
  21.  
  22.  
  23. // Do the real select
  24. \$q = "SELECT * FROM ".\$table." ORDER BY ".\$field." ".\$dir." LIMIT ".\$pp." OFFSET ".\$offset;
  25. \$qr = dbQuery( \$q );
  26.  
  27. \$assigns['total'] = \$total;
  28. \$totalpages = ceil( \$total / \$pp );
  29. \$assigns['totalpages'] = \$totalpages;
  30. \$assigns['items'] = dbFetchAllArray( \$qr );
  31.  
  32. \$prev = false;
  33. \$first = false;
  34. \$next = true;
  35. \$last = true;
  36.  
  37. if( \$offset != 0 ) {
  38. \$prev = true;
  39. \$first = true;
  40. }
  41.  
  42. if( \$page == \$totalpages ) {
  43. \$next = false;
  44. \$last = false;
  45. }
  46.  
  47. \$assigns['prevpage'] = \$page - 1;
  48. \$assigns['nextpage'] = \$page + 1;
  49. \$assigns['first'] = \$first;
  50. \$assigns['prev'] = \$prev;
  51. \$assigns['next'] = \$next;
  52. \$assigns['last'] = \$last;
  53.  
  54.  
  55.  
  56. ]]></content>
  57. <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
  58. <tabTrigger>pageit</tabTrigger>
  59. <!-- Optional: Set a scope to limit where the snippet will trigger -->
  60. <!-- <scope>source.python</scope> -->
  61. </snippet>

Report this snippet  

You need to login to post a comment.