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

前端之家收集整理的这篇文章主要介绍了帮助建立一个基本的PHP搜索引擎前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我到处寻找教程,但似乎无法得到一个好的…

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

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

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

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

这是我的控制器代码

  1. function index(){
  2. $resultset = null;
  3. if ($this->input->get()){
  4. // searching
  5. if ($this->input->get('keyword')){
  6. $resultset = $this->hotel->searchterm($_GET['keyword']);
  7. }
  8.  
  9. if (!$this->input->get('keyword')){
  10. $searchkeys = array();
  11. foreach($_GET as $key => $value){
  12. $searchkeys[$key] = $value;
  13. }
  14. $resultset = $this->hotel->searchcat($searchkeys);
  15. }
  16.  
  17. // sorting
  18. if ($this->input->get('sortby')){
  19. $resultset = $this->hotel->sortdefault($_GET['sortby']);
  20. }
  21.  
  22. if ($this->input->get('keyword') && $this->input->get('sortby')){
  23. $resultset = $this->hotel->sortdefault($_GET['sortby']);
  24. }
  25. }else{
  26. $resultset = ORM::factory('hotel')->limit(15)->find_all();
  27. }
  28. $this->template->title = 'The Hotel Inventory :: Search';
  29. $this->template->sidebar = new View('search-sidebar');
  30. $this->template->featured = new View('search-details');
  31. $this->template->featured->data = $resultset;
  32. }

至于酒店图书馆的功能

  1. public function sortdefault($sort,$resultset=null){
  2. if (!$this->session->get('sortorder')){
  3. $_SESSION['sortorder'] = 'asc';
  4. }else if ($this->session->get('sortorder') == 'asc'){
  5. $_SESSION['sortorder'] = 'desc';
  6. }else{
  7. $_SESSION['sortorder'] = 'asc';
  8. }
  9.  
  10. $sortorder = $this->session->get('sortorder');
  11. $sortby = '';
  12. switch ($sort){
  13. case 'name' :
  14. $sortby = 'name';
  15. break;
  16. case 'location':
  17. $sortby = 'location';
  18. break;
  19. case 'price' :
  20. $sortby = 'price';
  21. }
  22. if (is_null($resultset)){
  23. $query = ORM::factory('hotel')->
  24. select('id,name,location,room,price,date_added,url_path')->
  25. limit(15)->orderby($sortby,$sortorder)->find_all();
  26. }
  27. return $query;
  28. }

至于视图中的表:

  1. <table id="tableresults" cellpadding="0" cellspacing="0" border="1" >
  2. <thead>
  3. <tr style="height: 20px;">
  4. <th>Image</th>
  5. <th><?PHP echo (isset($_SESSION['searchterm'])) ?
  6. html::anchor('search/index?keyword=' . $_SESSION['searchterm'] . '&sortby=name','Hotel Name') :
  7. html::anchor('search/index?sortby=name','Hotel Name');?></th>
  8. <th><?PHP echo html::anchor('search/index?sortby=location','Location');?></th>
  9. <th><?PHP echo html::anchor('search/index?sortby=price','Price');?></th>
  10. <th>Actions</th>
  11. </tr>
  12. </thead>
  13.  
  14. <tbody>
  15. <?PHP
  16. foreach($data as $d){
  17. echo '<tr class="result">';
  18. echo '<td>&nbsp;</td>';
  19. echo '<td>' . html::anchor('hotel/' . $d->url_path,$d->name) . '</td>';
  20. echo '<td>' . $d->location . '</td>';
  21. echo '<td>USD ' . $this->util->money($d->price);
  22. echo '<td>&nbsp</td>';
  23. echo '</tr>';
  24. }
  25. ?>
  26. </tbody>

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

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

  1. 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链接中的每个变量.

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

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

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

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

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

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

猜你在找的PHP相关文章