Permalink
Browse files

Fix Search Shows Unpublished & Multi-word Searches

- Fix search returning deleted and unplublished items.
- Fix multi-word searches not returning results when they should have.

Fixes #23
Fixes #26
  • Loading branch information...
1 parent 263ddb0 commit bc0e76e530605fa7fcbdc1369d144db072bb8b91 @MitchellMcKenna committed Jun 23, 2012
Showing with 13 additions and 7 deletions.
  1. +1 −1 application/controllers/items.php
  2. +12 −6 application/models/item_model.php
@@ -60,7 +60,7 @@ function index()
function search($query = NULL)
{
if ($query) {
- $this->lifepress->get_items_page('search', $this->uri->segment(5,1), TRUE, $query);
+ $this->lifepress->get_items_page('search', $this->uri->segment(5,1), TRUE, urldecode($query));
} else {
header('Location: '.$this->config->item('base_url'));
}
@@ -175,12 +175,15 @@ function count_items_by_search($query, $public = FALSE)
$where = 'item_status != "deleted"';
}
+ // Add query wild cards and replace spaces (+ sign) with wildcards
+ $query = '%' . str_replace(' ', '%', $query) . '%';
+ $query = $this->db->escape($query);
+
return $this->db
->select('*')
->where($where)
- ->like('item_title', $this->db->escape_like_str($query))
- ->or_like('item_content', $this->db->escape_like_str($query))
- ->join('feeds', 'feeds.feed_id = items.item_feed_id', 'left outer')
+ ->where("(items.item_title LIKE " . $query . " OR items.item_content LIKE " . $query . ")")
+ ->join('feeds', 'feeds.feed_id = items.item_feed_id', 'left')
->order_by('item_date', 'desc')
->get('items')
->num_rows();
@@ -194,12 +197,15 @@ function get_items_by_search($offset = 0, $limit = 10, $query, $public = FALSE)
$where = 'item_status != "deleted"';
}
+ // Add query wild cards and replace spaces with wildcards
+ $query = '%' . str_replace(' ', '%', $query) . '%';
+ $query = $this->db->escape($query);
+
$items = $this->db
->select('*')
->where($where)
- ->like('item_title', $this->db->escape_like_str($query))
- ->or_like('item_content', $this->db->escape_like_str($query))
- ->join('feeds', 'feeds.feed_id = items.item_feed_id', 'left outer')
+ ->where("(items.item_title LIKE " . $query . " OR items.item_content LIKE " . $query . ")")
+ ->join('feeds', 'feeds.feed_id = items.item_feed_id', 'left')
->order_by('item_date', 'desc')
->limit($limit)
->offset($offset)

0 comments on commit bc0e76e

Please sign in to comment.