Jump to content

Recommended Posts

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....

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/290207-query-database-according-to-user-choice/
Share on other sites

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 by Ch0cu3r
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.