pagina-nummers-plug-and-play-rc2

Gesponsorde koppelingen

PHP script bestanden

  1. pagina-nummers-plug-and-play-rc2

« Lees de omschrijving en reacties

## index.php (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
<?php
#-->    setting enviroment (just for development)
error_reporting(E_ALL);
ini_set("display_errors", 1);


#-->    even zelf invullen
$mysql['host']  = "localhost";    #-->   localhost is meetstal goed
$mysql['user']  = "";                #-->   user om te connecten met mysql
$mysql['pass']  = "";                #-->   pass om te connecten met mysql
$mysql['db']    = "";                 #-->   database die je wil selecteren
$mysql['table'] = "";                 #-->   table die je wil gaan gebruiken


#-->    je query ( this is just an example - !!! GEEN LIMIT GEBRUIKEN HIER !!! )

$sql = "SELECT * FROM `".$mysql['table']."` ORDER BY `id` DESC";


#-->    loading file
require('pagenummers.php');


#-->    connecten met mysql en db selecteren
@mysql_connect($mysql['host'], $mysql['user'], $mysql['pass']) or die(mysql_error());    
mysql_select_db($mysql['db']) or die(mysql_error());


#-->    assigning pagenumbers
$page_nummers = page_nummers($mysql['table'], $sql, 1, 1, 1, 1, 0);


#-->    to see what it returns (debugging only)
//    view_array($page_nummers);



?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Page Numbers met dropdownbox</title>
<link rel="stylesheet" type="text/css" href="style.css">
<SCRIPT type="text/javascript" language="JavaScript">
<!--
function openURI(){
var control = document.form.documentkeuze;
if (control.options[control.selectedIndex].value != ' ') {
location.href = control.options[control.selectedIndex].value;
}
}
-->
</SCRIPT>
</head>
<body>
<center>
<table width="500" cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="top" align="left">
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
<?php

    echo $page_nummers['page_numbers'];
    echo '<br><br>';
    echo $page_nummers['prev_next'];

?>

</td>
<td class="top" align="right">
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
<?php

    echo $page_nummers['drop_down'];

?>

</td>
</tr>
</table>
<hr>
<br>
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
<?php

    #-->    uit de database getrokken data echo-en
    foreach ($page_nummers['db_data'] as $var)
    {

        echo ucfirst($var['message'])."<br>\n";
    }


?>

<br>
<hr>
<div class="bottom">
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
<?php

    echo $page_nummers['prev_next'];

?>

</div>
</center>
<br>
<br>
</body>
</html>



## pagenummers.php ##

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
<?php

#-->    Main functie ( mysql_table, pagina_nummers, prev_next, dropdownbox, database_data, return_vars )
function page_nummers($mysql_table, $sql, $pgn, $p_n, $ddb, $dbd, $r_v)
{

    $msg                  = "Currently disabled";
    $arr['vars']         = make_vars($mysql_table);
    $arr['page_numbers'] = !$pgn ? $msg : build_pagenumbers($arr['vars']['pg'],  $arr['vars']['mpp'], $arr['vars']['tp'], $ddb, $arr['vars']['qs']);
    $arr['prev_next']     = !$p_n ? $msg : build_prev_next  ($arr['vars']['pg'],  $arr['vars']['mpp'], $arr['vars']['tp'], $ddb, $arr['vars']['qs']);
    $arr['drop_down']     = !$ddb ? $msg : build_drop_down  ($arr['vars']['mpp'], $arr['vars']['tm'],  $arr['vars']['qs']);
    $arr['db_data']         = !$dbd ? $msg : fetch_data       ($arr['vars']['ofs'], $arr['vars']['mpp'], $mysql_table, $sql);
    if(!$r_v)             unset($arr['vars']);

    return $arr;
}




#-->    checking and making some vars    
function make_vars($mysql_table)
{

    #-->    kijken of er (geldige) userinput in de url staat
    $vars['qs'] = query_string();
    $vars['pg'] = isset($_GET['pg']) && is_numeric($_GET['pg'])? $_GET['pg'] : 1;
    $vars['mpp'] = isset($_GET['mpp']) && is_numeric($_GET['mpp']) ? $_GET['mpp'] : 25;

    #-->    tellen van de records in de database
    $res = @mysql_query("SELECT count(*) FROM ".$mysql_table) or die(mysql_error());
    $vars['tm'] = @mysql_result($res, 0, 0) or die(mysql_error());

    #-->    aantal pagina nummers uitrekenen en offset voor de limit query bepalen    
    $vars['tp'] = ceil($vars['tm'] / $vars['mpp']);
    $vars['ofs'] = ($vars['pg'] -1) * $vars['mpp'];

    return $vars;
}




