2

我到处寻找教程,但似乎找不到一个好的教程...

  1. 具有分页、列标题排序和多重过滤的搜索页面(过滤器在复选框中)

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

我想单独使用 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>
  1. 用户搜索一个项目(单项搜索)或使用多个类别(多项搜索)
  2. 结果被分页,以表格形式呈现,每个列标题都带有指向排序方法的链接(sort.php?by=title)
  3. 用户可以过滤已排序的表(或者如果他/她没有进行任何排序,则将过滤当前表)

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

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

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

4

2 回答 2

0

如我错了请纠正我。但是一种方法是将过滤器设置在 GET 变量中

然后,您可以稍后推出独立的 PHP 解决方案和 javascript。

于 2009-11-17T13:26:24.443 回答
0

我不知道如何让这三个功能(分页、排序和过滤)一起工作......我想要实现

GET 提交与 POST 提交有点不同,因为您使用提交 INPUT 一次性传递所有变量。

因此,要通过 GET 提交正确传递相同的数据,您需要传递A HREF 链接中的每个变量。

要构建它,请尝试以下操作:

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

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

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

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

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

于 2009-11-20T09:32:58.773 回答