Help to build a basic php search engine

I've searched for tutorials everywhere but just couldn't seem good ...

  • paginated search page, sorting column headings and multiple filtering (filters are in checkboxes)

problem: worked paginated, sort worked but couldn't get them to work together. add to this getting filters working with paginated and sorted result set

I want to make this work with php only and only with GET form methods (javascript comes later, I want to apply progressive improvement on this)

I don't know how to make the three functions (pagination, sorting and filtering) work together ... I want what 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 functions of the hotel library:

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 a table in a 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>

      

  • user searches for an item (single term search) or using multiple categories (multi-user search)
  • the results are paginated, presented in tables with each column heading with links to the sorting method (sort.php? by = title)
  • the user gets the option to filter the sorted table (or if he / she does not do the sort, then the current table will be filtered)

This is what the URL looks like if I apply all filters (no pages and no sorting yet):

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

      

now it looks like a mess, but i guess that's exactly with search urls :)

+2


source to share


2 answers


I don't know how to make the three functions (pagination, sorting and filtering) work together ... I want what I want to achieve

GET views are a little different from POST submissions in that you are passing all of your variables in one shot using an INPUT submission.

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

To create 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 clean up the data url and create counters to increase the pagination with each link to the HREF page.

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

      

Hopefully this will point you in the right direction.

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

0


source


correct me if i am wrong. But one way to do it is to set the filter to a GET variable



Then you can execute the standalone PHP and javascript solution in a few seconds.

0


source







All Articles