filter id meegeven in link

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

George mendel

george mendel

24/06/2014 14:11:19
Quote Anchor link
Ik heb hier een script voor het filteren van een resultaat dat bestaat uit checkboxes, dropdowns, radiobuttons.
wat ik nu heb is dat bij het klikken op de submit button de zoekresultaten worden meegegeven in de link.
wat ik wil dat ik bij het keuze maken van bijv. dropdown het gelijk in de link wordt meegeven voordat ik op de submit button klik.
Hoe kan ik dat oplossen?

dit is mijn script:
index.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
require_once('inc/functions.php');

$sql = "SELECT DISTINCT bk.title AS Title, YEAR(bk.date_released) AS Year, bk.price AS Price, cat.name AS Category, aut.name AS Author
FROM books bk
JOIN categories cat
    ON cat.id = bk.category
    
JOIN books_covers bk_co
    ON bk_co.book_id = bk.id

JOIN covers co
    ON co.id = bk_co.cover_id

JOIN books_authors bk_aut
    ON bk_aut.book_id = bk.id

JOIN authors aut
    ON aut.id = bk_aut.author_id

JOIN books_languages bk_lan
    ON bk_lan.book_id = bk.id

JOIN languages lan
    ON lan.id = bk_lan.lang_id

JOIN books_locations bk_loc
    ON bk_loc.book_id = bk.id

JOIN locations loc
    ON loc.id = bk_loc.location_id
    

