微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

帮助建立一个基本的PHP搜索引擎

我到处寻找教程,但似乎无法得到一个好的…

>具有分页,列标题排序和多重过滤的搜索页面(过滤器位于复选框中)

问题:
分页工作,排序工作,但不能让他们一起工作.添加到过滤器使用分页和排序结果集

我想单独使用PHP和单独使用GET表单方法(javascript后来出现,我想在这个上应用渐进增强)

我不知道如何使三个功能(分页,排序和过滤)一起工作…想要我想实现

这是我的控制器代码

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;
    }

至于酒店图书馆的功能

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>

>用户搜索项目(单项搜索)或使用多个类别(多项搜索)
>结果是分页的,在表格中显示,每个列标题都带有一个排序方法链接(sort.PHP?by = title)
>用户过滤排序的表(或者如果他/她没有进行任何排序,那么将过滤当前表)

如果我应用所有过滤器(没有页面和排序),这里是url的样子:

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

它现在看起来很乱,但我想这就是它与搜索引擎网址的关系:)

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提交正确传递相同的数据,您需要传递A HREF链接中的每个变量.

要构建这个,尝试这样的事情:

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

当然,您需要清理数据URL友好并创建计数器以增加每个A HREF页面链接分页.

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

希望这会指出你正确的方向.

顺便说一句,使用$_SESSION你使用它的方式是一个很大的安全风险,除非你设置一个会话cookie并使用XSRF验证.

原文地址:https://www.jb51.cc/php/138820.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