KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Filtering from fetched array #1831

Closed MarkoS opened this topic on on Jan 9, 2021 - 6 comments

MarkoS commented on Jan 9, 2021

Hi all,

I am making filter and having problem with Filter used with prefetched array of possible options. I have mysql table with JSON stored value (list of ID-s)

{"0":"3656","1":"2747","2":"4636","3":"4908","4":"3108"}

Than I do fetch of data using this:

       $this->src('automaker')
   ->query("SELECT odobreni_proizvodjaci AS odoProizvodjaci FROM fuel_users WHERE id = :korisnik")
            ->params(array(
                ":korisnik"=>$this->params["korisnik"]
            ))
            ->pipe($this->dataStore("tempproizvodjaci"))
            ->requestDataSending();
            $odoProizvodjaci = $this->dataStore("tempproizvodjaci")->data()[0]["odoProizvodjaci"];
            
            $proizvodjaci = json_decode($odoProizvodjaci, true); 
            $privremenArr = array();
            
            foreach ($proizvodjaci as $value) {
              array_push($privremenArr, intval($value));
            }
            
            // $privremenArr = array(1720,3281,3595);

And at the end I am using filter like so:

          ->pipe(new Filter(array(
                    array("Proizvodjac","in",$privremenArr),                    
                )))

The thing is that this does not work. If I uncomment // $privremenArr = array(1720,3281,3595); it does work. I have tried different options and still no luck.

For the test only, I've added code below in the view directly and it works as supposed to (not what I want but shows data)

$odoProizvodjaci = $this->dataStore("tempproizvodjaci")->data()[0]["odoProizvodjaci"];
            
            //$proizvodjaci = array(1720,3281,3595);
            $proizvodjaci = json_decode($odoProizvodjaci, true); 
             
            $privremenArr = array();
            
            
           echo  '<select id="proizv" name="proizv" class="form-control">';
    

            foreach ($proizvodjaci as $value) {
              //array_push($privremenArr, intval($value)+1);
              echo '<option value="sve">'.$value.'</option>';
            }
            
            //print_r($privremenArr[1]);
        echo '</select>';

Would anyone know how to get this working?

KoolReport commented on Jan 9, 2021

It seems that the $proizvodjaci contains correct data so may I see how do you use this variable. Could you please post the whole code above the [piping to Filter] section.

MarkoS commented on Jan 9, 2021

Here is the part with pipe filtering:

             $this->src('automaker')
                    ->query($sumirana_prodaja['proizvodjaci'])
                    ->params(array(
                        ":korisnik"=>$dodDobavljac
                ))
                ->pipe(new Filter(array(
                    array("Proizvodjac","in",$privremenArr),
                    
                )))
                ->pipe($this->dataStore("data_proizvodjaci"));

It can take static array of integers and filter over query generated data.

$privremenArr = array(1720,3281,3595);

In the db table I can have use two types of data (below) and I tried them both and non of them worked. I've tried converting from string to int but that did not help. print_r does print array and echo in foreach prints single value. All that works fine, but in filter does not.


//1st
{"0":"3656","1":"2747","2":"4636","3":"4908","4":"3108"}

//2nd
["3656","2747"]

KoolReport commented on Jan 9, 2021

So the result you get is empty data_proizvodjaci datastore. Could you please check the query if you have the field "Proizvodjac". Another way you can test is to run the query directly in phpadmin and check if you "Proizvodjac" has value like 1720,3281,3595.

MarkoS commented on Jan 9, 2021

It returns data, it's query is okey.

Here is a test which returns data in "data_proizvodjaci" datastore:

    $privremenArr = array(1720,3281,3595);

    $this->src('automaker')// ## Izlistaj sve proizvođače koje dobavljač radi
                    ->query($sumirana_prodaja['proizvodjaci'])
                    ->params(array(
                        ":korisnik"=>$dodDobavljac
                ))
                ->pipe(new Filter(array(
                    array("Proizvodjac","in",$privremenArr),
                    
                )))
                ->pipe($this->dataStore("data_proizvodjaci"));

I've set static array and It will return data as expected with matching values.

Another test I did is echo content of odoProizvodjaci datastore directly in report's view:

$odoProizvodjaci = $this->dataStore("tempproizvodjaci")->data()[0]["odoProizvodjaci"];

And result in the View is: {"0":"3656","1":"2747","2":"4636","3":"4908","4":"3108"} Meaning it contains data and it can be processed (json_decode) into new array

In addition to that I run this in View:

$odoProizvodjaci = $this->dataStore("tempproizvodjaci")->data()[0]["odoProizvodjaci"];
$proizvodjaci = json_decode($odoProizvodjaci, true); 

$privremenArr = array();
foreach ($proizvodjaci as $value) {
         array_push($privremenArr, intval($value)); 
            }
            
print_r($privremenArr);

Which as output shows: Array ( [0] => 3656 [1] => 2747 [2] => 4636 [3] => 4908 [4] => 3108 )

I am just wondering why same array $privremenArr does not work inside Filter function.

MarkoS commented on Jan 9, 2021

UPDATE: I've actually figured out why it does not work. I did not have match in saved ID-s where filter is called. Once I added "(1720,3281,3595)" id-s into db it started working. What a epic mistake :D Well this code could help someone else so it's good to have it here :)

Thanks again!

KoolReport commented on Jan 9, 2021

That's awesome to find out :D

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
solved

None