Webcams slecteren en weergeven
<head>
<title>dbase</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
Hallo,
Ik heb een script in elkaar gedraaid waarbij de webcams uit een database over 3 kolommen worden weergegeven.
Nu lukt het mij niet een selectbox te maken waarbij ik een land kan selecteren en dat dan de webcams van dat land uit de databse worden gehaald en conform de beschreven indeling wordt verdeeld over de pagina.
Wie weet een oplossing?
<body>
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
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
<?php
$dbhost = 'localhost';
$dbuser = 'admin';
$dbpass = 'pass';
$dbname = 'webcams';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
// how many columns
$columns = 3;
// how many rows to show per page
$rowsPerPage = 3;
// by default we show first page
$pageNum = 1;
// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}
// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;
$query = "SELECT * FROM sionvalais ORDER BY resort LIMIT $offset, $rowsPerPage";
$result = mysql_query($query) or die('Error, query failed');
$numrows = mysql_num_rows($result);
echo "<TABLE BORDER=\"0\">\n";
//changed this to a for loop so we can use the number of rows
for($i = 0; $i < $numrows; $i++) {
$row = mysql_fetch_array($result);
if($i % $columns == 0) {
//if there is no remainder, we want to start a new row
echo "<TR>\n";
}
echo "<TD><a href=" . $row['image'] . "><img src=" . $row['image'] . " /></a> <br />" . $row['resort'] . "<br />" . $row['country'] . " </TD>\n";
if(($i % $columns) == ($columns - 1) || ($i + 1) == $num_rows) {
//if there is a remainder of 1, end the row
//or if there is nothing left in our result set, end the row
echo "</TR>\n";
}
}
echo "</TABLE>\n";
// how many rows we have in database
$query = "SELECT COUNT(*) AS numrows FROM sionvalais";
$result = mysql_query($query) or die('Error, query failed');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);
$self = $_SERVER['PHP_SELF'];
// creating 'previous' and 'next' link
// plus 'first page' and 'last page' link
// print 'previous' link only if we're not
// on page one
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
$first = " <a href=\"$self?page=1\">[First Page]</a> ";
}
else
{
$prev = ' [Prev] '; // we're on page one, don't enable 'previous' link
$first = ' [First Page] '; // nor 'first page' link
}
// print 'next' link only if we're not
// on the last page
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\"$self?page=$page\">[Next]</a> ";
$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
}
else
{
$next = ' [Next] '; // we're on the last page, don't enable 'next' link
$last = ' [Last Page] '; // nor 'last page' link
}
// print the page navigation link
echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " . $next . $last;
// Number of rows
echo $numrows;
echo "webcams in database";
echo "<br /><br />";
enhanced_list_box(array(
'table' => 'sionvalais',
'id_field' => 'id',
'value_field' => 'country',
'highlight_id' => 4));
function enhanced_list_box($options){
$sql = "select " . $options['id_field'];
$sql .= ", " . $options['value_field'];
$sql .= " from " . $options['table'];
/* append any where criteria to the sql */
if(isset($options['where_statement'])) {
$sql .= " where " . $options['where_statement'] ;
}
/* set the sort order of the list */
$sql .= " order by " . $options['value_field'];
$result = mysql_query($sql)
or die("error in SQL");
echo '<select name="', $options['id_field'], '" size="1">';
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
if($row[0] == $options['highlight_id']) {
echo '<option value="', $row[0], '" SELECTED>',
$row[1], '</option>';
} else {
echo '<option value="', $row[0], '">',
$row[1], '</option>';
}
}
echo '</select>';
}
// Close db connection
mysql_close($conn);
?>
$dbhost = 'localhost';
$dbuser = 'admin';
$dbpass = 'pass';
$dbname = 'webcams';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
// how many columns
$columns = 3;
// how many rows to show per page
$rowsPerPage = 3;
// by default we show first page
$pageNum = 1;
// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}
// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;
$query = "SELECT * FROM sionvalais ORDER BY resort LIMIT $offset, $rowsPerPage";
$result = mysql_query($query) or die('Error, query failed');
$numrows = mysql_num_rows($result);
echo "<TABLE BORDER=\"0\">\n";
//changed this to a for loop so we can use the number of rows
for($i = 0; $i < $numrows; $i++) {
$row = mysql_fetch_array($result);
if($i % $columns == 0) {
//if there is no remainder, we want to start a new row
echo "<TR>\n";
}
echo "<TD><a href=" . $row['image'] . "><img src=" . $row['image'] . " /></a> <br />" . $row['resort'] . "<br />" . $row['country'] . " </TD>\n";
if(($i % $columns) == ($columns - 1) || ($i + 1) == $num_rows) {
//if there is a remainder of 1, end the row
//or if there is nothing left in our result set, end the row
echo "</TR>\n";
}
}
echo "</TABLE>\n";
// how many rows we have in database
$query = "SELECT COUNT(*) AS numrows FROM sionvalais";
$result = mysql_query($query) or die('Error, query failed');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);
$self = $_SERVER['PHP_SELF'];
// creating 'previous' and 'next' link
// plus 'first page' and 'last page' link
// print 'previous' link only if we're not
// on page one
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
$first = " <a href=\"$self?page=1\">[First Page]</a> ";
}
else
{
$prev = ' [Prev] '; // we're on page one, don't enable 'previous' link
$first = ' [First Page] '; // nor 'first page' link
}
// print 'next' link only if we're not
// on the last page
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\"$self?page=$page\">[Next]</a> ";
$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
}
else
{
$next = ' [Next] '; // we're on the last page, don't enable 'next' link
$last = ' [Last Page] '; // nor 'last page' link
}
// print the page navigation link
echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " . $next . $last;
// Number of rows
echo $numrows;
echo "webcams in database";
echo "<br /><br />";
enhanced_list_box(array(
'table' => 'sionvalais',
'id_field' => 'id',
'value_field' => 'country',
'highlight_id' => 4));
function enhanced_list_box($options){
$sql = "select " . $options['id_field'];
$sql .= ", " . $options['value_field'];
$sql .= " from " . $options['table'];
/* append any where criteria to the sql */
if(isset($options['where_statement'])) {
$sql .= " where " . $options['where_statement'] ;
}
/* set the sort order of the list */
$sql .= " order by " . $options['value_field'];
$result = mysql_query($sql)
or die("error in SQL");
echo '<select name="', $options['id_field'], '" size="1">';
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
if($row[0] == $options['highlight_id']) {
echo '<option value="', $row[0], '" SELECTED>',
$row[1], '</option>';
} else {
echo '<option value="', $row[0], '">',
$row[1], '</option>';
}
}
echo '</select>';
}
// Close db connection
mysql_close($conn);
?>
</body>
</html>
Je zult ontdekken dat wanneer je je vraag iets subtieler stelt er meer mensen de tijd voor je probleem zullen nemen, je plaatst tegen de tweehonderd regels code en verwacht dat de mensen van het forum wel een oplossing zullen aandragen.
Meestal werkt het beter als je de significate stukken code plaatst, en per stuk code je probleem weergeeft. Hierdoor laat je zien dat je moeite hebt gedaan voor je site, en kan je met meer aankomen dan de vermelding "Het doet het niet".
Zie ook de slimme vragen tutorial:
http://www.phphulp.nl/php/tutorials/10/143/
<SELECT name="country">
<OPTION value="land_1">Land 1</OPTION>
<OPTION value="land_2">Land 2</OPTION>
<OPTION value="land_3">Land 3</OPTION>
</SELECT
En dan query SELECT * FROM tabel WHERE country='" . $_GET['country'] . "'";
Dat heb je waarschijnlijk al geprobeerd, maar het zou wel moeten werken :)
Gewijzigd op 17/11/2004 20:17:00 door Winston Smith
Ik heb onderstaande syntax ingevoegd, maar het gaat niet goed. Wordt de selectie wel doorgegeven aan de query? Hoe komt de query weer in de layoutindeling terug die ik heb gedefinieerd?
echo '<form method="post" action=""><select name="country" size=1 onChange="if (form.country.selectedIndex != 0) location = form.country.options[form.country.selectedIndex].value; "><OPTION value="austria">austria</OPTION><OPTION value="switzerland">switzerland</OPTION> <OPTION value="france">Land france</OPTION></SELECT></form>';
$query = "SELECT * FROM sionvalais WHERE country='" . $_GET['country'] . "'";
Maak van de method GET in plaats van POST...
http://www.sionvalais.com/sql/view.php
het is de bedoeling dat het resultaat weer in de pagina view.php word gegooid, zodat het script weer van begin af aan wordt doorlopen, maar dan met slechts de selectie "switzerland".
groet,
Mark
" name="country">
<INPUT TYPE="hidden" name="checked" value="1">
<select name="country">
<OPTION value="austria">austria</OPTION>
<OPTION value="switzerland">switzerland</OPTION> <OPTION value="france">Land france</OPTION>
</SELECT>
<INPUT type="submit" name="submit" value="show!">
</form>';
$query = "SELECT * FROM sionvalais WHERE country='" . $_GET['country'] . "'";
Heb hem veranderd zodat je op een button moet klikken. Weet niet zoveel van javascript, maar er is ongetwijfeld een oplossing voor jouw probleem.
Je kan hem laten door lopen door een if else constructie in te voegen:
<form method="GET" action="<INPUT TYPE="hidden" name="checked" value="1">
<select name="country">
<OPTION value="austria">austria</OPTION>
<OPTION value="switzerland">switzerland</OPTION> <OPTION value="france">Land france</OPTION>
</SELECT>
<INPUT type="submit" name="submit" value="show!">
</form>';
$query = "SELECT * FROM sionvalais WHERE country='" . $_GET['country'] . "'";
Heb hem veranderd zodat je op een button moet klikken. Weet niet zoveel van javascript, maar er is ongetwijfeld een oplossing voor jouw probleem.
Je kan hem laten door lopen door een if else constructie in te voegen:
Code (php)
Gewijzigd op 17/11/2004 21:17:00 door Winston Smith
Moet alleen nog de navigatie functie aanpassen want deze begrijpt niet dat er door de selectie minder cams en pagina's zijn.
Bedankt!
groetjes,
Mark
Blij dat ik je heb kunnen helpen :)