"
;
if (isset($_GET['srch_for'])){

    $locations = array();
    $getters = array();
    $queries = array();
    
    foreach($_GET as $key => $value) {
        $temp = is_array($value) ? $value : trim($value);
        if(!empty($temp)){
            list($key) = explode("-",$key);
            if($key == 'srch_locations'){
                array_push($locations, $value);
            }

            if (!in_array($key, $getters)){
                $getters[$key] = $value;
            }
        }
    }

    if (!empty($locations)){
        $loc_qry = implode(",",$locations);
    }

    
    if (!empty($getters)){
    
        foreach($getters as $key => $value){
            ${$key} = $value;
        switch($key){
            case
'srch_for':
            array_push($queries,"(bk.title LIKE '%$srch_for%' || bk.description LIKE '%$srch_for%' || bk.isbn LIKE '%$srch_for%')");    
            break;
            case
'srch_category':
            array_push($queries,"bk.categories = $srch_category");
            break;
            case
'srch_cover':
            array_push($queries, "bk_co.cover_id = $srch_cover");
            break;
            case
'srch_author':
            array_push($queries, "bk_aut.author_id = $srch_author");
            break;
            case
'srch_language':
            array_push($queries, "bk_lan.lang_id = $srch_language");
            break;
            case
'srch_year':
            array_push($queries, "YEAR(bk.date_released) = $srch_year");
            break;
            case
'srch_locations':
            array_push($queries, "bk_loc.location_id IN ($loc_qry)");
            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 bk.title ASC";
}

mysql_select_db($database,$conndb);
$rs = mysql_query($sql,$conndb) or die(mysql_error());
$rows = mysql_fetch_assoc($rs);
$tot_rows = mysql_num_rows($rs);
?>

<html xmlns="http://www.w2,org//1999/xhtml">
<head>
<meta http-equiv="Content-Type" Content="text/html; charset=UTF-8" />
<title>Advanced search form</title>
<link href="style/core.css" rel="stylesheet" type="text/css" />
</head>
<div id="out">
    <div id="ph">
        <div id="phin">
            <h1>Advanced Search Form</h1>
            <h3>Web design tutorials by Sebastian Sulinski</h3>
        </div>
    </div>
    <div id="wr">
        <div id="hd"></div>
        <div id="cnt">
            <h2>Search for the book</h2>
            <form id="search_form" name="search_form" method="get" action="">
                <table border="0" cellspacing="0" cellpadding="0" class="tbl_search">
                <tr>
                    <th scope="row"><label for="srch_for">Search for:</label></th>
                    <td><input type="text" name="srch_for" id="srch_for" class="f_fld" value="<?php getSticky(1,'srch_for'); ?>"></td>
                    <th><label for="srch_category">Category:</table></th>
                    <td><?php getCategories(); ?></td>
                </tr>
                <tr>
                    <th scope="row"><label for="srch_cover">Cover type:</label></th>
                    <td><?php getCover(); ?></td><br>
                    <th><label for="srch_author">Author:</th>
                    <td><?php getAuthors(); ?></td>
                </tr>
                  <tr>
                    <th scope="row"><label for="srch_language">Language:</label></th>
                    <td><?php getLanguage(); ?></td>
                    <th><label for="src_year">Year:</label></th>
                    <td><?php getYears(); ?></td>
                </tr>
                <tr>
                    <th scope="row">Available in:</th>
                    <td colspan="3"><?php getLocation(); ?></td>
                </tr>
                <tr>
                    <td colspan="4"><label for="btn" class="sbm fl_r">
                        <input type="submit" id="btn" value="Search" />
                    </label>
                </tr>
            </form>
            <?php if ($tot_rows > 0) { ?>
            <table border="0" cellspacing="0" cellpadding="0" class="tbl_repeat">
                <tr>
                <th scope="col">Book title</th>
                <th scope="col" class="col_15">Category</th>
                <th scope="col" class="col_15">Author</th>
                <th scope="col" class="col_10 al_r">Year</th>
                <th scope="col" class="col_10 al_r">Price</th>
                </tr>
                <?php do { ?>
                <tr>
                <th scope="col"><?php echo $rows['Title']; ?></th>
                <th scope="col"><?php echo $rows['Category']; ?></th>
                <th scope="col"><?php echo $rows['Author']; ?></th>
                <th scope="col"><?php echo $rows['Year']; ?></th>
                <th scope="col"><?php echo $rows['Price']; ?></th>
                </tr>
                <?php } while($rows = mysql_fetch_assoc($rs)); ?>
            </table>
            <?php
            } else {
                if (!empty($queries)) {
                    echo "<p>There are no records matching your searching criteria.</p>";
                }
else {
                    echo "<p>There are currently no records available.</p>";
                }
            }


            ?>

        </div>
    </div>    
<body>
</body>
</html>
<?php mysql_free_result($rs); ?>


functions.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
168
<?php

require_once('database.php');

function
getLanguage() {
    global $database;
    global $conndb;
    
    $sql = "SELECT * FROM languages ORDER BY name ASC";
    mysql_select_db($database, $conndb);
    $rs = mysql_query($sql, $conndb) or die(mysql_error());
    $rows = mysql_fetch_assoc($rs);
    $total_rows = mysql_num_rows($rs);
    if($total_rows > 0){
        echo "<select name=\"srch_language\" id=\"srch_language\">\n";
        echo "<option value=\"\">Any language&hellip;</option>\n";
        do {
            echo "<option value=\"".$rows['id']."\"";
            getSticky(2, 'srch_language',$rows['id']);
            echo ">".$rows['name']."</optio>";
        }
while($rows = mysql_fetch_assoc($rs));
        echo "</select>";
    }

    mysql_free_result($rs);
}

function
getAuthors() {
    global $database;
    global $conndb;
    
    $sql = "SELECT * FROM authors ORDER BY name ASC";
    mysql_select_db($database, $conndb);
    $rs = mysql_query($sql, $conndb) or die(mysql_error());
    $rows = mysql_fetch_assoc($rs);
    $total_rows = mysql_num_rows($rs);
    if($total_rows > 0){
        echo "<select name=\"srch_author\" id=\"srch_author\">\n";
        echo "<option value=\"\">Any author&hellip;</option>\n";
        do {
            echo "<option value=\"".$rows['id']."\"";
            getSticky(2, 'srch_author',$rows['id']);
            echo ">".$rows['name']."</optio>";
        }
while($rows = mysql_fetch_assoc($rs));
        echo "</select>";
    }

    mysql_free_result($rs);
}

function
getCategories() {
    global $database;
    global $conndb;
    
    $sql = "SELECT * FROM categories ORDER BY name ASC";
    mysql_select_db($database, $conndb);
    $rs = mysql_query($sql, $conndb) or die(mysql_error());
    $rows = mysql_fetch_assoc($rs);
    $total_rows = mysql_num_rows($rs);
    if($total_rows > 0){
        echo "<select name=\"srch_category\" id=\"srch_category\">\n";
        echo "<option value=\"\">Any category&hellip;</option>\n";
        do {
            echo "<option value=\"".$rows['id']."\"";
            getSticky(2,'srch_category',$rows['id']);
            echo ">".$rows['name']."</optio>";
        }
while($rows = mysql_fetch_assoc($rs));
        echo "</select>";
    }

    mysql_free_result($rs);
}

function
getYears() {
    global $database;
    global $conndb;
    
    $sql = "SELECT DISTINCT YEAR(date_released) as year FROM books ORDER BY date_released ASC";
    mysql_select_db($database, $conndb);
    $rs = mysql_query($sql, $conndb) or die(mysql_error());
    $rows = mysql_fetch_assoc($rs);
    $total_rows = mysql_num_rows($rs);
    if($total_rows > 0){
        echo "<select name=\"srch_year\" id=\"srch_year\">\n";
        echo "<option value=\"\">Any year&hellip;</option>\n";
        do {
            echo "<option value=\"".$rows['year']."\"";
            getSticky(2,'srch_year',$rows['year']);
            echo ">".$rows['year']."</optio>";
        }
while($rows = mysql_fetch_assoc($rs));
        echo "</select>";
    }

    mysql_free_result($rs);
}
function
getCover(){
    global $database;
    global $conndb;
    
    $sql = "SELECT * FROM covers ORDER BY name ASC";
    mysql_select_db($database, $conndb);
    $rs = mysql_query($sql, $conndb) or die(mysql_error());
    $rows = mysql_fetch_assoc($rs);
    $tot_rows = mysql_num_rows($rs);
    if($tot_rows > 0) {
        echo "<label for=\"srch_cover-0\">";
        echo "<input type=\"radio\" name=\"srch_cover\" id=\"srch_cover-0\"
        value=\"0\""
;
        getSticky(4,'srch_cover',0,1);
        echo "/>\nAny</label>";
        do {
            echo "<label for=\"srch_cover-".$rows['id']."\">";
            echo "<input type=\"radio\" name=\"srch_cover\" id=\"srch_cover-".$rows['id']."\"
            value=\""
.$rows['id']."\"";
            getSticky(4,'srch_cover',$rows['id']);
            echo "/>\n".$rows['name']."</label>";
        }
while($rows = mysql_fetch_assoc($rs));
    }
    
    mysql_free_result($rs);
}

function
getLocation() {
    global $database;
    global $conndb;
    
    $sql = "SELECT * FROM locations ORDER BY name ASC";
    mysql_select_db($database, $conndb);
    $rs = mysql_query($sql, $conndb) or die(mysql_error());
    $rows = mysql_fetch_assoc($rs);
    $total_rows = mysql_num_rows($rs);
    if($total_rows > 0){
        echo "<ul class=\"chkbx\">";
        do {
            echo "<li>";
            echo "<input type=\"checkbox\" name=\"srch_locations-".$rows['id']."\" id=\"srch_locations-".$rows['id']."\" value=\"".$rows['id']."\"";
            getSticky(3,"srch_locations-".$rows['id'],$rows['id']);
            echo "/><label for=\"srch_locations-".$rows['id']."\">".$rows['name']."</label>";
            echo "</li>";
        }
while($rows = mysql_fetch_assoc($rs));
        echo "</ul>";
    }

    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;
}
}

?>
 
PHP hulp

PHP hulp

15/11/2024 02:59:06
 
Pipo Clown

Pipo Clown

25/06/2014 19:16:24
Quote Anchor link
Voeg een onChange toe aan de selectboxen.

Via die onChange kun je, met behulp van AJAX, geselecteerde data refreshen.
 



Overzicht Reageren

 
 

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.