Jump to content

Recommended Posts

Posted

I'm trying to figure out how to create a selector that will find pages where a certain text field is either empty of unique. I have about 3500 records where the majority of have a specific field which is simply empty, there some records however that will have a certain value filled in this same field and some of these values are the same. I want to only select the first page which is found with this value and exclude the rest of these pages with the same value.

Any ideas?

Posted

 

7 minutes ago, Barry said:

empty of unique

Was this meant to read empty or unique? You want to select all records whose a certain text field is empty or has unique values?

There isn't a selector option to filter pages by unique value, but you can do something like this

<?php namespace ProcessWire;

$empties = $pages->findMany("template=record, textField=''");
$fulls = $pages->findMany("template=record, textField!=''");
$uniques = new PageArray();
$fieldValues = [];
foreach ($fulls as $record) {
    if (in_array($record->textField, $fieldValues)) continue;
    $uniques->add($record);
}

$uniqueOrEmpty = $empties->import($uniques);

 

  • Thanks 1
Posted
36 minutes ago, abdus said:

Was this meant to read empty or unique?

yes, or, sorry, in Dutch or is of :)  empty OR unique

Posted

I edited this code a little and it worked perfectly! Thanks

<?php namespace ProcessWire;

$empties = $pages->findMany("template=record, textField=''");
$fulls = $pages->findMany("template=record, textField!=''");
$uniques = new PageArray();
$fieldValues = [];
foreach ($fulls as $record) {
    if (in_array($record->textField, $fieldValues)) continue;
    $uniques->add($record);
}

$uniqueOrEmpty = $empties->import($uniques);
Posted

I just noticed that I forgot to populate $fieldValues array, which effectively removes unique value check. 

You need to add $fieldValues[] = $record->textField at the end of foreach loop

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
×
×
  • Create New...