pagina-nummers-class

Gesponsorde koppelingen

PHP script bestanden

  1. pagina-nummers-class

« Lees de omschrijving en reacties

De Pagenumber Class:

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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php

/********
*    Show all errors
********/

error_reporting(E_ALL);
ini_set("display_errors", 1);


/********
*    Define Some vars
********/

define("MIN_PER_PAGE", 5);
define("MAX_PER_PAGE", 40);
define("DEF_PER_PAGE", 15);
define("THIS_PAGE", $_SERVER['PHP_SELF']);


/********
*    CLASS PAGE NUMBERS
********/

class Page_numbers
{
    var
$table;
    var
$condition;
    var
$link_id;
    var
$total_records;
    var
$mpp;
    var
$total_pages;
    var
$page;
    var
$offset;
    var
$query_string;


    /********
    *    Constructor, setting some vars
    ********/

    function Page_numbers($table, $condition="", $link_id=NULL)
    {

        $this->table = $table;
        $this->condition = $condition;
        $this->link_id = $link_id;
        $this->total_records = $this->count_records();
        $this->mpp = isset($_GET['mpp']) && is_numeric($_GET['mpp']) && $_GET['mpp'] >= MIN_PER_PAGE && $_GET['mpp'] <= MAX_PER_PAGE ? $_GET['mpp'] : DEF_PER_PAGE;
        $this->total_pages = ceil($this->total_records / $this->mpp);
        $this->page = isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] > 0 && $_GET['page'] <= $this->total_pages ? $_GET['page'] : 1;
        $this->offset = ($this->page - 1) * $this->mpp;
        $this->query_string = $this->get_query_string();
    }



    /********
    *    Records tellen voor het uitrekenen van de pagenumbers
    ********/

    function count_records()
    {

        $res = @mysql_query("SELECT count(*) FROM ".$this->table." ".$this->condition, $this->link_id);
        return @mysql_result($res, 0, "count(*)");
    }



    /********
    *    Data uit de database trekken van het pagenummer waar we op zitten
    ********/

    function fetch_data($fields="*", $order="")
    {

        $res = @mysql_query("SELECT ".$fields." FROM ".$this->table." ".$this->condition." ".$order." LIMIT ".$this->offset.",".$this->mpp);
        while($row = mysql_fetch_assoc($res))
        {

            $data[] = $row;
        }

        return $data;
    }



    /********
    *    query sting opmaken
    ********/

    function get_query_string($query_string="")
    {

        foreach($_GET as $key => $value)
        {

            if($key != 'page' && $key != 'mpp')
            {

                $query_string .= '&amp;'.$key.'='.$value;
            }
        }

        return $query_string;
    }



    /********
    *    Message Per Page upmaken en terug geven.
    ********/

    function show_mpp()
    {

        $str = '<script type="text/javascript" language="javascript1.5">
        <!--
        function openUrl()
        {
            var control = document.getElementById(\'mpp\');
            window.location = "'
.THIS_PAGE.'?page=1&mpp="+control.options[control.selectedIndex].value+"'.str_replace('&amp;', '&', $this->query_string).'";
        }
        //-->
        </script>
        <select id="mpp" onchange="openUrl();">'
;
        
        for($i=MIN_PER_PAGE; $i<=MAX_PER_PAGE; $i+=5)
        {

            $str .= '<option value="'.$i.'"'.($i == $this->mpp ? ' selected="selected"': '').'>'.$i.'</option>'."\r\n";
        }

        
        return $str.'</select>';
    }
        


    /********
    *    Previous & Next links
    ********/

    function prev_next()
    {

        $str = ($this->page > 1) ? '<a href="index.php?page='.($this->page-1).'&amp;mpp='.$this->mpp.$this->query_string.'" title="Vorige Pagina">&laquo;&laquo;</a>' : '<span style="color:#aaa">&laquo;&laquo;</span>';
        $str .= '&nbsp;&nbsp;&nbsp;';
        $str .= ($this->page < $this->total_pages) ? '<a href="index.php?page='.($this->page+1).'&amp;mpp='.$this->mpp.$this->query_string.'" title="Volgende Pagina">&raquo;&raquo;</a>' : '<span style="color:#aaa">&raquo;&raquo;</span>';
    
        return $str;
    }



    /********
    *    Pagenumbers opmaken en uitspugen
    ********/

    function show_page_numbers($num_page_links=7)
    {

        if($this->total_pages > 1)
        {

            $num_page_links = $num_page_links % 2 ? $num_page_links : $num_page_links + 1;
            
            $pagenumbers = 'Pagina: <strong>'.$this->page.'</strong> van '.$this->total_pages.'<br />';
            
            if($this->total_pages > $num_page_links)
            {

                
                $cutoff = floor($num_page_links / 2);
                
                $start = $this->page - $cutoff;
                $end   = $this->page + $cutoff;


                /********
                *    No Pagenumbers Less then 1 && Greater then total_pages
                ********/

                while($start < 1)                    { $start++; $end++; }
                while($end > $this->total_pages)    { $start--; $end--; }


                /********
                *    Pagina nummers opmaken en uitspugen
                ********/

                if($this->page > $cutoff + 1) { $pagenumbers .= '<a href="'.THIS_PAGE.'?page=1&amp;mpp='.$this->mpp.$this->query_string.'" title="First Page (1)">...</a>&nbsp; '; }
                
                for($i=$start; $i<=$end; $i++)
                {

                    $pagenumbers .= ($i == $this->page) ? '<strong style="text-decoration:underline;">'.$i.'</strong>&nbsp; '."\r\n" : '<a href="'.THIS_PAGE.'?page='.$i.'&amp;mpp='.$this->mpp.$this->query_string.'" title="Go to Page '.$i.'">'.$i.'</a>&nbsp; '."\r\n";
                }

                
                if($this->page < $this->total_pages - $cutoff) { $pagenumbers .= '<a href="'.THIS_PAGE.'?page='.$this->total_pages.'&amp;mpp='.$this->mpp.$this->query_string.'" title="Last Page ('.$this->total_pages.')">...</a>&nbsp; '; }
                
            }

            else
            {
                for($i=1; $i<=$this->total_pages; $i++)
                {

                    $pagenumbers .= ($i == $this->page) ? '<strong style="text-decoration:underline;">'.$i.'</strong>&nbsp; '."\r\n" : '<a href="'.THIS_PAGE.'?page='.$i.'&amp;mpp='.$this->mpp.$this->query_string.'" title="Go to Page '.$i.'">'.$i.'</a>&nbsp; '."\r\n";
                }
            }

            return rtrim($pagenumbers);
        }

        else
        {
            return NULL;
        }
    }


}

