php - Codeigniter search engine system using multi filter keyword -
i'm building own book library management scheme using codeigniter. stuck when want buil search engine using filtered keyword. have 4 tables (books, publisher, category, format) , utilize bring together retrieve data. results not excepted. below show codes:
// table construction 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 p', 'b.publisher_id=p.publisher_id', 'left'); $this->db->join('category c', 'b.cat_id=c.cat_id', 'left'); $this->db->join('format 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 b', $offset, $limit); if($getdata->num_rows() > 0) { homecoming $getdata->result(); } else { homecoming null; } } public function getpublishers() { $query = $this->db->query("select * publisher order publisher_id asc"); homecoming $query; } public function getcategories() { $query = $this->db->query("select * category order cat_id asc"); homecoming $query; } public function getformat() { $query = $this->db->query("select * format order format_id asc"); homecoming $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 seek 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() $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() $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() $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>
here except is, instance, search term "adam" , might name of person, publisher, or part of title. so, if search keyword "adam" , filtered publisher, show result such adam publishing rather "history of adam , eve" (as book title).
another question, getting wrong in code? if so, give me direction should be.
best regards
php mysql codeigniter relational-database
No comments:
Post a Comment