-1

First of all, I'm a newbie PHP Developer.

I am developing a website which has a database that contains employee records. Each is assigned ID, Name, mobNo, Address etc..

There's a filter panel in the website with many filters to use like name filter, mobNo filter...

What I exactly want is, if 1 filter is selected, PHP will process just 1 MySQLi Query something like:

WHERE '$param' LIKE '%$paramValue%'

If user selects 2 filters, say Name and mobNo, PHP should process 2 queries something like:

WHERE '$param' LIKE '%$paramValue%'AND '$param2' LIKE '%$paramValue2%';

Chain should follow on.. I will use AJAX but you need NOT to worry about it.. I will integrate it myself.

DocRattie
  • 1,392
  • 2
  • 13
  • 27
  • http://stackoverflow.com/questions/767026/how-can-i-properly-use-a-pdo-object-for-a-select-query or http://stackoverflow.com/questions/27422784/multiple-sql-objects-in-php-mysqli – xQbert May 22 '15 at 14:04

2 Answers2

1

You should use a function like isset() or any other way to check what the user selected, and then display the appropriate query.

i.e.

$query = "SELECT blabla WHERE ";
$int = 0; //checks if query needs an 'AND'

    if (isset(filter1)) {
          $query += "$param1 LIKE $paramValue1";
          $int = 1;
    }
    if (isset(filter2)) {
          if ($int = 1)
             $query += " AND ";
          $query += "$param2 LIKE $paramValue2";
          $int = 1;
    }

and so on. Not quite sure that's what you need but hopefully its something like that. Be creative :)

Mirodinho
  • 1,171
  • 1
  • 13
  • 25
0
$sql = "WHERE '$param' LIKE '%$paramValue%'"; 
if($param2 != '')$sql .= "AND '$param2' LIKE '%$paramValue2%'";
if($param3 != '')$sql .= "AND '$param3' LIKE '%$paramValue3%'";
...

if you want the paramenters to be modular you need to add them in pieces depending on wether they are set or not.

DocRattie
  • 1,392
  • 2
  • 13
  • 27
  • 1
    Makes some logic.. If I conbine this idea with another response from Amrito, I'm sure I'll make it work out.. Thanks!! – Onish Garg May 22 '15 at 14:29