0

I'm trying to create a PHP script to filter data from a MySQL DB. Filters are selected with HTML Select tags.

The problem is, there are many filters, and a user may only select some of them. I'm quite confused about how to use PHP to dynamically adjust the MySQL select statement so that it only uses those filters selected by the user. That is, if the user select SchoolType and District. The query is something like this:

"SELECT * FROM mydatabase 
 WHERE SchoolType='SelectedSchoolType' 
   AND District='SelectedDistrict'"

And then if the user also select County in the filter, the SQL statement adjusts to:

"SELECT * FROM mydatabase 
 WHERE SchoolType='SelectedSchoolType' 
   AND District='SelectedDistrict' 
   AND County='SelectedCounty'"

How can I make the SQL statement adjust according to the filter selected?

2
  • 1
    How can I make the SQL statement adjust according to the filter selected? What selected filters Maybe you should also show us the form where the user selects these filters Commented Jun 28, 2016 at 21:04
  • Click "edit" below your question, paste your HTML and PHP codes, save your question. Commented Jun 28, 2016 at 21:12

2 Answers 2

3

On your PHP page, you need to:

  1. Check the select values (switch)
  2. Construct your query from those selects (I used implode)
  3. Run your query (as you normally would)

Example:

<?php
$sql = "Select * FROM mydatabase";
$conditions = array();
switch($_POST['select_1']){//you can have mutiple switches
    case "school":
        $conditions[] = "SchoolType = '".$_POST['select_1_text']."'";
        break;
    case "district":
        $conditions[] = "District = '".$_POST['select_1_text']."'";
        break;
    //etecetera
}
if(count($conditions)){
    $sql.= " WHERE ";
    $sql.=implode(" AND",$conditions);
}
//Run your query

Check this related question about dynamic queries

Sign up to request clarification or add additional context in comments.

Comments

0

So firstly you need to send filter type id and selected values to server side. For example you can use jquery (for 1 selected value):

$.ajax({
  method: "POST",
  url: "path_to_your_script",
  data: {
    type: $("your_element:selected").val(), 
    filter : $("your_element:selected").text()}
  });

Then on the server side you can dynamically create query for your database:

<?php
$input_filter = $_POST['filter'];
$my_mapped_type = get_mapped_type($_POST['type']); //for.ex. you store it in db
$base_query = "SELECT * FROM mydatabase";
if(isset($input_filter) && isset($my_mapped_type)){
  $base_query .= " where $my_mapped_type = $input_filter";
}

As result you'll have whole query in $base_query

3 Comments

I don't think he was loking for an ajax or javascript answer, considering the question itself and it's tags
if you won't use ajax or javascript so you need to decide how to send selected values by user to server side. you can use form submit )
I think that is implicit and does not seem like @Mishidoo 's problem, since he does not have difficulties in passing the values but rather in constructing a query dynamically

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.