Pagina Nummers Plug & play trouble
En het bijna helemaal gelukt alleen heb ik nu nog 1 probleem. Wanneer ik het script gebruik in combinatie met een zoek woord (LIKE) in de query krijg ik steeds een probleem. De zoek variabele wordt niet onthouden. Telkens wanneer ik op een volgende pagina kom is het weg en dus de melding:
Notice: Undefined variable: merk in /home/ajtrading/domains/pietparket.nl/public_html/pages/kies/product_kies_verwerk2.php on line 14
Dit is de lijn:
$mysql['search'] = "$merk";
Merk komt voort uit de pagina hiervoor en wordt megegeven vanuit een formulier.
Het script:
Code (php)
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
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'] = "ajtrading_producten"; #--> database die je wil selecteren
$mysql['table'] = "producten"; #--> table die je wil gaan gebruiken
$mysql['search'] = "$merk"; #--> table die je wil gaan gebruiken
#--> je query ( this is just an example - !!! GEEN LIMIT GEBRUIKEN HIER !!! )
$sql = "SELECT * FROM `".$mysql['table']."` WHERE merk LIKE '".$mysql['search']."' ORDER BY id";
#--> loading file
require('pagenummers_2.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'], $mysql['search'], $sql, 1, 1, 1, 1, 0);
#--> to see what it returns (debugging only)
//view_array($page_nummers);
?>
#--> 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'] = "ajtrading_producten"; #--> database die je wil selecteren
$mysql['table'] = "producten"; #--> table die je wil gaan gebruiken
$mysql['search'] = "$merk"; #--> table die je wil gaan gebruiken
#--> je query ( this is just an example - !!! GEEN LIMIT GEBRUIKEN HIER !!! )
$sql = "SELECT * FROM `".$mysql['table']."` WHERE merk LIKE '".$mysql['search']."' ORDER BY id";
#--> loading file
require('pagenummers_2.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'], $mysql['search'], $sql, 1, 1, 1, 1, 0);
#--> to see what it returns (debugging only)
//view_array($page_nummers);
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Product show</title>
<link rel="stylesheet" type="text/css" href="../../scripts/show_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>
<p></p>
<div align="left">
<img src="../../images/header_zoekresultaten.gif" width="250" height="29">
</div>
<p>Hier onder treft U de producten aan die aan uw zoek opdracht voldoen. Om meer informatie over een product te krijgen zoals afmeting, verpakkings eenheid, interieur foto's of om een offerte op te stellen klik dan op <em>Details bekijken</em>. Wanneer U direct een bestelling wil plaatsen klik dan op <em>Bestel direct</em>. </p>
<table width="500px" align="center">
<tr>
<td class="top" align="left">
</td>
<td class="top" align="right">
</td>
</tr>
<tr>
<td class="top" align="center" colspan="2">
</td>
</tr>
</table>
<hr>
<br>
<table border="0" align="center">
Code (php)
1
2
3
4
5
2
3
4
5
<?php
#--> uit de database getrokken data echo-en
foreach ($page_nummers['db_data'] as $var)
{
?>
#--> uit de database getrokken data echo-en
foreach ($page_nummers['db_data'] as $var)
{
?>
<tr>
<td><b></b></td>
<td rowspan="8" height="166"><img src="../../images/parket/.jpg"></td>
<td></td>
</tr>
<tr>
<td> </td></tr>
<tr>
<tr>
<td></td>
<td> </td>
</tr>
<tr>
<td> euro per m2</td>
<td><a href="details.php?id=">Details bekijken</a></td>
</tr>
<tr>
<td> </td>
<td><a href="#" onclick="window.open('../../images/sfeer/.jpg', 'Sfeer_impressie', 'width=400,height=500,scrollbars=no,toolbar=no,location=no,status=no,resizable=no'); return false">Sfeer impressie</a></td>
</tr>
<tr>
<td> </td>
<td><a href="offerte.php?id=">Offerte opstellen</a></td>
</tr>
<tr>
<td valign=top> </td>
<td valign=top><a href="../bestel.php?id=">Bestel direct</a></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
<p></p>
<br>
<table border="0" align="center">
<tr>
<td class="top"></td>
</tr>
</table>
<br>
<br>
</body>
</html>
de page nummers
Code (php)
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
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
<?php
error_reporting(E_ALL);
#--> Main functie ( mysql_table, pagina_nummers, prev_next, dropdownbox, database_data, return_vars )
function page_nummers($mysql_table, $mysql_search, $sql, $pgn, $p_n, $ddb, $dbd, $r_v)
{
$msg = "Currently disabled";
$arr['vars'] = make_vars($mysql_table, $mysql_search);
$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, $mysql_search, $sql);
if(!$r_v) unset($arr['vars']);
return $arr;
}
#--> checking and making some vars
function make_vars($mysql_table, $mysql_search)
{
#--> 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'] : 5;
#--> tellen van de records in de database
$res = @mysql_query("SELECT count(*) FROM $mysql_table WHERE merk LIKE '$mysql_search' ")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 .= '&'.$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 ? '&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">«««</a>'."\n" : '<span style="color:#ccc;" title="prev page">«««</span>'."\n";
$str .= ' <span style="color:#ccc;">--</span> '."\n";
$str .= ($pg < $tp) ? '<a href="'.$_SERVER['PHP_SELF'].'?pg='.($pg + 1).$mpp_c.$qs.'" title="next page">»»»</a>'."\n" : '<span style="color:#ccc;" title="next page">»»»</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',
'all' => $tm);
foreach($arr as $key => $value)
{
$str .= '<option value="'.$_SERVER['PHP_SELF'].'?pg=1&mpp='.$value.$qs.'" > '.$key.'</option>'."\n";
}
return $str . "</select>\n</form>\n";
}
#--> building page nummers
function build_pagenumbers($pg, $mpp, $tp, $ddb, $qs)
{
$mpp_c = $ddb ? '&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> '."\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> \n";
else
$str .= '<a href="'.$_SERVER['PHP_SELF'].'?pg='.$i.$mpp_c.$qs.'" title="page '.$i.'">'.$i.'</a> '."\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, $mysql_search, $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;
}
?>
error_reporting(E_ALL);
#--> Main functie ( mysql_table, pagina_nummers, prev_next, dropdownbox, database_data, return_vars )
function page_nummers($mysql_table, $mysql_search, $sql, $pgn, $p_n, $ddb, $dbd, $r_v)
{
$msg = "Currently disabled";
$arr['vars'] = make_vars($mysql_table, $mysql_search);
$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, $mysql_search, $sql);
if(!$r_v) unset($arr['vars']);
return $arr;
}
#--> checking and making some vars
function make_vars($mysql_table, $mysql_search)
{
#--> 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'] : 5;
#--> tellen van de records in de database
$res = @mysql_query("SELECT count(*) FROM $mysql_table WHERE merk LIKE '$mysql_search' ")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 .= '&'.$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 ? '&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">«««</a>'."\n" : '<span style="color:#ccc;" title="prev page">«««</span>'."\n";
$str .= ' <span style="color:#ccc;">--</span> '."\n";
$str .= ($pg < $tp) ? '<a href="'.$_SERVER['PHP_SELF'].'?pg='.($pg + 1).$mpp_c.$qs.'" title="next page">»»»</a>'."\n" : '<span style="color:#ccc;" title="next page">»»»</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',
'all' => $tm);
foreach($arr as $key => $value)
{
$str .= '<option value="'.$_SERVER['PHP_SELF'].'?pg=1&mpp='.$value.$qs.'" > '.$key.'</option>'."\n";
}
return $str . "</select>\n</form>\n";
}
#--> building page nummers
function build_pagenumbers($pg, $mpp, $tp, $ddb, $qs)
{
$mpp_c = $ddb ? '&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> '."\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> \n";
else
$str .= '<a href="'.$_SERVER['PHP_SELF'].'?pg='.$i.$mpp_c.$qs.'" title="page '.$i.'">'.$i.'</a> '."\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, $mysql_search, $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;
}
?>
Controleren met empty()
sorry hoe bedoel je??
t is al opgelost in enkele functies stond ?pg dit moest &pg zijn!!