/* END CLASS */

?>



Het aanroepen / inbouwen van de class (voorbeeld)

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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Pagenumber Class - by Nano 2006</title>
<style type="text/css">
body {
    font-family:Verdana, Arial, Helvetica, sans-serif;
    font-size:12px;
    color:#006;
    line-height:18px;
    margin:40px;
}
a {
    color:#08f;
    text-decoration:none;
}
a:hover {
    color:#f00;
}
h1 {
    font-size:24px;
    color:#369;
    margin:0px 0px 30px 0px;
}
</style>
</head>
<body>
<h1>Page Number Class</h1>
<?php


/********
*    Connecten met mysql
********/

$link_id = @mysql_connect("localhost", "***", "***");
@
mysql_select_db("***", $link_id);



/********
*    Pagina nummer class aanroepen
********/

@require("class_pagenumbers.php");
$page_nums = new Page_numbers("guestbook", "WHERE blocked='N'", $link_id);

$page_numbers = $page_nums->show_page_numbers(7);
$mpp = $page_nums->show_mpp();
$prev_next = $page_nums->prev_next();
$data = $page_nums->fetch_data("name", "ORDER BY id DESC");



/********
*    Pagina Nummers en Berichten Per Pagina uitspugen naar browser
********/

echo '<table cellpadding="0" cellspacing="0" style="width:450px; margin-bottom:30px;">
    <tr>
        <td>'
.$page_numbers.'</td>
        <td align="right" valign="bottom">Berichten Per Pagina: '
.$mpp.'</td>
    </tr>
</table>'
;



/********
*    Data uitspugen naar de browser
********/

foreach($data as $key => $array)
{

    echo $array['name']."<br />\r\n";
}




/********
*    Previous & Next Links
********/

echo '<br /><br />'.$prev_next;


?>

</body>
</html>

 
 

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.