I\'m trying to create a search/filtering option in my blood donor application. Where donor can be searched by sex, name, blood group or by selecting all three. Here is my code>
Try like this:
function search_donar($_POST) {
$by_name = $_POST['by_name'];
$by_sex = $_POST['by_sex'];
$by_group = $_POST['by_group'];
$by_level = $_POST['by_level'];
$isfirst=0;
$search_query = "SELECT * FROM donar WHERE";
if($by_name !="") {
$search_query .= " name='$by_name'";
$isfirst=1;
}
if($by_sex !="") {
if($isfirst!=0)
$search_query .= " AND ";
$search_query .= " sex='$by_sex'";
$isfirst=1;
}
if($by_group !="") {
if($isfirst!=0)
$search_query .= " AND ";
$search_query .= " blood_group='$by_group'";
$isfirst=1;
}
if($by_level !="") {
if($isfirst!=0)
$search_query .= " AND ";
$search_query .= " e_level='$by_level'";
$isfirst=1;
}
$result = mysql_query($search_query);
return $result;
}