Jump to content

Weird selector behaviour with OR operators


Ksenia
 Share

Recommended Posts

Hello! 

I have a weird situation going on with my selector field, which I can't really get. 

I create an array of titles as filter using this logic:

firstname=Mike|Steve
id=123|124|125
title*=Red|Blue|Green

This is the field I am logging: 

1106201726_Screenshot2021-11-05at22_59_10.thumb.png.598978af419f63c2331f320a26847f51.png

It goes in a for loop till it looks like what you see on the screen.

So, my code regarding the selector string looks like this:

$output = "";
foreach ($allorganisations as $item){
                $output .= "$item | ";
                echo "<div style='color:red;'>//output String://</div>";
                echo $output;
            }
            $selector_org .= ", title=$output";
      

I also log the output just after the matched pages are selected to be sure:

$matches = $pages->find($selector_org);
echo $output;
foreach ($matches as $match) {
    echo " <li><a href='$match->url'>$match->title</a></li>";
}

But you can see that it doesn't select all four needed pages, only selects one of them. 1723172448_Screenshot2021-11-05at23_07_15.thumb.png.d46b50317721aaf0258d1a79367f44d2.png

Yet when I copy this exact selector text and manually put it in my selector, it works great... I even copy extra space and "|" and all that, it is supposed to be identical. I am very confused, what is the difference between the code? 

    foreach ($allorganisations as $item){
        $output .= "Institute for Scientific Research in Cosmic Anthropoecology | Institute for Scientific Research in Cosmic Anthropoecology | Stanford Research Institute | Institute for Scientific Research in Cosmic Anthropoecology | Stanford Research Institute | Institute of Clinical and Experimental Medicine | Institute for Scientific Research in Cosmic Anthropoecology | Stanford Research Institute | Institute of Clinical and Experimental Medicine | Cosmists | ";
        echo "<div style='color:red;'>//output String://</div>";
        echo $output;
    }
    $selector_org .= ", title=$output";
}

------>179391302_Screenshot2021-11-05at23_18_10.png.f01990191e816a86402df26062ee6ff8.png

If you see where I'm going wrong, please enlighten me. I am very puzzled with this behaviour . :--)

Best,

Ksenia

Link to comment
Share on other sites

22 minutes ago, Ksenia said:

Hello! 

I have a weird situation going on with my selector field, which I can't really get. 

I create an array of titles as filter using this logic:

firstname=Mike|Steve
id=123|124|125
title*=Red|Blue|Green

This is the field I am logging: 

1106201726_Screenshot2021-11-05at22_59_10.thumb.png.598978af419f63c2331f320a26847f51.png

It goes in a for loop till it looks like what you see on the screen.

So, my code regarding the selector string looks like this:

$output = "";
foreach ($allorganisations as $item){
                $output .= "$item | ";
                echo "<div style='color:red;'>//output String://</div>";
                echo $output;
            }
            $selector_org .= ", title=$output";
      

I also log the output just after the matched pages are selected to be sure:

$matches = $pages->find($selector_org);
echo $output;
foreach ($matches as $match) {
    echo " <li><a href='$match->url'>$match->title</a></li>";
}

But you can see that it doesn't select all four needed pages, only selects one of them. 1723172448_Screenshot2021-11-05at23_07_15.thumb.png.d46b50317721aaf0258d1a79367f44d2.png

Yet when I copy this exact selector text and manually put it in my selector, it works great... I even copy extra space and "|" and all that, it is supposed to be identical. I am very confused, what is the difference between the code? 

    foreach ($allorganisations as $item){
        $output .= "Institute for Scientific Research in Cosmic Anthropoecology | Institute for Scientific Research in Cosmic Anthropoecology | Stanford Research Institute | Institute for Scientific Research in Cosmic Anthropoecology | Stanford Research Institute | Institute of Clinical and Experimental Medicine | Institute for Scientific Research in Cosmic Anthropoecology | Stanford Research Institute | Institute of Clinical and Experimental Medicine | Cosmists | ";
        echo "<div style='color:red;'>//output String://</div>";
        echo $output;
    }
    $selector_org .= ", title=$output";
}

