搜索两个以上的值

时间:2019-01-21 08:47:44

标签: php search

我有一个搜索框,目前我用于搜索基于文档编号的1个文档数据。我要搜索框超过1个文档编号。例如:如果我搜索4个文档编号223 224 225 226,它将基于4个文档编号显示4个数据

<div class="col-md-7 text-right">
    <form class="form-inline" action="<?= site_url('order/index') ?>" method="get">

        <input type="text" name="q" value="<?= $this->input->get('q') ?>" placeholder="Quick Search" class="form-control">
        <button type="submit" name="submit" class="btn btn-primary"><i class="icon-search"></i></button>
        <a href="#" class="btn btn-primary" data-toggle="modal" data-target="#modal-search">Advance Search</a>
            <a href="<?= current_url() ?>" class="btn btn-danger"><i class="icon-ccw"></i></a>
    </form>
</div>

这是我用于快速搜索的控制器

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Order extends MY_Controller {

    public $auth_only = false;

    public function __construct()
    {
        parent::__construct();
        $this->load->model('order_model', 'order');
    }

    public function index()
    {
        // get filter from user input
        $s = $this->input->get();

        if (!isset($s['action'])) {
            $this->dokmee_log();
        }

        $condition = '1=1 ';

        // quick search
        if (isset($s['q']))
        {
            $condition .= "AND (z.doc_no LIKE '%{$s['q']}%'
                OR z.description LIKE '%{$s['q']}%'
                OR z.plant_work_center LIKE '%{$s['q']}%'
                OR z.revision LIKE '%{$s['q']}%'
                OR z.functional_location LIKE '%{$s['q']}%'
                OR d.doc_category LIKE '%{$s['q']}%'
            )";
        }

结果仅出现1个文档数据,我如何搜索2个以上的文档编号?

1 个答案:

答案 0 :(得分:0)

在这里您可以通过拆分搜索关键字

来做到这一点
if (isset($s['q']))
{   
    $condition .= "AND (";
    $keywords = explode(" ",$s['q']);
    $count = count($keywords);
    $flag = 0;
    foreach($keywords as $keyword) {
        $flag++;
        if($flag == $count) {
            $condition .= "(z.doc_no LIKE '%{$s['q']}%'
            OR z.description LIKE '%{$s['q']}%'
            OR z.plant_work_center LIKE '%{$s['q']}%'
            OR z.revision LIKE '%{$s['q']}%'
            OR z.functional_location LIKE '%{$s['q']}%'
            OR d.doc_category LIKE '%{$s['q']}%'
            )";
        } else {
            $condition .= "(z.doc_no LIKE '%{$s['q']}%'
            OR z.description LIKE '%{$s['q']}%'
            OR z.plant_work_center LIKE '%{$s['q']}%'
            OR z.revision LIKE '%{$s['q']}%'
            OR z.functional_location LIKE '%{$s['q']}%'
            OR d.doc_category LIKE '%{$s['q']}%'
            ) OR";
        }
    }
    $condition .= ")";
}