Skip to main content
deleted 5 characters in body
Source Link

At the end I wrote something like this

Controler method

public function filter(){

        if ($_SERVER['REQUEST_METHOD'] == 'POST'){

            $filters = $_POST;

            $contacts = $this->contactModel->getContacts($_SESSION['user_id'], $filters);

            $data = [
                'contacts' => $contacts
            ];

            $this->view('contacts/table', $data);
        }

    }

Model method

public function getContacts($user_id, $filterBy = []){


        $query = 'SELECT * FROM contacts WHERE user_id = :user_id ';
        $bindings = [':user_id' => $user_id];

        if(!empty($filterBy)){

            if($filterBy['group'] !== '0'){
                $query .= 'AND contact_group = :group ';
                $bindings[':group'] = $filterBy['group'];
            }
            if($filterBy['email'] !== 'false'){
                $query .= 'AND email != "" ';
            }
            if($filterBy['phone'] !== 'false'){
                $query .= 'AND phone_number != "" ';
            }

            if($filterBy['search'] !== ''){
                $query .= 'AND name LIKE :search ';
                $bindings[':search'] = '%' . $filterBy['search'] . '%';
            }
        }

        $this->db->query($query);
        foreach ($bindings as $key => $value){
            $this->db->bind($key, $value);
        }

        return $this->db->resultSet();

    }

It works with fine and it seems good enough for me. Thanks a lot for your help

At the end I wrote something like this

Controler method

public function filter(){

        if ($_SERVER['REQUEST_METHOD'] == 'POST'){

            $filters = $_POST;

            $contacts = $this->contactModel->getContacts($_SESSION['user_id'], $filters);

            $data = [
                'contacts' => $contacts
            ];

            $this->view('contacts/table', $data);
        }

    }

Model method

public function getContacts($user_id, $filterBy = []){


        $query = 'SELECT * FROM contacts WHERE user_id = :user_id ';
        $bindings = [':user_id' => $user_id];

        if(!empty($filterBy)){

            if($filterBy['group'] !== '0'){
                $query .= 'AND contact_group = :group ';
                $bindings[':group'] = $filterBy['group'];
            }
            if($filterBy['email'] !== 'false'){
                $query .= 'AND email != "" ';
            }
            if($filterBy['phone'] !== 'false'){
                $query .= 'AND phone_number != "" ';
            }

            if($filterBy['search'] !== ''){
                $query .= 'AND name LIKE :search ';
                $bindings[':search'] = '%' . $filterBy['search'] . '%';
            }
        }

        $this->db->query($query);
        foreach ($bindings as $key => $value){
            $this->db->bind($key, $value);
        }

        return $this->db->resultSet();

    }

It works with fine and it seems good enough for me. Thanks a lot for your help

At the end I wrote something like this

Controler method

public function filter(){

        if ($_SERVER['REQUEST_METHOD'] == 'POST'){

            $filters = $_POST;

            $contacts = $this->contactModel->getContacts($_SESSION['user_id'], $filters);

            $data = [
                'contacts' => $contacts
            ];

            $this->view('contacts/table', $data);
        }

    }

Model method

public function getContacts($user_id, $filterBy = []){


        $query = 'SELECT * FROM contacts WHERE user_id = :user_id ';
        $bindings = [':user_id' => $user_id];

        if(!empty($filterBy)){

            if($filterBy['group'] !== '0'){
                $query .= 'AND contact_group = :group ';
                $bindings[':group'] = $filterBy['group'];
            }
            if($filterBy['email'] !== 'false'){
                $query .= 'AND email != "" ';
            }
            if($filterBy['phone'] !== 'false'){
                $query .= 'AND phone_number != "" ';
            }

            if($filterBy['search'] !== ''){
                $query .= 'AND name LIKE :search ';
                $bindings[':search'] = '%' . $filterBy['search'] . '%';
            }
        }

        $this->db->query($query);
        foreach ($bindings as $key => $value){
            $this->db->bind($key, $value);
        }

        return $this->db->resultSet();

    }

It works fine and it seems good enough for me. Thanks a lot for your help

Source Link

At the end I wrote something like this

Controler method

public function filter(){

        if ($_SERVER['REQUEST_METHOD'] == 'POST'){

            $filters = $_POST;

            $contacts = $this->contactModel->getContacts($_SESSION['user_id'], $filters);

            $data = [
                'contacts' => $contacts
            ];

            $this->view('contacts/table', $data);
        }

    }

Model method

public function getContacts($user_id, $filterBy = []){


        $query = 'SELECT * FROM contacts WHERE user_id = :user_id ';
        $bindings = [':user_id' => $user_id];

        if(!empty($filterBy)){

            if($filterBy['group'] !== '0'){
                $query .= 'AND contact_group = :group ';
                $bindings[':group'] = $filterBy['group'];
            }
            if($filterBy['email'] !== 'false'){
                $query .= 'AND email != "" ';
            }
            if($filterBy['phone'] !== 'false'){
                $query .= 'AND phone_number != "" ';
            }

            if($filterBy['search'] !== ''){
                $query .= 'AND name LIKE :search ';
                $bindings[':search'] = '%' . $filterBy['search'] . '%';
            }
        }

        $this->db->query($query);
        foreach ($bindings as $key => $value){
            $this->db->bind($key, $value);
        }

        return $this->db->resultSet();

    }

It works with fine and it seems good enough for me. Thanks a lot for your help