------>179391302_Screenshot2021-11-05at23_18_10.png.f01990191e816a86402df26062ee6ff8.png

If you see where I'm going wrong, please enlighten me. I am very puzzled with this behaviour . :--)

Best,

Ksenia

I am using IDs now to simplify it, but I really don't get it....

     foreach ($allorganisations as $item){
                $output .= "$item|";
            }
            $final_output = mb_substr($output, 0, -1);
            echo $final_output;
            if ($final_output == "1652|1652|1674") {
                echo "<div>I don't get it!!!</div>";
            }
//            $final_output = "1652|1652|1674";
            $selector_org .= ", id=$final_output";

Then I change it:

foreach ($allorganisations as $item){
    $output .= "$item|";
}
$final_output = mb_substr($output, 0, -1);
echo $final_output;
if ($final_output == "1652|1652|1674") {
    echo "<div>I don't get it!!!</div>";
}
$final_output = "1652|1652|1674";
$selector_org .= ", id=$final_output";

1479882498_Screenshot2021-11-05at23_43_43.thumb.png.06ec12cfa8338b662b6e732549ee8f96.png-------->>>>2031421539_Screenshot2021-11-05at23_45_06.thumb.png.5b4c246b5473a87654278ed9f4656279.png

 

Am I loosing my mind? ahah

Link to comment
Share on other sites

Hello, @Ksenia!

I think it happens because IDs of pages are 1652 and 1674, and `$item` variable converts to ID. When you iterate over pages, you get `$item` to be 1652, 1652 and 1674, which composes into 1652|1652|1674| and then truncates into 1652|1652|1674 when you assign `$final_output` to it. That's why your condition is true and you have "I don't get it!!!" string.

For example, if you write on homepage template file `echo $page`, it will output 1 because 1 is the ID of the homepage.

  • Like 1
Link to comment
Share on other sites

20 hours ago, Ksenia said:
foreach ($allorganisations as $item){
    $output .= "$item|";
}
$final_output = mb_substr($output, 0, -1);
echo $final_output;
if ($final_output == "1652|1652|1674") {
    echo "<div>I don't get it!!!</div>";
}
//$final_output = "1652|1652|1674";
$selector_org .= ", id=$final_output";

Since the “I don’t get it” message is displayed, $final_output is the same whether the second-to-last line is commented out or not. So if nothing else happens to $selector_org that we’re not seeing, the results should be the same as well. Above, you called the results $matches. Can you put the following line immediately after the $pages->find() call and compare the output?

var_dump($matches->getSelectors());

See the docs here: https://processwire.com/api/ref/page-array/get-selectors/

To help you debug you may also want to install the Tracy Debugger module. It can show you the selectors that were executed along with their SQL statements.

Link to comment
Share on other sites

2 hours ago, Jan Romero said:

Since the “I don’t get it” message is displayed, $final_output is the same whether the second-to-last line is commented out or not. So if nothing else happens to $selector_org that we’re not seeing, the results should be the same as well. Above, you called the results $matches. Can you put the following line immediately after the $pages->find() call and compare the output?

var_dump($matches->getSelectors());

See the docs here: https://processwire.com/api/ref/page-array/get-selectors/

To help you debug you may also want to install the Tracy Debugger module. It can show you the selectors that were executed along with their SQL statements.

Hello! 

Hm, this is very interesting! I see the bug, but can't tell how to fix it. Here is the example of picking two different Techniques (no changing of code) who both happened to have only one matched ID. 

One works and we can see that it has one Individual to loop through and selector is: 

"template=Organisation, sort=-created, id=1710"

But when it loops through two Individuals, it is messed up:

"template=Organisation, sort=-created, id=, id=1705"

502885314_Screenshot2021-11-06at22_42_49.thumb.png.a17b44afca30a9d7db2526d86c6b4b85.png809525971_Screenshot2021-11-06at22_43_26.thumb.png.cb0730d427628bd4e4252588d9908866.png

Link to comment
Share on other sites

7 minutes ago, Ksenia said:

"template=Organisation, sort=-created, id=, id=1705"

Hi. Not sure how this relates to the original issue, but this selector string can never get any results, because it is asking for a page that has both no ID (”id=“) and ID 1705 at the same time!

