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

时间:2014-06-11 03:03:33

标签: javascript php jquery html

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

Library:调用和过滤结果的单一函数。 Controller:目前有两个函数,一个用于初始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;

}

1 个答案:

答案 0 :(得分:0)

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

GET /path/to/search?q=potatoes

表示初始加载vs

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

用于AJAX。

这两个请求都会遇到相同的路由和控制器(伪代码跟随!

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' ?>

results_table.template

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

对于一个非常普遍的问题,这是一个非常普遍的答案,伪代码只是示意图。