Help setting logic for advanced search options in PHP

I would like to create an advanced search form, as well as in the workplace, where there would be such criteria as a keyword, job type, minimum payment, maximum payment, category, subcategory, etc.

My problem is how best to install this, so if I need to add categories to the parameters, I do not need to change a whole bunch of queries and functions, etc.

My best guess would be to create some kind of associative array from all the potential parameters and reuse that array, but for some reason I feel it is a lot more complicated than that. I use CodeIgniter as an MVC framework, if that matters.

Does anyone have a suggestion on how best to set this up?

Keep in mind that I will need to create links like index.php? keyword = designer & job_type = 2 & min_pay = 20 & max_pay = 30

I hope my question is not vague.

+5
source share
9 answers

I don’t know what you need, but I usually create a search class.

<?php
$search = new Search('people');
$search->minPay(1000);
$search->maxPay(4000);
$search->jobType('IT');
$results = $search->execute();

foreach ($results as $result)
{
  //whatever you want
}

?>

You can use all of these methods or have some mapping in __set()between the method name and the database field. The parameter passed to the constructor is the table in which the main query should be executed. __set()You must take care of the methods or the matching in any necessary connection and the fields you need to join.

+2

"", . ActiveRecord, . CI , SQL.

if($this->input->get('min_pay')) {
  $this->db->where('min_pay <', $this->input->get('min_pay'));
}

if($this->input->get('keyword')) {
  $this->db->like($this->input->get('keyword'));
}

$query = $this->db->get('table_name');
foreach ($query->result() as $row) {
  echo $row->title;
}
+1

, .

, , ICriteria. () , TimeCriteria, DateCriteria, listCriteria, , IntRange ..

Criteria, - - , 3 :

  • ( )

:

  • a label
  • (in, not in, =, > , > =, <, < =, , ) - ,
  • (, , ..)

ICriteria, , , , , SQL sql- . Criteria , , (, Date , " ", ',' ',' ').

SQL- , , , Zend_Db_Select. , .

+1

, , . , CodeIgniter .

, . , , :

  • GET URL.
  • ( GET).
  • .
  • .

CodeIgniter GET, , HTTP- .

!

0

CodeIgniter, , , , , , , ( ;-). job_type, location .., .

  • .
  • .

  • - - , .

, .

P.S. , , , , + ( -)

0

. .

<?php

    // ...in Pagination class

    $acceptableVars = array('page', 'delete', 'edit', 'sessionId', 'next', 'etc.');

    foreach($_GET as $key => $value) {
        if(in_array($key, $acceptableVar)) {
            $queryStringVars[] = $key . '=' . $value;
        }
    }

    $queryString = '?' . implode('&', $queryStringVars);

    $this->nextLink = $_SEVER['filename'] . $queryString;

    ?>
0

, , . , , : = 10 , 1 0. , . MATCH . . , IN() SQL

0

Agile Toolkit allows you to add filters as follows (just for comparison with CodeIgniter, maybe you can take some concepts):

$g=$this->add('Grid');
$g->addColumn('text','name');
$g->addColumn('text','surname');
$g->setSource('user');

$conditions=array_intersect($_GET, array_flip(
  array('keyword','job_type','min_pay'));

$g->dq->where($conditions);

$ g-> dq is a dynamic query where () returns the values ​​passed from $ _GET, so it is safe to use. The rest, pagination, displaying columns, connecting to MVC depends on the structure.

0
source
    function maybeQuote($v){
        return is_numeric($v) ?: "'$v'";
    }

    function makePair($kv){
    +--  7 lines: $a = explode('=', $kv);
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    }

    function makeSql($get_string, $table){
    +-- 10 lines: $data = explode('&', $get_string);
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    }
    $test = 'lloyd=alive&age=40&weather=hot';
    $table = 'foo';
    print_r(makeSql($test, $table));
0
source

All Articles