Jump to content

get all unique values from a page field in all pages …


ngrmm
 Share

Recommended Posts

i have a filter and use buttons for filtering.
i want to get all selected values from a page-field (multi) in different pages (same parent). sorted by title (title of the pages)

 

<div class="controls" id="Filters">
	<h3>Size</h3>
	<button class="filter" data-filter=".size_page-title-1">38</button>
	<button class="filter" data-filter=".size_page-title-2">40</button>
	<button class="filter" data-filter=".size_page-title-3">42</button>
	<button class="filter" data-filter=".size_page-title-4">44</button>
	<button class="filter" data-filter=".size">all</button>
  </div>

 

Link to comment
Share on other sites

that's my solution

but where can i sort the array by title?

<?php 
	$children = $page->children;	
	$uniques = array();
	foreach ($children as $child) {
		$sizes = $child->size;
		$s = $sizes;
		foreach($s as $g) {
        	$uniques[$g->id] = $g->title;
		}
	}		
	foreach ($uniques as $id => $title) {
	    echo "<button class='filter' data-filter='.size_$title'>$title</button>";
	}		
	?>

 

Link to comment
Share on other sites

Better to store the selected pages in a PageArray:

$children = $page->children;
$uniques = new \ProcessWire\PageArray(); // assumes PW3
foreach($children as $child) {
    $uniques->import($child->size);
}
$uniques->sort("title");

foreach($uniques as $unique) {
    echo "<button class='filter' data-filter='.size_{$unique->title}'>{$unique->title}</button>";
}

 

  • Like 3
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...