简体   繁体   English

根据 POST 请求传递的参数从 MYSQL 数据库中过滤数据

[英]Filtering data from MYSQL database based on parameters passed with POST request

I am working on a PHP MVC project for my portfolio.我正在为我的投资组合开发一个 PHP MVC 项目。 Its an web app where you can store contacts (email and phone number).它是一个网络应用程序,您可以在其中存储联系人(电子邮件和电话号码)。 I am having trouble with filtering contacts based on filter form that has 3 options https://ibb.co/8dJbSWd我无法根据具有 3 个选项的过滤器表单过滤联系人https://ibb.co/8dJbSWd

I wrote code that works fine but i am sure there is better way of doing this.我写的代码运行良好,但我相信有更好的方法来做到这一点。 Here is the code that i wrote for solving this problem.这是我为解决此问题而编写的代码。

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();
        }

    }

As you can see i used a lot of if statements to make different queries in Contact model which is possible because i have only 3 filtering options but i cannot imagine doing this with 10 options.如您所见,我使用了很多 if 语句在 Contact 模型中进行不同的查询,这是可能的,因为我只有 3 个过滤选项,但我无法想象用 10 个选项来做到这一点。

So i was wondering is there a better way of solving this?所以我想知道是否有更好的方法来解决这个问题?

Here is link to my github repository if you want to see rest of the code https://github.com/Smiley-dev/Phonebook-demo如果您想查看其余代码,这里是我的 github 存储库的链接https://github.com/Smiley-dev/Phonebook-demo

You can use a class to do the heavy lifting for you and define what to do when some variable is set.您可以使用一个类来为您完成繁重的工作,并定义设置某个变量时要执行的操作。 This will help you get rid of the repeated code.这将帮助您摆脱重复的代码。 Based off the code posted in your question something like this should get you going in the right direction.根据您问题中发布的代码,这样的事情应该会让您朝着正确的方向前进。

QueryHelper.php查询助手.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;
}

and file with function filterContacts()和文件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();
}

You can add additional error checking and such but this should be a starting point for you.您可以添加额外的错误检查等,但这应该是您的起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM