Jump to content

Getting only unique values from every page in a PageArray


SamC
 Share

Recommended Posts

I have a bunch of building-entry pages which have a town text field. I need to populate a select menu for a search filter but only with unique values (a few buildings may be in the same town). It's just a demo but it's really helping me learn a load of stuff. At the moment I do this:

// _search-form.php - shortened version
// addressBuildingTown is field where town is stored on building-entry template
<?php
    // get all buildings
    $buildings = $pages->find("template=building-entry");

    //get unique towns into standard array
    $towns = [];
    foreach ($buildings as $building) {
  
        if(!in_array($building->addressBuildingTown, $towns)) {
            $towns[] = $building->addressBuildingTown;
        }
    }

    $uniqueTowns = $towns;
    sort($uniqueTowns);
?>

// LIKE
// (1) Get all buildings (2) Filter by ones with unique towns (3) Use the ones left to get the addressBuildingTown field
// OR
// (1) Get all buildings with unique towns (2) Use the ones left to get the addressBuildingTown field

Is there another way of doing this? Like using the PW PageArray or something instead of a standard array? Or would this be an ok solution. Looking for some best practice advice really.

I end up with an array of town names, which some are two+ words. I can't help but feel I'd be better of with a PageArray with IDs instead (of the page the field is on), then I can output Id->addressBuildingTown when I want to print the actual name, but use the ID for all the actual filtering. If that makes sense.

Thanks for any advice.

Link to comment
Share on other sites

4 hours ago, LostKobrakai said:

<?php
$buildings = $pages->find("template=building-entry");
$towns = sort(array_unique($buildings->explode('addressBuildingTown')));

Not really without plain arrays, but still shorter and more elegant.

I tried this instead of my code and got an error:

Notice: Only variables should be passed by reference

Not sure why though.

 

--EDIT--

This seems to work though:

$uniqueTowns = array_unique($buildings->explode('addressBuildingTown'));
sort($uniqueTowns);

 

Link to comment
Share on other sites

It should work if you do this:

$buildings = $pages->find("template=building-entry");
$allTowns = $buildings->explode('addressBuildingTown');
$uniqueSortedTowns = sort(array_unique($allTowns));

IIRC this notice is new to PHP 7. Google it and you will get some explanations as to what's going on.

EDIT: Oops - not sure when you made your edit to show what works, but somehow I didn't see it :)

  • Like 2
Link to comment
Share on other sites

1 hour ago, adrian said:

It should work if you do this:

 

Yep, it does, thanks! :P

I'm still working on my buildings search site. Going ok but I'm in a situation now where I think my code needs to be re-arranged. I guess this is down to inexperience but I'll post it in a new thread, could do with some input from more experienced coders.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...