Project

General

Profile

1
<?php
2

    
3
class m_form_DiggPagination
4
{
5

    
6
/**
7
 * Builds & returns a variable containing the HTML code to display pagination links based on given params
8
 *
9
 * @param int $page - the current page
10
 * @param int $totalitems - the number of items to paginate (not total number of pages)
11
 * @param int $limit - the number of items per page
12
 * @param int $adjacents - the number of page links to put adjacent to the current page
13
 * @param string $targetpage - URL to the web page requiring pagination links
14
 * @param string $pagestring - the URL params to pass the new page value e.g. page/ (the number is inserted at the end of the string)
15
 * @param string $cssClass - the class of the containing DIV tag for the returned pagination
16
 * @return void
17
 * @author Stranger Studios, adapted by Rich Milns
18
 * @see http://www.strangerstudios.com/sandbox/pagination/diggstyle.php
19
 */
20
  static public function Pager( $page = 1, $totalitems, $limit = 15, $adjacents = 1, $targetpage = null,
21
    $pagestring = "?page=", $cssClass = 'pagination')
22
  {
23
    $sScriptUrl = $_SERVER['SCRIPT_NAME'];
24
    $query_string = '&amp;' . preg_replace( "/page=[0-9]{0,10}&/", "", $_SERVER['QUERY_STRING']) . '#submissions';
25
    //$query_string = $_SERVER['QUERY_STRING'];
26

    
27
    //defaults
28
    if ( !$adjacents) { $adjacents = 1; }
29
    if ( !$limit) { $limit = 15; }
30
    if ( !$page) { $page = 1; }
31
    if ( !$targetpage) { $targetpage = $sScriptUrl; }
32
    if ( !isset( $margin)) { $margin = 5; }
33
    if ( !isset( $padding)) { $padding = 1; }
34
    //other vars
35
    $prev = $page - 1; //previous page is page - 1
36
    $next = $page + 1; //next page is page + 1
37
    $lastpage = ceil( $totalitems / $limit); //lastpage is = total items / items per page, rounded up.
38
    $lpm1 = $lastpage - 1; //last page minus 1
39

    
40
    /*
41
    Now we apply our rules and draw the pagination object.
42
    We're actually saving the code to a variable in case we want to draw it more than once.
43
    */
44
    $pagination = "";
45
    if ( $lastpage > 1) {
46
      $pagination .= "<div class=\"" . $cssClass . "\"";
47
      if ( $margin || $padding) {
48
        $pagination .= " style=\"";
49
        if ( $margin) { $pagination .= "margin: $margin;"; }
50
        if ( $padding) { $pagination .= "padding: $padding;"; }
51
        $pagination .= "\"";
52
      }
53
      $pagination .= ">";
54

    
55
      //previous button
56
      if ( $page > 1) {
57
        $pagination .= "<a href=\"$targetpage$pagestring$prev$query_string\">« prev</a>";
58
      } else {
59
        $pagination .= "<span class=\"disabled\">« prev</span>";
60
      }
61

    
62
      //pages
63
      if ( $lastpage < ( $adjacents * 2) + 7) //not enough pages to bother breaking it up
64
        {
65
        for ( $counter = 1; $counter <= $lastpage; $counter++) {
66
          if ( $counter == $page) {
67
            $pagination .= "<span class=\"current\">$counter</span>";
68
          } else {
69
            $pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . $query_string . "\">$counter</a>";
70
          }
71
        }
72
      } elseif ( $lastpage >= ( $adjacents * 2) + 7) //enough pages to hide some
73
      {
74
        //close to beginning; only hide later pages
75
        if ( $page < 1 + ( $adjacents * 3)) {
76
          for ( $counter = 1; $counter < 4 + ( $adjacents * 2); $counter++) {
77
            if ( $counter == $page) {
78
              $pagination .= "<span class=\"current\">$counter</span>";
79
            } else {
80
              $pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . $query_string . "\">$counter</a>";
81
            }
82
          }
83
          $pagination .= "<span class=\"elipses\">...</span>";
84
          $pagination .= "<a href=\"" . $targetpage . $pagestring . $lpm1 . $query_string . "\">$lpm1</a>";
85
          $pagination .= "<a href=\"" . $targetpage . $pagestring . $lastpage . $query_string . "\">$lastpage</a>";
86
        }
87
        //in middle; hide some front and some back
88
        elseif ( $lastpage - ( $adjacents * 2) > $page && $page > ( $adjacents * 2)) {
89
          $pagination .= "<a href=\"" . $targetpage . $pagestring . "1$query_string\">1</a>";
90
          $pagination .= "<a href=\"" . $targetpage . $pagestring . "2$query_string\">2</a>";
91
          $pagination .= "<span class=\"elipses\">...</span>";
92
          for ( $counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) {
93
            if ( $counter == $page) {
94
              $pagination .= "<span class=\"current\">$counter</span>";
95
            } else {
96
              $pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . $query_string . "\">$counter</a>";
97
            }
98
          }
99
          $pagination .= "...";
100
          $pagination .= "<a href=\"" . $targetpage . $pagestring . $lpm1 . $query_string . "\">$lpm1</a>";
101
          $pagination .= "<a href=\"" . $targetpage . $pagestring . $lastpage . $query_string . "\">$lastpage</a>";
102
        }
103
        //close to end; only hide early pages
104
        else {
105
          $pagination .= "<a href=\"" . $targetpage . $pagestring . "1$query_string\">1</a>";
106
          $pagination .= "<a href=\"" . $targetpage . $pagestring . "2$query_string\">2</a>";
107
          $pagination .= "<span class=\"elipses\">...</span>";
108
          for ( $counter = $lastpage - ( 1 + ( $adjacents * 3)); $counter <= $lastpage; $counter++) {
109
            if ( $counter == $page) {
110
              $pagination .= "<span class=\"current\">$counter</span>";
111
            } else {
112
              $pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . $query_string . "\">$counter</a>";
113
            }
114
          }
115
        }
116
      }
117

    
118
      //next button
119
      if ( $page < $lastpage - 0) {
120
        $pagination .= "<a href=\"" . $targetpage . $pagestring . $next . $query_string . "\">next »</a>";
121
      } else {
122
        $pagination .= "<span class=\"disabled\">next »</span>";
123
      }
124
      $pagination .= "</div>\n";
125
    }
126

    
127
    return $pagination;
128

    
129
  }
130
}
(1-1/28)