dependable dropdown
Ik heb hier een script gemaakt waarbij ik informatie kan filteren tussen afdeling en subafdeling. Hij werkt goed, alleen wil er een dependable dropdown inbouwen maar weet niet hoe precies.
heb meerdere voorbeelden op internet gevonden, maar die konden mij niet helpen.
Hieronder mijn database indeling
afdeling
subafdeling
help_mij_zoeken.php
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
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
<?php
require_once('class/zoeken.php'); //haal class op
startDB(); //functie startDB
//sql query voor het zoeken van trainingen
$sql = "SELECT DISTINCT train.id,train.naam_training AS Training, train.beschrijving AS Beschrijving, type_train.type_plaatje AS Plaatje
FROM training train
JOIN training_afdeling train_afd
ON train_afd.training_id = train.id
JOIN afdeling afd
ON afd.id = train_afd.afdeling_id
JOIN training_subafdeling train_subafd
ON train_subafd.training_id = train.id
JOIN subafdeling subafd
ON subafd.id = train_subafd.subafdeling_id
JOIN training_type train_type
ON train_type.training_id = train.id
JOIN type_training type_train
ON type_train.id = train_type.type_id
";
if (isset($_GET['srch_for'])){
$getters = array();
$queries = array();
foreach($_GET as $key => $value) {
$temp = is_array($value) ? $value : trim($value);
if(!empty($temp)){
list($key) = explode("-",$key);
if (!in_array($key, $getters)){
$getters[$key] = $value;
}
}
}
if (!empty($getters)){
foreach($getters as $key => $value){
${$key} = $value;
switch($key){
case 'srch_for':
array_push($queries,"(train.naam_training LIKE '%$srch_for%' || train.beschrijving LIKE '%$srch_for%')"); //als de gezochte term overeenkomt met de waarde uit de database(alles)
break;
case 'zk_afdeling':
array_push($queries, "train_afd.afdeling_id = $zk_afdeling"); //als de gezochte term overeenkomt met de waarde uit de database(afdeling)
break;
case 'zk_subafdeling':
array_push($queries, "train_subafd.afdeling_id = $zk_subafdeling"); //als de gezochte term overeenkomt met de waarde uit de database(subafdeling)
break;
case 'zk_typ':
array_push($queries, "train_type.type_id = $zk_typ"); //als de gezochte term overeenkomt met de waarde uit de database(type)
break;
}
}
}
if (!empty($queries)) {
$sql .= " WHERE ";
$i=1;
foreach($queries as $query){
if ($i < count($queries)){
$sql .= $query." AND ";
} else {
$sql .= $query;
}
$i++;
}
}
$sql .= " ORDER BY train.naam_training ASC"; //einde query
}
$rs = mysql_query($sql) or die(mysql_error());
$rows = mysql_fetch_assoc($rs);
$tot_rows = mysql_num_rows($rs);
?>
<form id="msform" name="search_form" method="get" action="help_mij_zoeken_resultaat.php">
<div>
<table border="0" cellspacing="0" cellpadding="0" class="tbl_search">
<tr>
<td><input type="hidden" name="srch_for" id="srch_for" class="f_fld" value="<?php getSticky(1,'srch_for'); ?>"></td>
<th><label for="zk_afdeling">Afdeling:</table>
<?php getAfdeling(); ?><br>
</div>
<div>
<label for="zk_subafdeling">Subafdeling:</th>
<?php getSubafdeling(); ?>
</div>
<input type="submit" id="btn" class="next action-button2" value="Zoeken" />
</fieldset>
<!-- einde pagina selecteer type training -->
</form>
<?php mysql_free_result($rs); ?>
require_once('class/zoeken.php'); //haal class op
startDB(); //functie startDB
//sql query voor het zoeken van trainingen
$sql = "SELECT DISTINCT train.id,train.naam_training AS Training, train.beschrijving AS Beschrijving, type_train.type_plaatje AS Plaatje
FROM training train
JOIN training_afdeling train_afd
ON train_afd.training_id = train.id
JOIN afdeling afd
ON afd.id = train_afd.afdeling_id
JOIN training_subafdeling train_subafd
ON train_subafd.training_id = train.id
JOIN subafdeling subafd
ON subafd.id = train_subafd.subafdeling_id
JOIN training_type train_type
ON train_type.training_id = train.id
JOIN type_training type_train
ON type_train.id = train_type.type_id
";
if (isset($_GET['srch_for'])){
$getters = array();
$queries = array();
foreach($_GET as $key => $value) {
$temp = is_array($value) ? $value : trim($value);
if(!empty($temp)){
list($key) = explode("-",$key);
if (!in_array($key, $getters)){
$getters[$key] = $value;
}
}
}
if (!empty($getters)){
foreach($getters as $key => $value){
${$key} = $value;
switch($key){
case 'srch_for':
array_push($queries,"(train.naam_training LIKE '%$srch_for%' || train.beschrijving LIKE '%$srch_for%')"); //als de gezochte term overeenkomt met de waarde uit de database(alles)
break;
case 'zk_afdeling':
array_push($queries, "train_afd.afdeling_id = $zk_afdeling"); //als de gezochte term overeenkomt met de waarde uit de database(afdeling)
break;
case 'zk_subafdeling':
array_push($queries, "train_subafd.afdeling_id = $zk_subafdeling"); //als de gezochte term overeenkomt met de waarde uit de database(subafdeling)
break;
case 'zk_typ':
array_push($queries, "train_type.type_id = $zk_typ"); //als de gezochte term overeenkomt met de waarde uit de database(type)
break;
}
}
}
if (!empty($queries)) {
$sql .= " WHERE ";
$i=1;
foreach($queries as $query){
if ($i < count($queries)){
$sql .= $query." AND ";
} else {
$sql .= $query;
}
$i++;
}
}
$sql .= " ORDER BY train.naam_training ASC"; //einde query
}
$rs = mysql_query($sql) or die(mysql_error());
$rows = mysql_fetch_assoc($rs);
$tot_rows = mysql_num_rows($rs);
?>
<form id="msform" name="search_form" method="get" action="help_mij_zoeken_resultaat.php">
<div>
<table border="0" cellspacing="0" cellpadding="0" class="tbl_search">
<tr>
<td><input type="hidden" name="srch_for" id="srch_for" class="f_fld" value="<?php getSticky(1,'srch_for'); ?>"></td>
<th><label for="zk_afdeling">Afdeling:</table>
<?php getAfdeling(); ?><br>
</div>
<div>
<label for="zk_subafdeling">Subafdeling:</th>
<?php getSubafdeling(); ?>
</div>
<input type="submit" id="btn" class="next action-button2" value="Zoeken" />
</fieldset>
<!-- einde pagina selecteer type training -->
</form>
<?php mysql_free_result($rs); ?>
zoeken.php
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
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
<?php
require_once 'class/Singleton.php';
function startDB(){
$connect = Singleton::getconnect();
}
function getAfdeling() {
global $database;
global $conndb;
$sql = "SELECT * FROM afdeling ORDER BY naam_afdeling ASC";
mysql_select_db($database);
$rs = mysql_query($sql) or die(mysql_error());
$rows = mysql_fetch_assoc($rs);
$total_rows = mysql_num_rows($rs);
if($total_rows > 0){
echo "<select name=\"zk_afdeling\" id=\"zk_afdeling\">\n";
echo "<option value=\"\">Elke afdeling…</option>\n";
do {
echo "<option value=\"".$rows['id']."\"";
getSticky(2, 'zk_afdeling',$rows['id']);
echo ">".$rows['naam_afdeling']."</optio>";
} while($rows = mysql_fetch_assoc($rs));
echo "</select>";
}
mysql_free_result($rs);
}
function getSubafdeling() {
global $database;
global $conndb;
$sql = "SELECT * FROM subafdeling ORDER BY naam_subafdeling ASC";
mysql_select_db($database);
$rs = mysql_query($sql) or die(mysql_error());
$rows = mysql_fetch_assoc($rs);
$total_rows = mysql_num_rows($rs);
if($total_rows > 0){
echo "<select name=\"zk_subafdeling\" id=\"zk_subafdeling\">\n";
echo "<option value=\"\">Elke subafdeling…</option>\n";
do {
echo "<option value=\"".$rows['id']."\"";
getSticky(2, 'zk_subafdeling',$rows['id']);
echo ">".$rows['naam_subafdeling']."</optio>";
} while($rows = mysql_fetch_assoc($rs));
echo "</select>";
}
mysql_free_result($rs);
}
function getSticky($case,$par,$value="",$initial=""){
switch($case) {
case 1: //text field
if (isset($_GET[$par]) && $_GET[$par] !=""){
echo stripslashes($_GET[$par]);
}
break;
case 2: //select dropdown menus
if (isset($_GET[$par]) && $_GET[$par] == $value){
echo " selected=\"selected\"";
}
break;
case 3: //checkbox group
if (isset($_GET[$par]) && $_GET[$par] !=""){
echo " checked=\"checked\"";
}
case 4: //radio buttons
if (isset($_GET[$par]) && $_GET[$par] == $value){
echo " checked=\"checked\"";
} else {
if ($initial !=""){
echo " checked=\"checked\"";
}
}
break;
}
}
?>
require_once 'class/Singleton.php';
function startDB(){
$connect = Singleton::getconnect();
}
function getAfdeling() {
global $database;
global $conndb;
$sql = "SELECT * FROM afdeling ORDER BY naam_afdeling ASC";
mysql_select_db($database);
$rs = mysql_query($sql) or die(mysql_error());
$rows = mysql_fetch_assoc($rs);
$total_rows = mysql_num_rows($rs);
if($total_rows > 0){
echo "<select name=\"zk_afdeling\" id=\"zk_afdeling\">\n";
echo "<option value=\"\">Elke afdeling…</option>\n";
do {
echo "<option value=\"".$rows['id']."\"";
getSticky(2, 'zk_afdeling',$rows['id']);
echo ">".$rows['naam_afdeling']."</optio>";
} while($rows = mysql_fetch_assoc($rs));
echo "</select>";
}
mysql_free_result($rs);
}
function getSubafdeling() {
global $database;
global $conndb;
$sql = "SELECT * FROM subafdeling ORDER BY naam_subafdeling ASC";
mysql_select_db($database);
$rs = mysql_query($sql) or die(mysql_error());
$rows = mysql_fetch_assoc($rs);
$total_rows = mysql_num_rows($rs);
if($total_rows > 0){
echo "<select name=\"zk_subafdeling\" id=\"zk_subafdeling\">\n";
echo "<option value=\"\">Elke subafdeling…</option>\n";
do {
echo "<option value=\"".$rows['id']."\"";
getSticky(2, 'zk_subafdeling',$rows['id']);
echo ">".$rows['naam_subafdeling']."</optio>";
} while($rows = mysql_fetch_assoc($rs));
echo "</select>";
}
mysql_free_result($rs);
}
function getSticky($case,$par,$value="",$initial=""){
switch($case) {
case 1: //text field
if (isset($_GET[$par]) && $_GET[$par] !=""){
echo stripslashes($_GET[$par]);
}
break;
case 2: //select dropdown menus
if (isset($_GET[$par]) && $_GET[$par] == $value){
echo " selected=\"selected\"";
}
break;
case 3: //checkbox group
if (isset($_GET[$par]) && $_GET[$par] !=""){
echo " checked=\"checked\"";
}
case 4: //radio buttons
if (isset($_GET[$par]) && $_GET[$par] == $value){
echo " checked=\"checked\"";
} else {
if ($initial !=""){
echo " checked=\"checked\"";
}
}
break;
}
}
?>
Het toverwoord in dit geval is AJAX, zoek er maar eens op.
Code (php)
1
http://www.sourcecodester.com/download-code?nid=5568&title=Creating+a+Dependent+Dropdown+List+with+PHP%2C+jQuery+and+Ajax.&uri=%2Ftutorials%2Fphp%2F5568%2Fcreating-dependent-dropdown-list-php-jquery-and-ajax.html
Pipo Clown op 25/06/2014 11:40:56:
Het toverwoord in dit geval is AJAX, zoek er maar eens op.