#-->    checking custom $_GET and building string of it
function query_string()
{

    $qs = '';
    foreach($_GET as $key => $value) {
        if($key != 'pg' && $key != 'mpp') {
            $qs .= '&amp;'.$key.'='.$value;
        }
    }

    return $qs;
}
    



#-->    functie voor de previous en next knoppen voor de page nummers
function build_prev_next($pg, $mpp, $tp, $ddb, $qs)
{

    $mpp_c = $ddb ? '&amp;mpp='.$mpp : '';
    $str = '';
    if ($tp > 1) {
        $str .= '<small>'."\n";
        $str .= ($pg > 1) ? '<a href="'.$_SERVER['PHP_SELF'].'?pg='.($pg - 1).$mpp_c.$qs.'" title="prev page">&laquo;&laquo;&laquo;</a>'."\n" : '<span style="color:#ccc;" title="prev page">&laquo;&laquo;&laquo;</span>'."\n";
        $str .= '&nbsp;<span style="color:#ccc;">--</span>&nbsp;'."\n";
        $str .= ($pg < $tp) ? '<a href="'.$_SERVER['PHP_SELF'].'?pg='.($pg + 1).$mpp_c.$qs.'" title="next page">&raquo;&raquo;&raquo;</a>'."\n" : '<span style="color:#ccc;" title="next page">&raquo;&raquo;&raquo;</span>'."\n";
        $str .= '</small>'."\n";
    }
  
    return $str;
}




#-->    array met de options voor de dropdownbox
function build_drop_down($mpp, $tm, $qs)
{

    $str = "<form name=\"form\" style=\"margin:0px;\">\nBerichten per Pagina : \n<select name=\"documentkeuze\" onchange=\"openURI()\">\n";
    $arr = array(    $mpp.' '=> $mpp,
                    '---'    => $mpp,
                    '10'     => '10',
                    '25'     => '25',
                    '50'     => '50',
                    '75'     => '75',
                    '100'     => '100',
                    '125'     => '125',
                    '150'     => '150',
                    'all'     => $tm);
                    
    foreach($arr as $key => $value)
    {

        $str .= '<option value="'.$_SERVER['PHP_SELF'].'?pg=1&amp;mpp='.$value.$qs.'" >&nbsp;'.$key.'</option>'."\n";
    }


    return $str . "</select>\n</form>\n";
}




#-->    building page nummers
function build_pagenumbers($pg, $mpp, $tp, $ddb, $qs)
{

    $mpp_c = $ddb ? '&amp;mpp='.$mpp : '';
    if($tp > 1) {
        if($tp > 7) {
            $start = $pg - 3;  
            $end = $pg + 3;  

            while($start < 1)    {  $start++;  $end++;  }    # geen page-nummers lager dan 1
            while($end > $tp)    {  $start--;  $end--;  }    # niet meer page-nummers dan het aantal totalPages dan dat we hebben

            if($pg > 4)
                $str_start = '<a href="'.$_SERVER['PHP_SELF'].'?pg=1'.$mpp_c.$qs.'" title="first page (1)">...</a>&nbsp;'."\n";
            if($pg < $tp - 3)
                $str_end = '<a href="'.$_SERVER['PHP_SELF'].'?pg='.$tp.$mpp_c.$qs.'" title="last page ('.$tp.')">...</a>'."\n";
        }

        else  {
            $start = 1;
            $end = $tp;
        }

        
        $str = "\n";  

        for ($i = $start; $i <= $end; $i++)
        {
  
            if($pg == $i)
                $str .= "<b>".$i."</b>&nbsp;\n";
            else
                $str .= '<a href="'.$_SERVER['PHP_SELF'].'?pg='.$i.$mpp_c.$qs.'" title="page '.$i.'">'.$i.'</a>&nbsp;'."\n";
        }


        return "Pagina:\n<br>".(isset($str_start) ? $str_start : "") . $str . (isset($str_end) ? $str_end : "");  
    }

    else
        return '<span style="color:#ccc">no page numbers yet</span>';
}




#-->    data uit de database trekken (alleen het aantal records dat we nodig hebben)
function fetch_data($ofs, $mpp, $mysql_table, $sql)
{

    $res= @mysql_query($sql." LIMIT ".$ofs.",".$mpp) or die(mysql_error());

    if(mysql_num_rows($res)) {
        while($data = mysql_fetch_assoc($res)) {
            $arr[] = $data;
        }

        return $arr;
    }

    else
        return "Geen records gevonden";
}




#-->    to see what it returns (debugging only)
function view_array($page_nummers)
{

    $page_nummers['page_numbers'] = htmlentities($page_nummers['page_numbers']);
    $page_nummers['drop_down']    = htmlentities($page_nummers['drop_down']);
    $page_nummers['prev_next']    = htmlentities($page_nummers['prev_next']);

    echo '<pre>';
    print_r($page_nummers);
    echo '<pre>';
    exit;
}



?>

 
 

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.