subhomoy Posted July 31, 2014 Share Posted July 31, 2014 Hello every body.... I'm creating a simple php project where the user will be able to choose option from dropdown menu and the query will be executed accordingly.. The html form is shown below.... <select name='movie_name'> <option value='ALL'>All</option> <option value='blah'>Blah blah </option> <option value='blah'>Blah blah </option> ... </select> <select name='movie_genre'> <option value='ALL'>All</option> <option value='Horror'>Horror</option> <option value='Anime'>Anime</option> ... </select> <select name='movie_cast'> <option value='ALL'>All</option> <option value='john'>john</option> <option value='Katrina'>Blah blah Katrina</option> ... </select> If the user select "ALL" then, all the resuls will be displayed according to the selected options AND again If the user select "different" options then, all the resuls matcing from the database will be shown... Tha database schema is shown below | id | movie_name| movie_genre | movie_cast | 1 | Hello Brother | Comedy | Salman Khan | 2 | Blah blah | ALL | Blah blah | 3 | ALL | ALL | ALL | 4 | Blah Blah | Blah Blah | ALL * here "ALL" is a specific text which may or may not be present in the rows.... Thank u in advance.... Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted July 31, 2014 Share Posted July 31, 2014 (edited) You need to dynamically build your query, specifically the WHERE clause. Example code // define the basic query $sql = 'SELECT movie_name, movie_genre, movie_cast FROM movies_table'; $where = array(); // dynamically build the WHERE clause conditions if($_POST['movie_name'] != 'All') { $where[] = "movie_name='{$_POST['movie_name']}'"; // <-- never do this, only an example } if($_POST['movie_genre'] != 'All') { $where[] = "movie_genre='{$_POST['movie_genre']}'"; // <-- never do this, only an example } if($_POST['movie_cast'] != 'All') { $where[] = "movie_cast='{$_POST['movie_cast']}'"; // <-- never do this, only an example } // apply the where clause conditions to the query. if(!empty($where)) $sql .= ' WHERE ' . implode(' AND ', $where); // execute the query NOTE: The above is only an example. You need to take extra steps before using it in your code, such as sanitizing _POST vars etc. Edited July 31, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.