Javascript 搜索结果-先是PHP,然后是JS?

Javascript 搜索结果-先是PHP,然后是JS?,javascript,php,jquery,html,Javascript,Php,Jquery,Html,我正试图使我的网站上的搜索功能更简单一些: 库:调用和筛选结果的单个函数。 控制器:目前有两个函数,一个用于结果的初始PHP加载,另一个用于替换搜索页面的results div 在本例中,我有两个视图执行几乎完全相同的操作,这使得代码有点冗余 与第一个控制器关联的第一个视图加载页面的所有元素+results div。 第二个视图只更新results div,它只包含与results div相关的HTML 当用户更新搜索过滤器时,AJAX调用调用第二个控制器并用第二个视图替换初始视图 如何移动到一

我正试图使我的网站上的搜索功能更简单一些:

库:调用和筛选结果的单个函数。 控制器:目前有两个函数,一个用于结果的初始PHP加载,另一个用于替换搜索页面的results div

在本例中,我有两个视图执行几乎完全相同的操作,这使得代码有点冗余

与第一个控制器关联的第一个视图加载页面的所有元素+results div。 第二个视图只更新results div,它只包含与results 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;

}

如果没有看到您的代码,很难做出非常详细的评论,但是您可以通过查找查询参数来使用单个路由和控制器来选择请求所需的变量。您的URL可能如下所示:

GET /path/to/search?q=potatoes
对于初始荷载vs

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

这两个请求将命中相同的路由和控制器(伪代码如下!)

部分/完全渲染情况将涉及模板继承

full.template:

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

我的好搜索
结果表.模板

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

{{result}}

这是对一个非常一般的问题的非常一般的回答,伪代码只是示意图。

您能更新您的问题以包含您的代码吗?
<table>
    <tr>
        <td>{{result}}</td>
    </tr>
</table>