2015-12-13 94 views
1

我想用多字段搜索選項(例如名稱,學院,部門,年份,國籍e.t.c)使用ajax方法搜索數據。我有插入名稱進行搜索,其餘的字段都是空的,比它去foreach循環,但如果(isset($ _ GET [$ field])& &!empty($ _ GET ['$ field']))condition not success and去其他迴路多個字段的PHP搜索

$fields = array(
    'name' => TRUE, 
    'gender' => TRUE, 
    'colf' => TRUE, 
    'deptf' => TRUE, 
    'natf' => TRUE, 
    'fstatusf' => TRUE, 
    'fyearf' => TRUE 
); 
foreach ($fields as $field => $like) { 
    if (isset($_GET[$field]) && !empty($_GET['$field'])) { 
    $value = $_GET[$field]; 
    $search[] = $field . ($like ? ('LIKE "%' . $value . '%"') : ('="' . $value . '"')); 
     } 
} 
    if ($search) { 
    $sql = 'SELECT * FROM fmaf WHERE ' . implode(' or ' . $search); 
    } 

else{ 
$sql="SELECT * FROM fmaf"; 

    } 
+0

通止已經提供了這些輸入字段由用戶 – Rahul

+1

請請請,**不要忘記驗證/消毒用戶輸入**(例如''_GET') –

回答

0

最後我已經找到了解決辦法,並感謝cFreed和其他誰幫助我。我最關心的是,如果用戶想只有一個字段或超過1場在下面回答這種情況下,搜索對我來說是有益的,也可以是某人:

if (empty($_GET['name']) && empty($_GET['gender']) && empty($_GET['colf']) && empty($_GET['deptf']) && empty($_GET['natf']) && empty($_GET['fstatusf']) && empty($_GET['fyearf'])) 
{ 
    $sql="select * from fmaf "; 
} 
else 
{ 
$wheres = array(); 

$sql = "select * from fmaf where "; 

if (isset($_GET['name']) and !empty($_GET['name'])) 
{ 
    $wheres[] = "name like '%{$_GET['name']}%' "; 
} 

if (isset($_GET['gender']) and !empty($_GET['gender'])) 
{ 
    $wheres[] = "gender = '{$_GET['gender']}'"; 
} 

if (isset($_GET['colf']) and !empty($_GET['colf'])) 
{ 
    $wheres[] = "college = '{$_GET['colf']}' "; 
} 

if (isset($_GET['deptf']) and !empty($_GET['deptf'])) 
{ 
    $wheres[] = "department = '{$_GET['deptf']}' "; 
} 

if (isset($_GET['natf']) and !empty($_GET['natf'])) 
{ 
    $wheres[] = "nationality = '{$_GET['natf']}' "; 
} 

if (isset($_GET['fstatusf']) and !empty($_GET['fstatusf'])) 
{ 
    $wheres[] = "finalstatus = '{$_GET['fstatusf']}' "; 
} 

if (isset($_GET['fyearf']) and !empty($_GET['fyearf'])) 
{ 
    $wheres[] = "fyear = '{$_GET['fyearf']}' "; 
} 

foreach ($wheres as $where) 
{ 
$sql .= $where . ' AND '; // you may want to make this an OR 
    } 
$sql=rtrim($sql, "AND "); 

    } 
0

您需要根據請求構建查詢。 玩具的例子是這樣的:

$sql = "select * from student where 1 = 1".(isset($name)?" AND name like '%$name%":"").(isset($country)?" AND country = '$country'":"").";"; 
0

你可以用一個簡單的方法,能夠面對任何情況下,像這樣:

// define searchable fields, with option for LIKE|EQUAL (TRUE|FALSE) 
$fields = [ 
    'name' => TRUE, 
    'country' => TRUE, 
    'address' => TRUE, 
    'gender' => FALSE, 
    'state' => FALSE 
]; 
foreach ($fields as $field => $like) { 
    if (isset($_GET[$field]) AND !empty($_GET['$field'])) { 
    $value = $_GET[$field]; 
    // prepare WHERE condition item, depending on LIKE option 
    $search[] = $field . (
     $like ? ('LIKE "%' . $value . '%"') : ('="' . $value . '"') 
    ); 
    } 
} 
if ($search) { 
    $sql = 'SELECT * FROM student WHERE ' . implode(' AND ' . $search); 
} 
+0

我試過了,但出現錯誤,請參閱上面我已更新我的問題 –

+0

@SalmanKarim。我可能在我提出的代碼(未經測試)中存在一個錯誤,所以請準確報告**你得到的錯誤。 – cFreed

+0

你能看到上述問題的更新版本嗎? –