Pagination function

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Jasper DS

Jasper DS

07/01/2012 12:06:13
Quote Anchor link
Er loopt iets mis met mijn paginate funtie.
Alles gaat goed totdat er meerdere pagina's komen en er pagina's worden weggelaten.

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
function paginate($limiet, $pagina, $total_pages, $html)
{
                    
    // Initial page num setup
    if($pagina == 0)
    {

        $pagina = 1;
    }

                        
    $prev = $pagina - 1;
    $next = $pagina + 1;
                        
    $lastpage = ceil($total_pages/$limiet);
    $LastPagem1 = $lastpage - 1;                    

    $paginate = '';
    
    if($lastpage > 1)
    {
    

        $paginate .= '<div class="paginate">';
                            
        // Previous
        if ($pagina > 1)
        {

            $paginate .= '<a href="'.$html.'?id='.$_GET['id'].'&amp;page='.$prev.'"> Vorige</a>';
        }

        else
        {
            $paginate.= '<span class="disabled"> Vorige </span>';    
        }

        
        $stages = "";
    
        // Pages
        if ($lastpage < 7 + ($stages * 2))    // Not enough pages to breaking it up
        {
            for ($counter = 1; $counter <= $lastpage; $counter++)
            {

                if ($counter == $pagina)
                {

                    $paginate .= '<span class="current">'.$counter.'</span>';
                }

                else
                {
                    $paginate .= '<a href="'.$html.'?id='.$_GET['id'].'&amp;page='.$counter.'"> '.$counter .'</a>';
                }
            }
        }

        elseif($lastpage > 5 + ($stages * 2))    // Enough pages to hide a few?
        {
            // Beginning only hide later pages
            if($pagina < 1 + ($stages * 2))
            {

                for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
                {

                    if ($counter == $pagina)
                    {

                        $paginate.= '<span class="current"> '.$counter.' </span>';
                    }

                    else
                    {
                        $paginate.= '<a href="'.$html.'?id='.$_GET['id'].'&amp;page='.$counter.'"> '.$counter.' </a>';
                    }
                }

                    
                $paginate.= '...';
                $paginate.= '<a href="'.$html.'?id='.$_GET['id'].'&amp;page='.$LastPagem1.'"> '.$LastPagem1.' </a>';
                $paginate.= '<a href="'.$html.'?id='.$_GET['id'].'&amp;page='.$LastPagem1.'"> '.$lastpage.' </a>';
            }

            elseif($lastpage - ($stages * 2) > $pagina && $pagina > ($stages * 2))
            {

                $paginate.= '<a href="'.$html.'?id='.$_GET['id'].'&amp;page=1"> 1 </a>';
                $paginate.= '<a href="'.$html.'?id='.$_GET['id'].'&amp;page=2"> 2 </a>';
                                    
                $paginate.= '...';
                for ($counter = $pagina - $stages; $counter <= $pagina + $stages; $counter++)
                {

                    if ($counter == $pagina)
                    {

                        $paginate.= '<span class="current"> '.$counter.' </span>';
                    }

                    else
                    {
                        $paginate.= '<a href="'.$html.'?id='.$_GET['id'].'&amp;page='.$counter.'"> '.$counter .'</a>';
                    }
                }

                                    
                $paginate.= '...';
                $paginate.= '<a href="'.$html.'?id='.$_GET['id'].'&amp;page='.$LastPagem1.'"> '.$LastPagem1.' </a>';
                $paginate.= '<a href="'.$html.'?id='.$_GET['id'].'&amp;page='.$lastpage.'"> '.$lastpage.' </a>';
            }

            else
            {
                $paginate.= '<a href="'.$html.'?id='.$_GET['id'].'&amp;page=1"> 1</a>';
                $paginate.= '<a href="'.$html.'?id='.$_GET['id'].'&amp;page=2"> 2</a>';
                $paginate.= '...';
                for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
                {

                    if ($counter == $pagina)
                    {

                        $paginate.= '<span class="current"> '.$counter.' </span>';
                    }

                    else
                    {
                        $paginate.= '<a href="'.$html.'?id='.$_GET['id'].'&amp;page='.$counter.'"> '.$counter.' </a>';
                    }
                }
            }
        }


        // Next
        if ($pagina < $counter - 1)
        {

            $paginate.= '<a href="'.$html.'?id='.$_GET['id'].'&amp;page='.$next.'"> Volgende</a>';
        }

        else
        {
            $paginate.= '<span class="disabled"> Volgende</span>';
        }


        $paginate.= '</div>';        

    }

    
    return $paginate;
}


?>


