2014-06-11 4 views
-1

내 사이트에서 검색 기능을 좀 더 간단하게하려고합니다.검색 결과 - 첫 번째 PHP, 다음 JS?

라이브러리 : 결과를 호출하고 필터링하는 단일 함수입니다. 컨트롤러 : 현재 두 가지 기능 중 하나는 결과의 초기 PHP로드, 다른 하나는 검색 페이지의 결과 div를 대체합니다.

이 경우 거의 똑같은 일을하는 두 개의보기가있어서 코드가 약간 중복됩니다.

첫 번째보기는 첫 번째 컨트롤러와 연결되어 페이지의 모든 요소와 결과 div를로드합니다. 결과 div를 업데이트하는 두 번째보기에는 결과 div와 관련된 HTML 만 포함됩니다.

사용자가 검색 필터를 업데이트하면 AJAX 호출이 두 번째 컨트롤러를 호출하고 초기보기를 두 번째보기로 바꿉니다.

하나의보기로 어떻게 이동할 수 있습니까?

컨트롤러 :

public function display($criteria = null) 
{ 
    // create an instance of the view 
    $this->template->content = View::instance('v_results_display'); 

    // name the page 
    $this->template->title = "Search Results"; 

    //$filtered_results->teacher_results = $this->teachers->filter_teachers(array('criteria' => $_POST['criteria'], 'countries' => $_POST['filters']['countries'], 'stations' => $_POST['filters']['stations'], 'min' => $_POST['min'], 'max' => $_POST['max'])); 

    if($_POST['criteria']) 
    { 
     $criteria = $_POST['criteria']; 
    } 

    # SEARCH BY GROUP 
    // allow search by group 
    // i.e. if someone searches for math, it should bring up all teachers who teach any kind of math 

     switch($criteria) 
     { 
      case ("math"): 
      case ("science"): 
      case ("language"): 
      case ("college prep"): 
      case ("test prep"): 
      case ("english"): 
      case ("other"): 
       $criterions = $this->subjects->get_subjects_return_implode($criteria); 
       break; 
      default: 
       $criterions = $criteria; 
       break; 
     } 

    if($_GET['page']) 
    { 
     if(is_numeric($_GET['page'])) 
     { 
      $pageNum = intval($_GET['page']); 
      if($pageNum == 0) 
      { 
       $pageNum = null; 
      } 

     } 
     else 
     { 
      $pageNum = null; 
     } 

    } 

    // call library function for searching 
    // maintaining the integrity of the group search 
    //$teacher_results = $this->teachers->search_teachers_with_or_without_criteria($criterions, $pageNum); 

    //$filtered_results->teacher_results = $this->teachers->filter_teachers(array('criteria' => $_POST['criteria'], 'countries' => $_POST['filters']['countries'], 'stations' => $_POST['filters']['stations'], 'min' => $_POST['min'], 'max' => $_POST['max'])); 


    $teacher_results = $this->teachers->filter_teachers(array('criteria' => $criterions)); 

    $teacher_results = $this->teachers->filter_teachers(array('criteria' => $criterions, 'countries' => $_POST['filters']['countries'], 'stations' => $_POST['filters']['stations'], 'min' => $_POST['min'], 'max' => $_POST['max'])); 

    // a non limited count of all the results, regardless of what page they appear on 
    // the previous query has a LIMIT in the SQL, so it can't offer a total count of all teachers 
    // across all pages 
    $this->template->content->totalResultsCount = count($this->teachers->get_teachers()); 

    $like_dislike = new Teachers(); 
    $this->template->content->like_dislike = $like_dislike; 

    // pass to view accessible variable 
    $this->template->content->teacher_results = $teacher_results; 

    // pass criteria variable to be used in view 
    // this will probably only be used to echo out to the user what is going on 
    $this->template->content->criteria = $criteria; 

    // get teacher count for all pages 
    $this->template->content->unlimitedTeacherCount = $this->teachers->CountTeachersAllPages($criterions); 

    // for the hidden div, which helps with the filtering 
    $this->template->content->criterions = $criterions; 

    $app_form = new App_Form(); 
    $this->template->content->app_form = $app_form; 

    $app_user_var = new App_User(); 
    $this->template->content->app_user_var = $app_user_var; 

    # CSS/JS includes 

    $client_files_head = Array(
     '/css/results.css', 
     '/css/mobile_results.css', 
     "//code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" 

    ); 

    $client_files_body = Array(
     '/js/results.js', 
     '/js/jquery.browser.min.js', 
     'http://libs.baidu.com/jqueryui/1.8.22/jquery-ui.min.js' 
    ); 

    // load CSS/JS files 
    $this->template->client_files_body = Utils::load_client_files($client_files_body); 
    $this->template->client_files_head = Utils::load_client_files($client_files_head); 

    // render template 
    echo $this->template; 

} 

