From cf215f328c18be185574bf941c580766c761729b Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 7 Sep 2014 23:23:20 +0200 Subject: [PATCH] ability to search --- htdocs/application/models/pastes.php | 39 +++++++++++++++++++++------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/htdocs/application/models/pastes.php b/htdocs/application/models/pastes.php index 6679fc6..a68aba6 100644 --- a/htdocs/application/models/pastes.php +++ b/htdocs/application/models/pastes.php @@ -427,10 +427,21 @@ class Pastes extends CI_Model $this->load->library('process'); $amount = $this->config->item('per_page'); $page = ($this->uri->segment(2) ? $this->uri->segment(2) : 0); - $this->db->select('id, title, name, created, pid, lang, raw'); - $this->db->where('private', 0); - $this->db->order_by('created', 'desc'); - $query = $this->db->get('pastes', $amount, $page); + $search = '%' . $this->input->get('search') . '%'; + + if ($search) + { + $sql = "SELECT id, title, name, created, pid, lang, raw FROM pastes WHERE private = 0 AND (title LIKE ? OR raw LIKE ?) ORDER BY created DESC LIMIT $page,$amount"; + $query = $this->db->query($sql, array( + $search, + $search, + )); + } + else + { + $sql = "SELECT id, title, name, created, pid, lang, raw FROM pastes WHERE private = 0 ORDER BY created DESC LIMIT $page,$amount"; + $query = $this->db->query($sql); + } if ($query->num_rows() > 0) { @@ -469,11 +480,21 @@ class Pastes extends CI_Model $this->load->library('pagination'); $amount = $this->config->item('per_page'); $page = ($this->uri->segment(2) ? $this->uri->segment(2) : 0); - $this->db->select('id, title, name, created, pid, lang, raw, hits'); - $this->db->where('private', 0); - $this->db->order_by('hits', 'desc'); - $this->db->order_by('created', 'desc'); - $query = $this->db->get('pastes', $amount, $page); + $search = '%' . $this->input->get('search') . '%'; + + if ($search) + { + $sql = "SELECT id, title, name, created, pid, lang, raw, hits FROM pastes WHERE private = 0 AND (title LIKE ? OR raw LIKE ?) ORDER BY hits DESC, created DESC LIMIT $page,$amount"; + $query = $this->db->query($sql, array( + $search, + $search, + )); + } + else + { + $sql = "SELECT id, title, name, created, pid, lang, raw, hits FROM pastes WHERE private = 0 ORDER BY hits DESC, created DESC LIMIT $page,$amount"; + $query = $this->db->query($sql); + } if ($query->num_rows() > 0) {