I was trying to fix the issue of "or" and "and" condition holder based on uncertain inputs by users. I wrote this and it works great for me.
below image shows the form.


In this form keyword input should match with all the available column in tables
if(isset($_POST['search'])) // Check whether form is submitted
{ if(isset($_POST['key'])) // Checks the field is set(present) or not
$key = is_not_null($_POST['key'])?$_POST['key']:false; // Set the value if user enters in input field if(isset($_POST['domain']))
$domain = is_not_null($_POST['domain'])?$_POST['domain']:false;
if(isset($_POST['course']))
$course = is_not_null($_POST['course'])?$_POST['course']:false;
if(isset($_POST['subdomain']))
$subdomain = is_not_null($_POST['subdomain'])?$_POST['subdomain']:false;
if(isset($_POST['duration']))
$duration = is_not_null($_POST['duration'])?$_POST['duration']:false;
if(isset($_POST['projectid']))
$projectid = is_not_null($_POST['projectid'])?$_POST['projectid']:false;
if(isset($_POST['addedby']))
$addedby = is_not_null($_POST['addedby'])?$_POST['addedby']:false;
$param=""; // Start a variable to use as conditional parameter in mysql query
$finalQuery = ""; // Start a variable to use as final parameter in mysql query
$kewQuery = ""; // Start a variable to use as keyword parameter in mysql query
$queryKeyCount = 0; // Start a variable to use in variable place to put "or" and "and" in mysql query
$query = DB_query("select * from ".TABLE_PROJECT_DETAILS.""); // select query to get columns of table $fieldName = mysql_field_array($query);
for($i=0;$i
$kewQuery .= " ".$fieldName[$i]." like '%".$key."%' or" ; // put "or" condition for keyword
else $kewQuery .= " ".$fieldName[$i]." like '%".$key."%'" ; // last "or" should not be here
}
foreach($_POST as $name => $value) { // this loop to match all the post value
$value = is_not_null($value)?$value:false; // set the value if it filled by user
if($value) // checks if value is filled
{
if($name != "search") // check the submit button value it should not increase the count
$queryKeyCount = $queryKeyCount + 1; // increase the count if any input is made by user
if ($queryKeyCount>1 && $name != "search" && $name != "sortProj") // check all the button and unwanted input
fields, it should be used to avoid adding unwanted "and" condition in query
$finalQuery .= " and ";
if($name == "key") // check if keyword is filled by user, if so then query should add above key query to final query $finalQuery .= " (".$kewQuery.") ";
if($name == "course")
$finalQuery .= "aca_project_suitable_for ='".$course."'"; // add the conditions according to requirements
if($name == "domain")
$finalQuery .= "aca_project_domain ='".$domain."'";
if($name == "subdomain")
$finalQuery .= "aca_project_subdomain ='".$subdomain."'";
if($name == "duration")
$finalQuery .= "aca_project_suggested_duration ='".$duration."'";
if($name == "projectid")
$finalQuery .= "aca_project_id = '".$projectid."'";
if($name == "addedby")
$finalQuery .= "aca_user_type ='".$addedby."'"; } }
if($queryKeyCount>0) // check whether user is made input in any field
$param .= "where "; // based on condition set the "where" clause in query
$param .= $finalQuery; // finally put the final query to go with the query
$sortByQuery = "";
if(isset($_POST['sortProj'])) // check if sort is choosen
{
$sortBy = $_POST['sortProj'];
if($sortBy == "sortByRate") // match the sore value
$sortByQuery = "order by aca_project_star DESC, aca_project_create_date DESC, aca_acadinnet_rate DESC, aca_project_total_workgroup DESC, aca_project_name"; // add sorting condition for the query
else if($sortBy == "sortByAcaRate")
$sortByQuery = "order by aca_acadinnet_rate DESC, aca_project_create_date DESC, aca_project_star DESC, aca_project_total_workgroup DESC, aca_project_name";
else if($sortBy == "sortByWorkgroup")
$sortByQuery = "order by aca_project_total_workgroup DESC, aca_project_create_date DESC, aca_acadinnet_rate DESC, aca_project_star DESC, aca_project_name";
else
$sortByQuery = "order by aca_project_create_date DESC, aca_project_star DESC, aca_acadinnet_rate DESC, aca_project_total_workgroup DESC, aca_project_name"; }
else {
$sortByQuery = "order by aca_project_create_date DESC, aca_project_star DESC, aca_acadinnet_rate DESC, aca_project_total_workgroup DESC, aca_project_name"; }
$param .= $sortByQuery;
$get_proj_query = DB_query("select aca_project_id as id from " . TABLE_PROJECT_DETAILS . " ".$param); $fav_proj_ids = get_fav_projects_id($_SESSION['']);
while($get_proj = DB_fetch_array($get_proj_query)) {
if(is_array($fav_proj_ids))
$fav = in_array($get_proj['id'],$fav_proj_ids)?true:false; echo draw_full_proj($get_proj['id'],$fav);
} }
Finally got a great output in any condition. Hope it will help you to find solutions for your search form development.
Comment if any doubt or suggestion.