Afbeelding
Gewijzigd op 08/01/2012 23:35:09 door Joren de Wit
 
PHP hulp

PHP hulp

28/03/2024 17:12:11
 
Joren de Wit

Joren de Wit

08/01/2012 23:35:54
Quote Anchor link
Topic op verzoek weer geopend.
 
Synaps Framework

Synaps Framework

09/01/2012 09:30:31
Quote Anchor link
PHP Jasper op 07/01/2012 12:06:13:
Alles gaat goed totdat er meerdere pagina's komen en er pagina's worden weggelaten.


Dan werkt de gehele functie toch niet ;). Waar de functie voor gemaakt is, gaat het mis :p
 
Jasper DS

Jasper DS

09/01/2012 17:07:31
Quote Anchor link
@Synaps, ja maar ik vind het zonde op heel de functie weg te doen want ik denk dat het maar een kleine misser is. Zo lang de functie de getallen niet opsplitst werk alles wel.
 
Erwin H

Erwin H

09/01/2012 17:09:11
Quote Anchor link
1) waar gaat het mis
2) een tip: bereken eerst alle getallen (vorige, volgende etc), maak de HTML buttons pas aan nadat je dat allemaal gedaan hebt. Dat scheelt je een hoop code en het is veel makkelijker om de fout te vinden.
 
Jasper DS

Jasper DS

09/01/2012 17:42:35
Quote Anchor link
Erwin,

