Codeigniter搜索引擎系统使用多重过滤关键字

时间:2014-06-18 14:42:03

标签: php mysql codeigniter relational-database

我正在使用Codeigniter构建自己的图书馆管理系统。当我想使用过滤关键字构建搜索引擎时,我会陷入困境。我有四个表(书籍,出版商,类别,格式),我使用连接来检索数据。但结果不是我除外。下面我展示了我的代码:

// TABLE STRUCTURE
Books |book_id, publisher_id, cat_id, format_id, title, author, ... etc|
Publisher |publisher_id, publisher, address|
Category |cat_id, category, description|
Format |format_id, format|

// MODEL
public function findBooks ( $keyword, $publisher, $category, $format, $offset, $limit )
{
    $this->db->select('*');
    $this->db->join('publisher as p', 'b.publisher_id=p.publisher_id', 'left');
    $this->db->join('category as c', 'b.cat_id=c.cat_id', 'left');
    $this->db->join('format as f', 'b.format_id=f.format_id', 'left');

    if(!empty($keyword)) {
        $this->db->like('b.title', $keyword);
        $this->db->like('p.publisher', $publisher);
        $this->db->or_like('c.category', $category);
        $this->db->or_like('f.format', $format);
    }

    $this->db->order_by('book_id', 'ASC');
    $getData = $this->db->get('books as b', $offset, $limit);

    if($getData->num_rows() > 0)
    {
        return $getData->result();
    } else {
        return NULL;
    }
}

public function getPublishers()
{
    $query = $this->db->query("SELECT * FROM publisher ORDER BY publisher_id ASC");
    return $query;
}

public function getCategories()
{
    $query = $this->db->query("SELECT * FROM category ORDER BY cat_id ASC");
    return $query;
}

public function getFormat()
{
    $query = $this->db->query("SELECT * FROM format ORDER BY format_id ASC");
    return $query;
}


// BOOKS CONTROLLER
public function find($keyword='', $offset = '', $limit = 3;)
{

    if($this->uri->segment(3) === FALSE){
        $offset = 0;
    }else{
        $offset = ($this->uri->segment(3)-1) * $limit;
    }

    $keyword = mysql_real_escape_string($this->input->post('term'));
    $publisher = $this->input->post('publisher_id');
    $category = $this->input->post('cat_id');
    $format = $this->input->post('format_id');

    $check = $this->adminModel->findBooks( $keyword, $publisher, $category, $format, $offset, $limit );

    if($check)
    {
        $data['message'] = "";
        $data['res'] = $check;
        $this->load->view('search_result', $data);

    } else {
        $data['message'] = "<div class='alert alert-warning'>No result. Please try with another keyword.</div>";
        $this->load->view('search_result', $data);
    }
}


// FORM VIEW
<div>
    <h4>Find Books</h4>
    <form action="<?php base_url(); ?>books/find" method="POST" class="form-horizontal" role="form">
        <div class="input-group input-group col-md-12">
            <label for="term" class="sr-only"></label>
            <input type="text" class="form-control" name="term" placeholder="Enter Keyword">
        </div>
        <div class="input-group col-md-12">
            <label for="publisher">By Publisher</label>
            <select name="publisher" id="publisher" class="form-control">
            <option value="0">All</option>
                <?php foreach ($pub->result() as $p) { ?>
                <option value="<?php echo $p->publisher_id; ?>"<?php echo set_select('publisher_id', $p->publisher_id, (!empty($data) && $data == $p->publisher_id ? TRUE : FALSE )); ?>><?php echo ucwords($p->publisher); ?></option>
                <?php ; } ?>
            </select>
        </div>
        <div class="input-group col-md-12">
            <label for="category">By Category</label>
            <select name="category" id="category" class="form-control">
                <option value="0">All</option>
                <?php foreach($cats->result() as $cat){ ?>
                <option value="<?php echo $cat->cat_id; ?>"<?php echo set_select('cat_id', $cat->cat_id, (!empty($data) && $data == $cat->cat_id ? TRUE : FALSE )); ?>><?php echo ucwords($cat->category); ?></option>
                <?php } ?>
            </select>
        </div>
        <div class="input-group col-md-12">
            <label for="format">By Format</label>
            <select name="format" id="format" class="form-control">
                <option value="0">All</option>
                <?php foreach ($format->result() as $frm) { ?>
                <option value="<?php echo $frm->format_id; ?>"><?php echo set_select('format', $frm->format_id, (!empty($data) && $data == $frm->format_id ? TRUE : FALSE )); ?><?php echo ucwords($frm->format); ?></option>
                <?php ; } ?>
            </select>
        </div>
        <div class="input-group col-md-12">
            <button type="submit" class="btn btn-success">FIND</button>
        </div>
    </form>
</div>

在这里,我除了是,例如,我搜索术语&#34;亚当&#34;它可能是人名,出版商或所有权的一部分。所以,如果我搜索关键字&#34; Adam&#34;并且仅由出版商过滤,它将显示诸如亚当出版的结果而不是&#34;亚当和夏娃的历史&#34; (作为书名)。

另一个问题,我的代码出错了吗?如果是这样,请指明它应该是什么。

祝你好运

0 个答案:

没有答案