From 9cd42e0614e6f5c384177cc6f8eae0589f30d8d9 Mon Sep 17 00:00:00 2001 From: jekkos Date: Tue, 8 Mar 2016 08:11:10 +0100 Subject: [PATCH] Add group operator to giftcard query --- application/models/Giftcard.php | 95 +++++++++++++++++---------------- 1 file changed, 49 insertions(+), 46 deletions(-) diff --git a/application/models/Giftcard.php b/application/models/Giftcard.php index 879f53cd2..3d6697047 100644 --- a/application/models/Giftcard.php +++ b/application/models/Giftcard.php @@ -187,18 +187,18 @@ class Giftcard extends CI_Model $suggestions[]=$row->giftcard_number; } - $this->db->from('customers'); - $this->db->join('people', 'customers.person_id=people.person_id', 'left'); + $this->db->from('customers'); + $this->db->join('people', 'customers.person_id=people.person_id', 'left'); $this->db->like('first_name', $this->db->escape_like_str($search)); $this->db->or_like('last_name', $this->db->escape_like_str($search)); $this->db->or_like('CONCAT(first_name, " ", last_name)', $this->db->escape_like_str($search)); - $this->db->where('deleted', 0); - $this->db->order_by('last_name', 'asc'); - $by_name = $this->db->get(); + $this->db->where('deleted', 0); + $this->db->order_by('last_name', 'asc'); + $by_name = $this->db->get(); - foreach($by_name->result() as $row) - { - $suggestions[]=$row->first_name.' '.$row->last_name; + foreach($by_name->result() as $row) + { + $suggestions[]=$row->first_name.' '.$row->last_name; } //only return $limit suggestions @@ -209,59 +209,61 @@ class Giftcard extends CI_Model return $suggestions; } - - /* - Get search suggestions to find customers - */ - function get_person_search_suggestions($search,$limit=25) - { - $suggestions = array(); - - $this->db->select('person_id'); + + /* + Get search suggestions to find customers + */ + function get_person_search_suggestions($search,$limit=25) + { + $suggestions = array(); + + $this->db->select('person_id'); $this->db->from('people'); - $this->db->like('person_id', $this->db->escape_like_str($search)); - $this->db->or_like('first_name', $this->db->escape_like_str($search)); - $this->db->or_like('last_name', $this->db->escape_like_str($search)); - $this->db->or_like('CONCAT(first_name, " ", last_name)', $this->db->escape_like_str($search)); - $this->db->or_like('email', $this->db->escape_like_str($search)); - $this->db->or_like('phone_number', $this->db->escape_like_str($search)); - $this->db->order_by('person_id', 'asc'); - $by_person_id = $this->db->get(); - - foreach($by_person_id->result() as $row) - { - $suggestions[]=$row->person_id; - } - - //only return $limit suggestions - if(count($suggestions > $limit)) - { - $suggestions = array_slice($suggestions, 0,$limit); + $this->db->like('person_id', $this->db->escape_like_str($search)); + $this->db->or_like('first_name', $this->db->escape_like_str($search)); + $this->db->or_like('last_name', $this->db->escape_like_str($search)); + $this->db->or_like('CONCAT(first_name, " ", last_name)', $this->db->escape_like_str($search)); + $this->db->or_like('email', $this->db->escape_like_str($search)); + $this->db->or_like('phone_number', $this->db->escape_like_str($search)); + $this->db->order_by('person_id', 'asc'); + $by_person_id = $this->db->get(); + + foreach($by_person_id->result() as $row) + { + $suggestions[]=$row->person_id; } - - return $suggestions; + + //only return $limit suggestions + if(count($suggestions > $limit)) + { + $suggestions = array_slice($suggestions, 0,$limit); + } + + return $suggestions; } /* Preform a search on giftcards */ function search($search, $rows = 0, $limit_from = 0) - { - $this->db->from('giftcards'); + { + $this->db->from('giftcards'); $this->db->join('people', 'giftcards.person_id=people.person_id', 'left'); - $this->db->like('first_name', $this->db->escape_like_str($search)); - $this->db->or_like('last_name', $this->db->escape_like_str($search)); - $this->db->or_like('CONCAT(first_name, " ", last_name)', $this->db->escape_like_str($search)); + $this->db->like('first_name', $this->db->escape_like_str($search)); + $this->db->or_group_start(); + $this->db->or_like('last_name', $this->db->escape_like_str($search)); + $this->db->or_like('CONCAT(first_name, " ", last_name)', $this->db->escape_like_str($search)); $this->db->or_like('giftcard_number', $this->db->escape_like_str($search)); $this->db->or_like('giftcards.person_id', $this->db->escape_like_str($search)); - $this->db->where('deleted', 0); + $this->db->group_end(); + $this->db->where('giftcards.deleted', 0); $this->db->order_by('giftcard_number', 'asc'); if ($rows > 0) { $this->db->limit($rows, $limit_from); } - + return $this->db->get(); } @@ -270,12 +272,13 @@ class Giftcard extends CI_Model $this->db->from('giftcards'); $this->db->join('people', 'giftcards.person_id=people.person_id', 'left'); $this->db->like('first_name', $this->db->escape_like_str($search)); + $this->db->or_group_start(); $this->db->or_like('last_name', $this->db->escape_like_str($search)); $this->db->or_like('CONCAT(first_name, " ", last_name)', $this->db->escape_like_str($search)); $this->db->or_like('giftcard_number', $this->db->escape_like_str($search)); $this->db->or_like('giftcards.person_id', $this->db->escape_like_str($search)); - $this->db->where('deleted', 0); - + $this->db->group_end(); + $this->db->where('giftcards.deleted', 0); return $this->db->get()->num_rows(); }