de code loopt mis vanaf lijn 50 tot lijn 110. Dus vanaf er een paar weggelaten worden.
Onderstaande code werkt dus wel perfect.

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?php
function paginate($limiet, $pagina, $total_pages, $html)
{
                    
    # Indien pagina gelijk is aan nul, instellen op 1
    if($pagina == 0)
    {

        $pagina = 1;
    }

    
    # Volgende en vorige instellen
    $prev = $pagina - 1;
    $next = $pagina + 1;
    
        
    $lastpage = ceil($total_pages/$limiet);
    $LastPagem1 = $lastpage - 1;                    

    $paginate = '';
    
    # Indien er voldoende records zijn om pagina's aan te maken
    if($lastpage > 1)
    {
                    
        # Vorige
        if ($pagina > 1)
        {

            # Er is geen vorige pagina -> disabled
            $vorige = '<a href="'.$html.'?pagina='.$prev.'"> Vorige</a>';
        }

        else
        {
            # Er is wel een vorige pagina
            $vorige = '<span class="disabled"> Vorige </span>';    
        }

        
        # Volgende
        if ($pagina < $lastpage)
        {

            # Er is wel een volgende pagina
            $volgende = '<a href="'.$html.'?pagina='.$next.'"> Volgende</a>';
        }

        else
        {
            # Er is geen volgende pagina
            $volgende = '<span class="disabled"> Volgende</span>';
        }
    
        
        # Cijfers voor volgende pagina's
        if ($lastpage < 7)
        {

            # Er zijn te weinig pagina's om op te splitsen
            
            # Maak var aan

            $paginas = '';
            
            for ($teller = 1; $teller <= $lastpage; $teller++)
            {

                if ($teller == $pagina)
                {

                    # Huidige pagina
                    $paginas .= '<span class="current">'.$teller.'</span>';
                }

                else
                {
                    # Andere pagina's
                    $paginas .= '<a href="'.$html.'?pagina='.$teller.'"> '.$teller .'</a>';
                }
            }
        }

        else
        {
            # we splitsen op en laten een paar cijfers weg
        }
?>


In dit geval duidt de 7 op lijn 48 aan wanneer er pagina's moeten verstopt worden.
Gewijzigd op 09/01/2012 17:44:06 door Jasper DS
 
Erwin H

Erwin H

09/01/2012 18:01:42
Quote Anchor link
Wat bedoel je met verstopt worden? Kan je een voorbeeld geven van wat je op je scherm wilt zien, zoiets als:

1...5,6,7,8,9...90

Wat is de betekenis/waarde van $stages en van $pagina?
 
Jasper DS

Jasper DS

09/01/2012 18:05:07
Quote Anchor link
Nee niet helemaal, zoals je onderaan mijn eerste post kan zien.
[Vorige][1][2]...[HUIDIGE PAGINA]...[VOORLAATSTE PAGINA][LAATSTE PAGINA][Volgende]

Stages is leeg en pagina word toegevoegd als paramater via $_GET.
Gewijzigd op 09/01/2012 18:06:00 door Jasper DS
 
Erwin H

Erwin H

09/01/2012 18:13:53
Quote Anchor link
Nu begrijp ik wat dat plaatje onderaan je post is :-)

Toevoeging op 09/01/2012 18:17:03:

Wat wil je met deze regels doen:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
<?php
        $stages
= "";
    
        // Pages
        if ($lastpage < 7 + ($stages * 2))
?>

Stages is een lege string en die ga je vermenigvuldigen met 2. Ik kan dat niet plaatsen. Regel 33/36
 
Jasper DS

Jasper DS

09/01/2012 20:58:43
Quote Anchor link
Ja, deze kijkt na hoeveel pagina's er een aantal pagina's moeten weggestoken worden. Zoals je ziet word stages ook aangepast naar een andere waarde a.d.h.v. het blokje waar het zich bevind.
 
Kris Peeters

Kris Peeters

09/01/2012 21:16:55
Quote Anchor link
Ben je hier iets mee?

Ongetwijfeld is dit soort class al x aantal keer gemaakt, maar ja ... toch maar eens beginnen schrijven.

Ik heb er niet te veel over nagedacht, ongetwijfeld kan het nog verbeterd worden.

Any how; het werkt wel.

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php

/**
*  Callback.  This callback will be called by the pagination class.
*  Do not change the number of parameters.
*  The pagination class expects this function to return a string containing the link (href) to the right page.
*  example: $number=5; the page is a feed of news items => the function returns 'http://my_site.com/?action=newsitems&page=5'
*  The reason for doing this with a callback is so you don't have to modify the class.  This also alows reusing the class.
*/

function my_links($number) {
  return '?page=' . $number;
}


$pag = new pagination();
$pag->items_per_page(30)        // zelf te kiezen
    ->total_items(816)          // haal je bv uit "SELECT COUNT(id) as FROM newsitems ... "
    ->current_link(isset($_GET['page']) ? (int) $_GET['page'] : 1);    // hangt af van je structuur van opbouwen van url's.  Zie ook de callback my_links, bovenaan
$pagination_string =  $pag->generate_links('my_links') .'<br/>';


echo
'<html>
  <head>
    <style>
      a {
        text-decoration: none;
        color: #000;
      }
      .pagination  span{
        display: block;
        float: left;
        width: 30px;
        text-align: center;
        margin: 6px;
        background-color: #999999;
     }
      .pagination .active {
        background-color: #ffff00;
      }
      .pagination .next, .pagination .previous {
        background-color: #A8752F;
      }
      .pagination .first, .pagination .last {
        background-color: #ff3355;
      }
       .pagination .etc {
        background-color: #f0f0f0;
      }
   </style>
  </head>
  <body>
    <div class="pagination">
'
. $pagination_string . '
    </div>
    <div style="clear: both;"></div>
    '
;
echo "SELECT id FROM newsitems ORDER BY date_created LIMIT " . $pag->limit();

echo '  </body>
</html>'
;
//echo '<pre>' . print_r($pag, 1) . '</pre>';

    
class pagination {

  protected $itemsPerPage;  // We have a fixed number of items (news items / articles / ...) per page
  protected $totalItems;    // Total number of items.  This information comes from the DB
  protected $currentLink;   //
  
  public $linksVisibleRadius;  // number of numbered links visible.  example: if set to 5,
  
  public function __construct() {
    $this->itemsPerPage         = 20;  
    $this->totalItems           = 0;  
    $this->currentLink          = 0;  
    $this->linksVisibleRadius   = 3;  // number of links to the left and to the right of the current link.
  }
  public function number_of_links() {
    return (int) ($this->totalItems / $this->itemsPerPage);
  }

  public function limit() {
    return $this->items_per_page() * ($this->current_link() - 1) . ', ' . $this->items_per_page();
  }

  public function generate_links($callback) {
    $numeric_links = "";
    for ($i = ($this->currentLink - $this->linksVisibleRadius) ; $i <= ($this->currentLink + $this->linksVisibleRadius); $i++) {
      if ($i == 1) {
        $nothing_left = 1;
      }

      if ($i == $this->number_of_links()) {
        $nothing_right = 1;
      }

      if ($this->page($i) != $i) {
        // if you are here, this means the page is beyond the range.  We don't need this link
        continue;
      }

      $numeric_links .= ' <span' . ($this->page($i) == $this->currentLink  ? ' class="active"' : '') . '><a href="' . $callback($this->page($i)) . '" >' . $this->page($i) . '</a></span> ';      
    }

    $content = '';
    $content .= ' <span class="first"><a href="' . $callback(1) . '" title="First page (1)">&#171;</a></span> ';  // first link
    $content .= ' <span class="previous"><a href="' . $callback($this->page($this->currentLink - 1)) . '" title="Previous page">&lt;</a></span> ';       // Previous
    if (empty($nothing_left)) {
      $content .=  ' <span class="etc">...</span> ';
    }

    $content .=  $numeric_links;
    if (empty($nothing_right)) {
      $content .=  ' <span class="etc">...</span> ';
    }

    $content .= ' <span class="next"><a href="' . $callback($this->page($this->currentLink + 1)) . '" title="Next page">&gt;</a></span> ';       // next
    $content .= ' <span class="last"><a href="' . $callback($this->number_of_links()) . '" title="Last page (' . $this->number_of_links() . ')"> &#187; </a></span> ';   // last link
    return $content;
  }

  /**
  *   this function usually just returns the parameter $delta .  The function makes sure the page is within range  
  */

  public function page($page) {
    $page = (int) $page;
    // first check if the current link is not beyond the range
    if ($page > $this->number_of_links()) {
      return $this->number_of_links();
    }

    elseif ($page < 1) {
      return 1;
    }

    return $page;
  }

  /*
  *  GETTER / SETTER function
  **/

  public function items_per_page($items=null) {
    if (empty($items)) {
      // getter
      return $this->itemsPerPage;
    }

    else {
      // setter
      if ($items > 0) {
        $this->itemsPerPage = (int) $items;
      }

    return $this;
    }
  }

  /*
  *  GETTER / SETTER function
  **/

  public function total_items($items=null) {
    if (empty($items)) {
      // getter
      return $this->totalItems;
    }

    else {
      // setter
      if ($items > 0) {
        $this->totalItems = (int) $items;
      }

    return $this;
    }
  }

  /*
  *  GETTER / SETTER function
  **/

  public function current_link($link=null) {
    if (empty($link)) {
      // getter
      return $this->currentLink;
    }

    else {
      // setter
      $this->currentLink = $this->page($link);
      return $this;
    }
  }
}

?>


Als niet alles volledig duidelijk is, vraag maar
Gewijzigd op 09/01/2012 21:19:38 door Kris Peeters
 
Jasper DS

Jasper DS

09/01/2012 21:24:26
Quote Anchor link
Bedankt Kris, dit kan ik wel gebruiken. Ik doe er misschien nog enkele kleine aanpassingen aan om dat ik eigenlijk nog niet echt met class bezig ben maar ik bewaar deze zeker voor later. Ik ga er nu één functie van proberen maken. Bedankt.
 
Wouter J

Wouter J

09/01/2012 22:01:08
Quote Anchor link
@Jasper, dit is slechts een class en geen OO, je kunt ook classes gebruiken zonder OO, maar gewoon als verzameling van functies. Maar daar zijn tegenwoordig eigenlijk namespaces uitgevonden...
 
Jasper DS

Jasper DS

09/01/2012 22:06:12
Quote Anchor link
Ja maar ik vind dat het dan ook niet netjes staat in mijn code.. :s Plotseling zo'n class tussen mijn functies.
 
Erwin H

Erwin H

09/01/2012 22:06:33
Quote Anchor link
PHP Jasper op 09/01/2012 20:58:43:
Ja, deze kijkt na hoeveel pagina's er een aantal pagina's moeten weggestoken worden. Zoals je ziet word stages ook aangepast naar een andere waarde a.d.h.v. het blokje waar het zich bevind.

Maar je zet stages eerst naar "" en dan gebruik je het in een berekening. Dat kan natuurlijk nooit, want stages is op dat punt ALTIJD "" en ($stages * 2) is dus altijd 0 (of eigenlijk niet gedefinieerd). Wat je daar dus doet is simpelweg fout, of op zijn minst overbodig.



Toevoeging op 09/01/2012 22:08:42:

En verder wordt stages ook nooit meer aangepast in je code, terwijl je het regelmatig gebruikt. Ik vermoed dus dat daar je fout zit, ook al wil je er zelf blijkbaar niet eens naar kijken.
 
Jasper DS

Jasper DS

09/01/2012 22:13:19
Quote Anchor link
Erwin, deze code is al een tijdje oud en ik heb deze ooit al eens bewerkt.. Geen idee of de fout er toen al in zat. Maar nu was ik dit lapje code aan het opkuisen en ik dacht dat er gewoon ergens een stomme fout zat maar blijkbaar is heel de opzet fout?
 
Erwin H

Erwin H

09/01/2012 23:13:15
Quote Anchor link
Of de hele opzet fout is weet ik niet, wat in elk geval fout is is de initialisatie van $stages. Ik heb het idee dat dat ergens uit berekend moet worden. Alleen hoe dat weet ik niet zo 1-2-3, daarvoor ken ik je code niet goed genoeg uiteraard.
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.