2013-07-05 4 views
1

나는 php를 사용하여 mysql 데이터베이스에서 검색을 수행하는 다른 방법으로 6 개의 다른 검색 필터를 지정해야합니다. 나는 그것에 대해 어떻게 가는지 혼란 스럽다.PHP에서 필터를 사용하여 검색을 수행하려면 어떻게합니까?

나는 웹 사이트를 운영하고있어 6 가지 검색 필터를 사용하여 매우 큰 검색을 수행해야합니다.

이 복잡한 논리로 많은 양의 코드를 작성한 것은 이번이 처음이기 때문에 나에게 복잡해 보입니다.

<input type='text' name='searchterm' /> 

<select name='ind' class='custom-select'> 
    <option value='0'> Select Industry</option> 
    <option value='1'> Real Estate </option> 
    <option value='2'> Hospitality/Hotel/Tourism/Travel & Aviation </option> 
    <option value='3'> Financial Services/Banking </option> 
</select>   

<select name='spec' class='custom-select'> 
    <option value='0'> Select Specialization</option> 
    <option value='1'> Accounting/Banking & Finance/Insurance </option> 
    <option value='2'> Administration/Management/Executive </option> 
    <option value='3'> Architecture/Construction/Civil </option> 
</select>   

<select name='loc' class='custom-select'> 
    <option value='0'> Select Location</option> 
    <option value='1'> Lagos </option> 
</select> 

전체 HTML 코드가 여기에 있습니다 : :

는 HTML 코드입니다 http://jsbin.com/otibel/1/

이 6 개 개의 선택 상자가 있으며, 각각 검색 필터입니다.

사용자는 한 번에 하나의 선택 상자를 선택하고 한 번에 두 개 또는 세 개의 검색 필터를 검색하거나 선택하는 등의 작업을 수행 할 수 있습니다.

필자는 검색 필터를 6 번만 관리했지만 사용자가 단 하나의 검색 필터 만 선택하면이 코드가 사용됩니다.

function performSearchWithFilter(){ 
    if (($_GET['ind'] > 0) && (isset($_GET['ind'])) && ($_GET['spec'] <= 0) && ($_GET['loc'] <= 0) && ($_GET['workexp'] <= 0) && ($_GET['type'] <= 0) && ($_GET['qualfctn'] <= 0)){ 
     //it will carry out the search, when just the ind select box has been selected 
    } else if (($_GET['ind'] <= 0) && ($_GET['spec'] > 0) && isset($_GET['spec']) && ($_GET['loc'] <= 0) && ($_GET['workexp'] <= 0) && ($_GET['type'] <= 0) && ($_GET['qualfctn'] <= 0)) { 
     //it will carry out the search, when just the spec select box has been selected 
    } else if (($_GET['ind'] <= 0) && ($_GET['spec'] <= 0) && ($_GET['loc'] > 0) && (isset($_GET['loc'])) && ($_GET['workexp'] <= 0) && ($_GET['type'] <= 0) && ($_GET['qualfctn'] <= 0)) { 
     //it will carry out the search, when just the loc select box has been selected 
    } else if (($_GET['ind'] <= 0) && ($_GET['spec'] <= 0) && ($_GET['loc'] <= 0) && ($_GET['workexp'] > 0) && isset($_GET['workexp']) && ($_GET['type'] <= 0) && ($_GET['qualfctn'] <= 0)) { 
     //it will carry out the search, when just the workexp select box has been selected 
    } else if (($_GET['ind'] <= 0) && ($_GET['spec'] <= 0) && ($_GET['loc'] <= 0) && ($_GET['workexp'] <= 0) && ($_GET['type'] > 0) && isset($_GET['type']) && ($_GET['qualfctn'] <= 0)) { 
     //it will carry out the search, when just the type select box has been selected 
    } else if (($_GET['ind'] <= 0) && ($_GET['spec'] <= 0) && ($_GET['loc'] <= 0) && ($_GET['workexp'] <= 0) && ($_GET['type'] <= 0) && ($_GET['qualfctn'] > 0) && isset($_GET['qualfctn'])) { 
     //it will carry out the search, when just the qualfctn select box has been selected 
    } 
} 

나는 이와 같은 검색에 대한 코드를 작성할 수 있지만, 25 개의 다른 else 문과이보기 복합체 여야합니다.

