Bin will be removed on June 1, 2014 in favor of http://gist.github.com. Please save all your pastes before then. So long, and thanks for all the fish!×
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
 /********************************************************************
  *
  *   Pagination
  *
  ********************************************************************/
 
 /**
  * pag_npages($nrecords, $perpage) = number of pages
  */
  function pag_npages($nrecords, $perpage) {
    return ceil($nrecords / $perpage);
  }
 
 /**
  * pag_sqllimit($page, $perpage, [$npages])
  *
  * checks if $page is valid and returns 'limit x,y' for sql
  */
  function pag_sqllimit($page, $perpage, $npages = 0) {
    $page = round($page);
    if ($page<1) $page = 1;
    if ($npages && $page>$npages) $page = $npages;
    $start = ($page - 1) * $perpage;
    return "limit {$start},{$perpage}";
  }
 
 /**
  * pag_prev($curpage, $url_left, $url_right, [$linktext]) = html link to previous page
  * pag_next($curpage, $npages, $url_left, $url_right, [$linktext]) = html link to next page
  */
  function pag_prev($curpage, $url_left, $url_right, $linktext="") {
    if (!$linktext) $linktext = _t("Vorige","Veurige");
    $page = $curpage - 1;
    if ($page < 1) {
      return "<div class='disabled'>{$linktext}</div>";
    } else {
      return "<a href='{$url_left}{$page}{$url_right}'>{$linktext}</a>";
    }
  }
  function pag_next($curpage, $npages, $url_left, $url_right, $linktext="") {
    if (!$linktext) $linktext = "Volgende";
    $page = $curpage + 1;
    if ($page > $npages) {
      return "<div class='disabled'>{$linktext}</div>";
    } else {
      return "<a href='{$url_left}{$page}{$url_right}'>{$linktext}</a>";
    }
  }
 
 /**
  * pag_numbers($curpage, $npages, $url_left, $url_right, [$ray], [$prevnext]) = "1 | 2 | 3 "
  *
  * $ray = # pages to show before/after current page
  * $prevnext = true/false = add Previous / Next links
  */
  function pag_numbers($curpage, $npages, $url_left, $url_right, $ray = 3, $prevnext = true) {
    //
    $nlinks = (2 * $ray) + 1;
    $firstlink = $curpage - $ray;
    if ($firstlink < 1) $firstlink = 1;
    $lastlink = $firstlink + $nlinks - 1;
    if ($lastlink > $npages) {
      $firstlink -= $lastlink - $npages;
      if ($firstlink < 1) $firstlink = 1;
      $lastlink = $npages;
    }
    //
    $n = '<div class="pagenav">';
    if ($prevnext) {
      $n = $n . pag_prev($curpage, $url_left, $url_right) . " | ";
    }
    //
    for ($page = $firstlink; $page <= $lastlink; $page++) {
      if ($page == $curpage) {
        $links[] = $page;
      } else {
        $links[] = "<a href='{$url_left}{$page}{$url_right}'>{$page}</a>";
      }
    }
    $n .= implode($links, " | ");
    //
    if ($prevnext) $n = $n . " | " . pag_next($curpage, $npages, $url_left, $url_right);
    $n .= '</div>';
    return $n;
  }