// may have gotten rid of the need for this guy 
public function search_filter() { 

    // create an instance of the view 
    $filtered_results = View::instance('v_results_search_filter'); 

    // return teachers sought 
    $filtered_results->teacher_results = $this->teachers->filter_teachers(array('criteria' => $_POST['criteria'], 'countries' => $_POST['filters']['countries'], 'stations' => $_POST['filters']['stations'], 'min' => $_POST['min'], 'max' => $_POST['max'])); 

    $client_files_head = Array (
     '/css/bootstrap-formhelpers.min.css', 
     '/css/results.css' 
    ); 

    // so that our country prefixes are defined correctly 
    $client_files_body = Array(
     '/js/bootstrap-formhelpers-countries.js' 
    ); 

    // load CSS/JS files 
    $filtered_results->client_files_head = Utils::load_client_files($client_files_head); 
    $filtered_results->client_files_body = Utils::load_client_files($client_files_body); 

    $app_user_var = new App_User(); 
    $filtered_results->app_user_var = $app_user_var; 

    // make filtered results available to page 
    $filtered_results->filters = $_POST['filters']; 

    // for the ratings 
    $like_dislike = new Teachers(); 
    $filtered_results->like_dislike = $like_dislike; 

    // make min and max pay available to page 
    $filtered_results->min = $_POST['min']; 
    $filtered_results->max = $_POST['max']; 

    $filtered_results->post = $_POST; 

    // send response back to AJAX 
    echo $filtered_results; 

} 
+0

코드를 포함하도록 질문을 업데이트 할 수 있습니까? – p1xelarchitect

답변

0

매우 상세한 발언을하기 어렵다, 그러나 당신이 요청에 필요한되는 변형 선택하는 쿼리 PARAM를 찾아 단일 경로 및 컨트롤러를 사용할 수있는 코드를 보지 않고. AJAX를위한

GET /path/to/search?q=potatoes&partial=true 

대 초기로드에 대한

GET /path/to/search?q=potatoes 

: 귀하의 URL은 다음과 같을 수 있습니다.

이러한 요청은 모두 동일한 경로 및 컨트롤러를 칠 것이다 (의사 코드는 다음과!)

route("/path/to/search", mySearchController) 

function mySearchController(args) { 
    result = do_some_query(args) 
    if ('partial' in queryparams) { 
     do_partial_render(result) 
    } else { 
     do_full_render(result) 
    } 
} 

부분/렌더링 전체 상황을 포함 할 템플릿 상속

full.template :

<? include 'header' ?> 
<h1>My Nice Search</h1> 
<? include 'results_table' ?> 
<? include 'footer' ?> 

결과 테이블 템플릿

<table> 
    <tr> 
     <td>{{result}}</td> 
    </tr> 
</table> 

꽤 일반적인 질문에 대한 일반적인 대답입니다. 의사 코드는 설계도로만 사용됩니다.

+0

감사합니다. 내 코드를 고려할 때 좀 더 구체적으로 설명 할 수 있습니까? – compguy24

관련 문제