help on building a basic php search engine

坚强是说给别人听的谎言 提交于 2020-01-11 10:34:10

问题


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

  1. a search page with pagination, column header sorting, and multiple filtering(filters are in checkboxes)

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

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

here's my code for the controller

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

as for the table in the view:

<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. user searches for an item (a single term search) or using multiple categories(multiple term search)
  2. results are paginated, presented in tables, with each column header with links to a sorting method (sort.php?by=title)
  3. user gets to filter the sorted table (or if he/she did not do any sorting, then the current table will be filtered)

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:)


回答1:


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

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

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

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.

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




回答2:


correct me if I'm wrong. But one way of doing it would be to set your filter in a GET variable

Then you can a standalone PHP solution and javascript coming in a bit later.



来源:https://stackoverflow.com/questions/1739496/help-on-building-a-basic-php-search-engine

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!