From f3d42f8b5147bb1ae264c2dd747c42fc5cf937d7 Mon Sep 17 00:00:00 2001 From: phl0 Date: Thu, 19 Feb 2026 18:30:46 +0100 Subject: [PATCH] Refactor SQL query --- application/controllers/Api.php | 2 +- application/models/Logbook_model.php | 46 +++++++++++----------------- 2 files changed, 19 insertions(+), 29 deletions(-) diff --git a/application/controllers/Api.php b/application/controllers/Api.php index 769f532b1..aeb01a353 100644 --- a/application/controllers/Api.php +++ b/application/controllers/Api.php @@ -774,7 +774,7 @@ class API extends CI_Controller { $query = $this->logbook_model->get_grids_worked_in_logbook($logbooks_locations_array, $band, $cnfm); http_response_code(201); foreach($query->result() as $line) { - $arr[] = $line->gridorcnfm; + $arr[] = $line->gridsquare; } echo json_encode($arr); diff --git a/application/models/Logbook_model.php b/application/models/Logbook_model.php index a2672794b..7aa6e133e 100644 --- a/application/models/Logbook_model.php +++ b/application/models/Logbook_model.php @@ -3344,38 +3344,28 @@ class Logbook_model extends CI_Model { $logbooks_locations_array = $StationLocationsArray; } - switch ($cnfm) { - case 'qsl': - $this->db->select('COL_QSL_RCVD as gridorcnfm'); - $this->db->group_by('COL_QSL_RCVD'); - break; - case 'lotw': - $this->db->select('COL_LOTW_QSL_RCVD as gridorcnfm'); - $this->db->group_by('COL_LOTW_QSL_RCVD'); - break; - case 'eqsl': - $this->db->select('COL_EQSL_QSL_RCVD as gridorcnfm'); - $this->db->group_by('COL_EQSL_QSL_RCVD'); - break; - default: - $this->db->select('SUBSTR(COL_GRIDSQUARE,1 ,4) as gridorcnfm'); - $this->db->group_by('gridorcnfm'); - break; - } - $this->db->order_by('gridorcnfm'); - $this->db->where_in('station_id', $logbooks_locations_array); - + $bindings = []; + $sql = 'SELECT DISTINCT UPPER(SUBSTR(COL_GRIDSQUARE, 1, 4)) AS gridsquare FROM ' . $this->config->item('table_name') . ' thcv '; + $sql .= ' WHERE COL_GRIDSQUARE <> "" AND station_id IN ('.implode(',', $logbooks_locations_array).')'; $band = ($band == 'All') ? null : $band; if ($band != null && $band != 'SAT') { - $this->db->where('COL_BAND', $band); + $sql .= ' AND COL_BAND = ? AND COL_PROP_MODE != "SAT"'; + $bindings[] = $band; } else if ($band == 'SAT') { - $this->db->where('COL_SAT_NAME !=', ''); + $sql .= ' AND COL_SAT_NAME != ""'; } - $this->db->having('gridorcnfm !=', ''); - $this->db->having('gridorcnfm is not null'); - - $query = $this->db->get($this->config->item('table_name')); - + switch ($cnfm) { + case 'qsl': + $sql .= ' AND COL_QSL_RCVD = "Y"'; + break; + case 'lotw': + $sql .= ' AND COL_LOTW_QSL_RCVD = "Y"'; + break; + case 'eqsl': + $sql .= ' AND COL_EQSL_QSL_RCVD = "Y"'; + break; + } + $query = $this->db->query($sql,$bindings); return $query; }