은이 같은 알고리즘에 배치 된 6 개 검색 파라미터 갖도록 순열 및 조합 수학 식을 사용했을 때, 사용자가 검색 필터를 선택하면,이 I 만든 검색 필터 알고리즘 인

ind, spec, loc, workexp, type, qualfctn, ind & spec, ind & loc, ind & workexp, ind & type, ind & qualfctn, spec & loc, spec & workexp, spec & type, spec & qualfctn, loc & workexp, loc & type, loc & qualfctn, workexp & type, workexp & qualfctn, type & qualfctn, ind & spec & loc & workexp & type & qualfctn, ind & spec & loc & workexp & type, ind & spec & loc & workexp, ind & spec & loc. 

을, 즉 다른 방식으로

PHP에서 검색 플러그인, 프레임 워크 또는 Librbary가 나를 위해이 작업을 수행 할 수 있으며 여러 필터로 검색하고 내가 작성한 코드의 양을 최소화 할 수 있으므로이 코드를 작성하거나 다시 작성하지 않아도됩니다. 바퀴는 다시 한번, 나는 무엇을해야하고 어떻게해야하는지에 대해 정말로 혼란 스럽습니다.

+0

'에''그 기본 옵션'<옵션 값 = '0'> 업종 선택을 변경하고 PHP 코드'는 isset을 ($ _ GET [ '공업'])'사용하여 테스트 받기 . 이렇게하면 훨씬 더 깨끗해 보입니다. –

+0

까마귀 까마귀 나는 이해하지 못합니다, 당신은 더 설명 할 수 있습니까? –

+0

기본적으로 SQL 문자열 작성기 패턴을 찾고 있습니다. –

답변

1

일단 모든 변수를 읽으려면 다소 긴 코드가 필요하지만 읽은 후에는 간단한 쿼리 문을 사용하여 검색을 실행하십시오. 주석에 설명 된대로 데이터베이스 테이블의 구조에 대해 몇 가지 가정을하고 데이터베이스 연결을 초기화해야합니다.

// this code assumes that $link has been initialized as a mysqli object 
// with database connection open. 
// assuming database table name is "people" 
// assuming database table column names match the names of GET variables 
// if any of these assumptions are incorrect, you'll need to modify the 
// code to match the DB 

// initialize WHERE conditions 
$conditions = "1=1"; 

// test for ind 
if(isset($_GET['ind']) && ($_GET['ind'] > 0)) { 
    $conditions .= " AND ind='".$link->real_escape_string($_GET['ind'])."'"; 
} 

// test for spec 
if(isset($_GET['spec']) && ($_GET['spec'] > 0)) { 
    $conditions .= " AND spec='".$link->real_escape_string($_GET['spec'])."'"; 
} 

// test for loc 
if(isset($_GET['loc']) && ($_GET['loc'] > 0)) { 
    $conditions .= " AND loc='".$link->real_escape_string($_GET['loc'])."'"; 
} 

// test for workexp 
if(isset($_GET['workexp']) && ($_GET['workexp'] > 0)) { 
    $conditions .= " AND workexp='".$link->real_escape_string($_GET['workexp'])."'"; 
} 

// test for type 
if(isset($_GET['type']) && ($_GET['type'] > 0)) { 
    $conditions .= " AND type='".$link->real_escape_string($_GET['type'])."'"; 
} 

// test for qualfctn 
if(isset($_GET['qualfctn']) && ($_GET['qualfctn'] > 0)) { 
    $conditions .= " AND qualfctn='".$link->real_escape_string($_GET['qualfctn'])."'"; 
} 

// make sure we have at least one condition 
if(!$first) { 
    if(!$result = $link->query(" 
     SELECT * 
     FROM people 
     WHERE ".$conditions 
    )) { 
     // your error handling here 
    } 

    // read the results here 

    $result->free(); 
} 
+0

답안에있는 모든 '$ first-'논리를 건너 뛰고 쿼리에'WHERE 1 = 1'을 추가하면됩니다 (이것은 일정한 평가이므로 어떤 성능도 취하지 않습니다). '$ conditions. = "AND ..."를 사용하면 코드를 훨씬 더 읽기 좋게 만들 수 있습니다. – s1lv3r

+0

큰 제안! 고맙습니다. :) – Zeeb

관련 문제