帮助建立基本的php搜索引擎 [英] help on building a basic php search engine

查看:77
本文介绍了帮助建立基本的php搜索引擎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到处都在寻找教程,但似乎并没有找到很好的教程...

i looked for tutorials everywhere but just can't seem to get a good one...

  1. 具有分页,列标题排序和多个过滤功能的搜索页面(过滤器位于复选框中)

问题: 分页工作,分类工作,但不能让他们一起工作.此外,还可以使筛选器与分页和排序的结果集一起使用

the problem: had pagination working, had sorting working, but can't get them to work together. add to that getting the filters working with a paginated and sorted resultset

我想单独使用php和GET表单方法来完成此工作(javascript稍后出现,我想对此应用渐进式增强)

i want to make this work with php alone and with GET form methods alone (javascript comes in later, I want to apply progressive enhancement on this one)

我不知道如何使三种功能(分页,排序和过滤)协同工作……我想实现

i have no idea how to make the three functionalities(pagination, sorting and filtering) work together...want I want to achieve

这是我的控制器代码

function index(){
        $resultset = null;
        if ($this->input->get()){
            // searching
            if ($this->input->get('keyword')){
                $resultset = $this->hotel->searchterm($_GET['keyword']);
            }

            if (!$this->input->get('keyword')){
                $searchkeys = array();
                foreach($_GET as $key => $value){
                    $searchkeys[$key] = $value;
                }
                $resultset = $this->hotel->searchcat($searchkeys);
            }

            // sorting
            if ($this->input->get('sortby')){
                $resultset = $this->hotel->sortdefault($_GET['sortby']);
            }

            if ($this->input->get('keyword') && $this->input->get('sortby')){
                $resultset = $this->hotel->sortdefault($_GET['sortby']);
            }
        }else{
            $resultset = ORM::factory('hotel')->limit(15)->find_all();
        }
        $this->template->title = 'The Hotel Inventory :: Search';
        $this->template->sidebar = new View('search-sidebar');
        $this->template->featured = new View('search-details');
        $this->template->featured->data = $resultset;
    }

关于酒店图书馆的功能:

as for the Hotel library functions:

public function sortdefault($sort, $resultset=null){
        if (!$this->session->get('sortorder')){
            $_SESSION['sortorder'] = 'asc';
        }else if ($this->session->get('sortorder') == 'asc'){
            $_SESSION['sortorder'] = 'desc';
        }else{
            $_SESSION['sortorder'] = 'asc';
        }

        $sortorder = $this->session->get('sortorder');
            $sortby = '';
            switch ($sort){
                case 'name' :
                    $sortby = 'name';
                    break;
                case 'location':
                    $sortby = 'location';
                    break;
                case 'price' :
                    $sortby = 'price';
            }
            if (is_null($resultset)){
                $query = ORM::factory('hotel')->
                select('id, name, location, room, price, date_added, url_path')->
                    limit(15)->orderby($sortby, $sortorder)->find_all();
            }
            return $query;
    }

关于视图中的表:

<table id="tableresults" cellpadding="0" cellspacing="0" border="1" >
           <thead>
               <tr style="height: 20px;">
                   <th>Image</th>
                   <th><?php echo (isset($_SESSION['searchterm'])) ?
                        html::anchor('search/index?keyword=' . $_SESSION['searchterm'] . '&sortby=name','Hotel Name') :
                        html::anchor('search/index?sortby=name','Hotel Name');?></th>
                   <th><?php echo html::anchor('search/index?sortby=location','Location');?></th>
                   <th><?php echo html::anchor('search/index?sortby=price','Price');?></th>
                   <th>Actions</th>
               </tr>
           </thead>

           <tbody>
           <?php
           foreach($data as $d){
               echo '<tr class="result">';
               echo '<td>&nbsp;</td>';
               echo '<td>' . html::anchor('hotel/' . $d->url_path, $d->name) . '</td>';
               echo '<td>' . $d->location . '</td>';
               echo '<td>USD ' . $this->util->money($d->price);
               echo '<td>&nbsp</td>';
               echo '</tr>';
           }
           ?>
           </tbody>

  1. 用户搜索项目(单个术语搜索)或使用多个类别(多个术语搜索)
  2. 将结果分页显示在表格中,每个列标题均带有排序方法的链接(sort.php?by = title)
  3. 用户可以过滤已排序的表(或者,如果他/她没有进行任何排序,那么当前表将被过滤)

如果我应用所有过滤器(没有页面并且尚未排序),则网址如下所示:

here is what the url looks like if i apply all filters(without pages and sort yet):

http://localhost/thi/search/index.html?filter[]=featured&filter[]=bankowned&filter[]=new&filter[]=owner&filter[]=broker&filter[]=bank

到目前为止,它看起来很凌乱,但我想搜索引擎网址就是这样:)

it looks messy as of now but I guess that's just how it is with search engine urls:)

推荐答案

我不知道如何使三种功能(分页,排序和过滤)协同工作……我想实现

i have no idea how to make the three functionalities(pagination, sorting and filtering) work together...want I want to achieve

GET提交与POST提交有些不同,因为您使用提交的INPUT一次传递了所有变量.

GET submits are a bit different than POST submits, since you pass all of your variables in one shot with the submit INPUT.

因此,要通过GET提交正确传递相同的数据,您将需要在A HREF链接中传递 each 变量.

So to pass the same data properly with a GET submit, you will need to pass each variable in the A HREF link.

要构建此文件,请尝试如下操作:

To build this, try something like this:

$URLsubmit=(TheSubmitURL).'?';
foreach ($submit_data as $key => $value)
  {
  $URLsubmit.=$key.'='.$value.'&';
  }
$URLsubmit = substr($URLsubmit,0,strlen($URLsubmit)-1); // remove last '&'

自然地,您将希望清理数据URL友好并创建计数器以增加每个A HREF页面链接的分页.

Naturally you will want to scrub the data URL friendly and create counters to increment pagination with each A HREF page link.

echo '<A HREF="'.$URLsubmit.'">';

希望这将为您指明正确的方向.

Hopefully that will point you in the right direction.

顺便说一句,除非您设置会话cookie并使用XSRF验证,否则以$ _SESSION的方式使用它会带来很大的安全风险.

BTW, using $_SESSION the way you are using it is a big security risk, unless you set a session cookie and use XSRF validation.

这篇关于帮助建立基本的php搜索引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