0

Server side filtering isn't working as I expected. My idea is to trigger writing of data from database when page is loaded and every time one filter is triggered. I made a function in PHP file and it has all the filters as arguments no matter if they are actually triggered or not because I set their value to null in case specific filter isn't triggered. I made a listener for every filter and when event happens JS checks all the filters and sends data to PHP file. And it worked properly for case when there are no filters (when page is loaded) but each time I trigger any filter, PHP function enters in if selection where it should enter if there are no filters triggered and result of PHP function is an array which is not filtered. I tried to delete if selection for case when there are no filters triggered and as I expected response is null. Function just doesn't enters right if selection. I really don't know what I'm missing.

Here is PHP code:

<?php
    header("Content-type: application/json");
    if($_SERVER['REQUEST_METHOD'] == 'GET'){
        include "../config/conn.php";
        include "functions.php";
        try{
            $sizeIds = null;
            $manufacturerIds = null;
            $epochIds = null;
            $name = null;
            if(!empty($_GET['sizeIds'])){
                $sizeIds = $_GET['sizeIds'];
            }
            if(!empty($_GET['manufacturerIds'])){
                $manufacturerIds = $_GET['manufacturerIds'];
            }
            if(!empty($_GET['epochIds'])){
                $epochIds = $_GET['epochIds'];
            }
            if(!empty($_GET['name'])){
                $name = $_GET['name'];
            }
function showReports($sizeIds,$manufacturerIds,$epochIds,$name){
        global $conn;
        $response = [];
        if($sizeIds==null && $manufacturerIds==null && $epochIds==null && $name==null){
            $query = "SELECT * FROM reports;";
            $response = $conn->query($query)->fetchAll();
            return $response;
        }
        if($sizeIds!=null && $manufacturerIds==null && $epochIds==null && $name==null){
            $sizeIdsString = implode(',',$sizeIds);
            $query = "SELECT * FROM reports WHERE sizeId IN (:sizeIdsString);";
            $result = $conn->prepare($query);
            $result->bindParam(':sizeIdsString',$sizeIdsString);
            $result->execute();
            $response=$result->fetchAll();
            return $response;
        }
}

            $jsonReports = showReports($sizeIds,$manufacturerIds,$epochIds,$name);
echo json_encode(["reports"=>$jsonReports]);
            http_response_code(200);
        }
        catch(PDOException $exception){
            http_response_code(500);
        }
    }
    else{
        http_response_code(404);
    }
?>
  • 2
    It's possible and legal syntax but DONT create a function INSIDE an IF, it will regularly cause you problems and basically destroys the readability of your code – RiggsFolly May 06 '21 at 09:48
  • 1
    `WHERE sizeId IN (:sizeIdsString)` will use your `$sizeIdsString` as one single value, the equivalent of: `IN ('id1,id2,id3')` and not what you want, which is: `IN ('id1', 'id2', 'id3')`. – M. Eriksson May 06 '21 at 09:51

0 Answers0