[英]Filtering data from MYSQL database based on parameters passed with POST request
我正在為我的投資組合開發一個 PHP MVC 項目。 它是一個網絡應用程序,您可以在其中存儲聯系人(電子郵件和電話號碼)。 我無法根據具有 3 個選項的過濾器表單過濾聯系人https://ibb.co/8dJbSWd
我寫的代碼運行良好,但我相信有更好的方法來做到這一點。 這是我為解決此問題而編寫的代碼。
public function filterContacts($user_id, $group, $email, $phone){
$query = 'SELECT * FROM contacts WHERE user_id = :user_id ';
//Nothing is selected
if($group == '0' && $email == '' && $phone == ''){
$this->db->query($query . ' ORDER BY name');
$this->db->bind(':user_id', $user_id);
return $this->db->resultSet();
//Everything is selected
} else if ($group !== '0' && $email !== '' && $phone !== ''){
$query .= 'AND contact_group = :group AND email != "" AND phone_number != ""';
$this->db->query($query . 'ORDER BY name');
$this->db->bind(':user_id', $user_id);
$this->db->bind(':group', $group);
return $this->db->resultSet();
//Just group
} else if ($group !== '0' && $email == '' && $phone == ''){
$query .= 'AND contact_group = :group ';
$this->db->query($query . 'ORDER BY name');
$this->db->bind(':user_id', $user_id);
$this->db->bind(':group', $group);
return $this->db->resultSet();
//Just email
} else if ($group == '0' && $email !== '' && $phone == ''){
$query .= 'AND email != "" ';
$this->db->query($query . 'ORDER BY name');
$this->db->bind(':user_id', $user_id);
return $this->db->resultSet();
//Just phone
} else if ($group == '0' && $email == '' && $phone !== ''){
$query .= 'AND phone_number != "" ';
$this->db->query($query . 'ORDER BY name');
$this->db->bind(':user_id', $user_id);
return $this->db->resultSet();
//Group and email
} else if($group !== '0' && $email !== '' && $phone == ''){
$query .= 'AND contact_group = :group AND email != ""';
$this->db->query($query . 'ORDER BY name');
$this->db->bind(':user_id', $user_id);
$this->db->bind(':group', $group);
return $this->db->resultSet();
//Group and phone number
} else if($group !== '0' && $email == '' && $phone !== ''){
$query .= 'AND contact_group = :group AND phone_number != ""';
$this->db->query($query . 'ORDER BY name');
$this->db->bind(':user_id', $user_id);
$this->db->bind(':group', $group);
return $this->db->resultSet();
//Email and phone number
} else if($group == '0' && $email !== '' && $phone !== ''){
$query .= 'AND phone_number != "" AND email != ""';
$this->db->query($query . 'ORDER BY name');
$this->db->bind(':user_id', $user_id);
return $this->db->resultSet();
}
}
如您所見,我使用了很多 if 語句在 Contact 模型中進行不同的查詢,這是可能的,因為我只有 3 個過濾選項,但我無法想象用 10 個選項來做到這一點。
所以我想知道是否有更好的方法來解決這個問題?
如果您想查看其余代碼,這里是我的 github 存儲庫的鏈接https://github.com/Smiley-dev/Phonebook-demo
您可以使用一個類來為您完成繁重的工作,並定義設置某個變量時要執行的操作。 這將幫助您擺脫重復的代碼。 根據您問題中發布的代碼,這樣的事情應該會讓您朝着正確的方向前進。
查詢助手.php
<?php
class QueryHelper
{
function __construct($group, $email, $phone, $query, $db)
{
$this->_group = $group;
$this->_email = $email;
$this->_phone = $phone;
$this->_query = $query;
$this->_db = $db;
$this->constructQuery();
}
private function constructQuery()
{
if($this->hasGroup())
{
$this->_query .= ' AND contact_group = :group';
}
if($this->hasEmail())
{
$this->_query .= ' AND email != ""';
}
if($this->hasPhone())
{
$this->_query .= ' AND phone_number != ""';
}
}
public function UpdateBindings()
{
$this->_db->query($this->_query . ' ORDER BY name');
if($this->hasGroup())
{
$this->_db->bind(':group', $group);
}
return $this->_db;
}
private function hasGroup()
{
if ($this->_group !== '0')
{
return true;
}
else
{
return false;
}
}
private function hasEmail()
{
if ($this->_email !== '')
{
return true;
}
else
{
return false;
}
}
private function hasPhone()
{
if ($this->_phone !== '')
{
return true;
}
else
{
return false;
}
}
private $_group;
private $_email;
private $_phone;
private $_query;
private $_db;
}
和文件function filterContacts()
include 'QueryHelper.php';
public function filterContacts($group, $email, $phone){
$query = 'SELECT * FROM contacts WHERE user_id = :user_id ';
$queryHelper = new QueryHelper($group, $email, $phone, $query, $this->db);
$this->db = $queryHelper->UpdateBindings();
$this->db->bind(':user_id', $user_id);
return $this->db->resultSet();
}
您可以添加額外的錯誤檢查等,但這應該是您的起點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.