English 中文(简体)
help on building a basic php search engine
原标题:

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

最佳回答

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.

问题回答

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.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签