Aanpassen search script
Hans Hermes op 04/07/2019 13:14:07:
Ja het zat idd nu bij mij fout cache geleegd en etc nu werkt alles zoals het moet zijn.
Heel erg bedankt ik ben hier superblij mee.
Met vriendelijk groet,
Hans Hermes
Heel erg bedankt ik ben hier superblij mee.
Met vriendelijk groet,
Hans Hermes
Goed om te horen dat het werkt.
Enjoy!
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
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
190
191
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
190
191
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="user-scalable=1, width=device-width, initial-scale=1.0, maximum-scale=2.0, minimum-scale=1.0" />
<title>Mijn prachtige db muziekcollectie</title>
<script>
if ( window.history.replaceState ) {
window.history.replaceState( null, null, window.location.href );
}
</script>
<style>
a {
text-decoration: none;
font-size: 120%;
}
</style>
</head>
<body>
<div style="max-width: 750px; padding: 5px; margin: 10px auto; border: 0.100em solid red;">
<?php
$initShow = FALSE;
if( isset( $_POST["postKeyword"] ) || isset( $_GET["getKeyword"] ) )
{
$serverName = "localhost";
$userName = "user";
$userPassword = "password";
$dbName = "database";
$db = new mysqli( $serverName, $userName, $userPassword, $dbName );
$strKeyword = '';
$per_page = 10; // Per Page
$page = 1;
if( isset( $_POST["postKeyword"] ) )
{
//echo '<pre>' . print_r( $_POST, TRUE ) . '</pre>';
$strKeyword = $db->real_escape_string( $_POST["postKeyword"] );
$initShow = TRUE;
}
elseif( isset( $_GET["getKeyword"] ) )
{
//echo '<pre>' . print_r( $_GET, TRUE ) . '</pre>';
if( isset( $_GET["Page"] ) )
{
$page = (int)$_GET["Page"];
}
$strKeyword = $db->real_escape_string( $_GET["getKeyword"] );
$initShow = TRUE;
}
$sql = "SELECT count(*) as aantal FROM muziek WHERE artiest LIKE '%" . $strKeyword . "%' OR songnummer LIKE '%" . $strKeyword . "%'";
$result = $db->query( $sql );
$row = $result->fetch_object();
//echo '<p>Aantal = ' . $row->aantal . '</p>';
$num_rows = $row->aantal;
$prev_page = $page - 1;
$next_page = $page + 1;
$row_start = ( ( $per_page * $page ) - $per_page );
if( $num_rows <= $per_page )
{
$num_pages =1;
}
else if( ( $num_rows % $per_page ) == 0 )
{
$num_pages = ( $num_rows / $per_page );
}
else
{
$num_pages =( $num_rows / $per_page ) + 1;
$num_pages = (int)$num_pages;
}
$sql = "SELECT artiest, songnummer, jaar FROM muziek WHERE artiest LIKE '%" . $strKeyword . "%' OR songnummer LIKE '%" . $strKeyword . "%' ORDER BY artiest ASC, songnummer ASC LIMIT " . $row_start . "," . $per_page;
//echo '<p>' . $sql . '</p>';
$result = $db->query( $sql );
//echo '<pre>' . print_r( $result, TRUE ) . '</pre>';
}
?>
<form name="frmSearch" method="post">
<table border="0" style="width: 100%;">
<tr>
<th>Keyword
<input name="postKeyword" type="text" id="postKeyword" value="<?php echo $strKeyword;?>" autofocus />
<input type="submit" name="submit" value="Search" />
</th>
</tr>
</table>
</form>
<?php
if ( $initShow == TRUE )
{
?>
<p>Gevonden :
<?php
echo $num_rows;?> Record<?php echo ( $num_rows <> 1 ? 's' : '' );
?>
|
<?php
echo ( $num_rows >= 1 ? $num_pages : 'Geen ' );
?>
Pagina<?php echo ( $num_pages == 1 ? '' : 's' ); ?>
:
<?php
if( $prev_page )
{
echo " <a href='?getKeyword=$strKeyword&Page=$prev_page'>⇦</a> ";
}
for( $i = 1; $i <= $num_pages; $i++ )
{
if( $i != $page )
{
echo " <a href='?getKeyword=$strKeyword&Page=$i'>$i</a> ";
}
else
{
echo $num_rows <> 0 ? "<b> $i </b>" : "";
}
}
if( $page != $num_pages )
{
echo " <a href ='?getKeyword=$strKeyword&Page=$next_page'>⇨</a>";
}
?>
</p>
<table border="0" style="width: 100%;">
<tr>
<th style="width: 40%; text-align: left;">Artiest</th>
<th style="width: 40%; text-align: left;">Titel</th>
<th style="width: 20%; text-align: right;">Jaar</th>
</tr>
<?php
while ( $row = $result->fetch_object() )
{
?>
<tr>
<td><?php echo $row->artiest;?></td>
<td><?php echo $row->songnummer;?></td>
<td align="right"><?php echo $row->jaar;?></td>
</tr>
<?php
}
?>
</table>
<?php
$db->close();
?>
<?php
}
?>
</div>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="user-scalable=1, width=device-width, initial-scale=1.0, maximum-scale=2.0, minimum-scale=1.0" />
<title>Mijn prachtige db muziekcollectie</title>
<script>
if ( window.history.replaceState ) {
window.history.replaceState( null, null, window.location.href );
}
</script>
<style>
a {
text-decoration: none;
font-size: 120%;
}
</style>
</head>
<body>
<div style="max-width: 750px; padding: 5px; margin: 10px auto; border: 0.100em solid red;">
<?php
$initShow = FALSE;
if( isset( $_POST["postKeyword"] ) || isset( $_GET["getKeyword"] ) )
{
$serverName = "localhost";
$userName = "user";
$userPassword = "password";
$dbName = "database";
$db = new mysqli( $serverName, $userName, $userPassword, $dbName );
$strKeyword = '';
$per_page = 10; // Per Page
$page = 1;
if( isset( $_POST["postKeyword"] ) )
{
//echo '<pre>' . print_r( $_POST, TRUE ) . '</pre>';
$strKeyword = $db->real_escape_string( $_POST["postKeyword"] );
$initShow = TRUE;
}
elseif( isset( $_GET["getKeyword"] ) )
{
//echo '<pre>' . print_r( $_GET, TRUE ) . '</pre>';
if( isset( $_GET["Page"] ) )
{
$page = (int)$_GET["Page"];
}
$strKeyword = $db->real_escape_string( $_GET["getKeyword"] );
$initShow = TRUE;
}
$sql = "SELECT count(*) as aantal FROM muziek WHERE artiest LIKE '%" . $strKeyword . "%' OR songnummer LIKE '%" . $strKeyword . "%'";
$result = $db->query( $sql );
$row = $result->fetch_object();
//echo '<p>Aantal = ' . $row->aantal . '</p>';
$num_rows = $row->aantal;
$prev_page = $page - 1;
$next_page = $page + 1;
$row_start = ( ( $per_page * $page ) - $per_page );
if( $num_rows <= $per_page )
{
$num_pages =1;
}
else if( ( $num_rows % $per_page ) == 0 )
{
$num_pages = ( $num_rows / $per_page );
}
else
{
$num_pages =( $num_rows / $per_page ) + 1;
$num_pages = (int)$num_pages;
}
$sql = "SELECT artiest, songnummer, jaar FROM muziek WHERE artiest LIKE '%" . $strKeyword . "%' OR songnummer LIKE '%" . $strKeyword . "%' ORDER BY artiest ASC, songnummer ASC LIMIT " . $row_start . "," . $per_page;
//echo '<p>' . $sql . '</p>';
$result = $db->query( $sql );
//echo '<pre>' . print_r( $result, TRUE ) . '</pre>';
}
?>
<form name="frmSearch" method="post">
<table border="0" style="width: 100%;">
<tr>
<th>Keyword
<input name="postKeyword" type="text" id="postKeyword" value="<?php echo $strKeyword;?>" autofocus />
<input type="submit" name="submit" value="Search" />
</th>
</tr>
</table>
</form>
<?php
if ( $initShow == TRUE )
{
?>
<p>Gevonden :
<?php
echo $num_rows;?> Record<?php echo ( $num_rows <> 1 ? 's' : '' );
?>
|
<?php
echo ( $num_rows >= 1 ? $num_pages : 'Geen ' );
?>
Pagina<?php echo ( $num_pages == 1 ? '' : 's' ); ?>
:
<?php
if( $prev_page )
{
echo " <a href='?getKeyword=$strKeyword&Page=$prev_page'>⇦</a> ";
}
for( $i = 1; $i <= $num_pages; $i++ )
{
if( $i != $page )
{
echo " <a href='?getKeyword=$strKeyword&Page=$i'>$i</a> ";
}
else
{
echo $num_rows <> 0 ? "<b> $i </b>" : "";
}
}
if( $page != $num_pages )
{
echo " <a href ='?getKeyword=$strKeyword&Page=$next_page'>⇨</a>";
}
?>
</p>
<table border="0" style="width: 100%;">
<tr>
<th style="width: 40%; text-align: left;">Artiest</th>
<th style="width: 40%; text-align: left;">Titel</th>
<th style="width: 20%; text-align: right;">Jaar</th>
</tr>
<?php
while ( $row = $result->fetch_object() )
{
?>
<tr>
<td><?php echo $row->artiest;?></td>
<td><?php echo $row->songnummer;?></td>
<td align="right"><?php echo $row->jaar;?></td>
</tr>
<?php
}
?>
</table>
<?php
$db->close();
?>
<?php
}
?>
</div>
</body>
</html>
Ja ik ga deze toepassen op mijn website.
Super bedankt.
Groetjes,
Hans Hermes
Ook zouden URL's "HTML safe" gemaakt moeten worden. Een & (ampersand) in een hyperlink-tag zou omgezet moeten worden naar &. Tevens zou het keyword, wanneer je deze in betreffende hyperlink gebruik, geëncodeerd moeten worden. Dit kun je met urlencode() of http_build_query() doen.
En nu we het toch over encodering hebben. Hoe luidt de character encoding van de databasetabellen en -data? Want je doet in je HTML-document wel alsof alles lekker UTF-8 is, maar je stelt geen character encoding in met de daarvoor bestemde functie/methode nadat je een verbinding hebt gemaakt.
Het ontbreken van deze informatie opent op zichzelf al een scala aan potentiële problemen. Als de data in de database op de verkeerde manier is geëncodeerd, of je de data gaat zoeken met een verkeerde/andere encodering, hoe kun je dan met goed fatsoen verwachten dat dit gaat werken?