Link to comment
Share on other sites

4 minutes ago, Jan Romero said:

Hi. Not sure how this relates to the original issue, but this selector string can never get any results, because it is asking for a page that has both no ID (”id=“) and ID 1705 at the same time!

Yes, I know why it doesn't work, but unfortunately I do not know why it puts "id=" in front of every result ahah.

template=Organisation, sort=-created, id=1655, id=1655|1655|1705, id=1655|1655|1705|1655|1705|1661, id=1655|1655|1705|1655|1705|1661|1655|1705|1661|1648" 

Link to comment
Share on other sites

3 hours ago, Clarity said:

Hello, @Ksenia!

I think it happens because IDs of pages are 1652 and 1674, and `$item` variable converts to ID. When you iterate over pages, you get `$item` to be 1652, 1652 and 1674, which composes into 1652|1652|1674| and then truncates into 1652|1652|1674 when you assign `$final_output` to it. That's why your condition is true and you have "I don't get it!!!" string.

For example, if you write on homepage template file `echo $page`, it will output 1 because 1 is the ID of the homepage.

Hello, Clarity! 

I am not sure I understand your comment also given the bug that I see after using suggested:

var_dump($matches->getSelectors());

Is it something to do with me sanitising the array items as IDs?

Best,

Ksenia 

8 minutes ago, Jan Romero said:

Hi. Not sure how this relates to the original issue, but this selector string can never get any results, because it is asking for a page that has both no ID (”id=“) and ID 1705 at the same time!

But also thanks a lot for this line, now I at least know what to debug. :--)

Link to comment
Share on other sites

3 minutes ago, Ksenia said:

Yes, I know why it doesn't work, but unfortunately I do not know why it puts "id=" in front of every result ahah.

Wherever you append ”id=“ to the selector string must be inside a loop. You only want to append it once after all the IDs have been collected. That is to say, I believe you probably have a foreach somewhere that either needs to be removed or closed earlier. It’s not in the lines you posted here, though.

Link to comment
Share on other sites

40 minutes ago, Jan Romero said:

Hi. Not sure how this relates to the original issue, but this selector string can never get any results, because it is asking for a page that has both no ID (”id=“) and ID 1705 at the same time!

Never mind! I solved it! 

Just was looping selector in the wrong place!

foreach ($selected_techniques as $selected_technique) {
    $selector_ind .= ", relation_individual_technique.relation_individual_technique_select=$selected_technique";
    $individuals->add($pages->find($selector_ind));
    $output = "";
    foreach ($individuals as $match_ind) {
        echo " <li><a href='$match_ind->url'>$match_ind->title</a></li>";
        foreach ($match_ind->relation_individual_organisation  as $relation_individual_organisation) {
            $organisations->add($relation_individual_organisation->relation_individual_organisation_select);
            $allorganisations = $organisations->explode('id');
            foreach ($allorganisations as $item){
                $output .= "$item|";
            }
            $final_output = mb_substr($output, 0, -1);
        }
    }
    $selector_org .= ", id=$final_output";
}
4 hours ago, Clarity said:

Hello, @Ksenia!

I think it happens because IDs of pages are 1652 and 1674, and `$item` variable converts to ID. When you iterate over pages, you get `$item` to be 1652, 1652 and 1674, which composes into 1652|1652|1674| and then truncates into 1652|1652|1674 when you assign `$final_output` to it. That's why your condition is true and you have "I don't get it!!!" string.

For example, if you write on homepage template file `echo $page`, it will output 1 because 1 is the ID of the homepage.

Never mind! I solved it! 

 

  • Like 2
Link to comment
Share on other sites

7 hours ago, Ksenia said:

Hello, Clarity! 

I am not sure I understand your comment also given the bug that I see after using suggested:

var_dump($matches->getSelectors());

Is it something to do with me sanitising the array items as IDs?

Best,

Ksenia 

But also thanks a lot for this line, now I at least know what to debug. :--)

Hello, Ksenia!

On a bit unrelated note, you can use Tracy Debugger instead of vanilla `var_dump`. You can output the variables to the debugger using `bd()` function like that: `bd($matches->getSelectors())`.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...