<form id="filters" method="post">
<!-- give the select the name of the category to filter by -->
<select name="experience" onchange="this.form.submit()">
<option selected value="">- Experience</option>
<!-- let's fill the options with our colors -->
<?php foreach($experience as $item): ?>
<!-- we don't want empty items -->
<?php if(!$item) continue ?>
<!-- set the option to selected if selected -->
<option<?php e(isset($data['experience']) && $data['experience'] == $item, ' selected') ?> value="<?php echo $item ?>"><?php echo $item?></option>
<?php endforeach ?>
</select>
<select name="hair" onchange="this.form.submit()">
<option selected value="">- Hair</option>
<?php foreach($hair as $item): ?>
<?php if ($item == "") continue; ?>
<option<?php e(isset($data['hair']) && $data['hair'] == $item, ' selected') ?> value="<?php echo $item ?>"><?php echo $item ?></option>
<?php endforeach ?>
</select>
<select name="eyes" onchange="this.form.submit()">
<option selected value="">- Eyes</option>
<?php foreach($eyes as $item): ?>
<?php if($item == "") continue; ?>
<option<?php e(isset($data['eyes']) && $data['eyes'] == $item, ' selected') ?> value="<?php echo $item ?>"><?php echo $item ?></option>
<?php endforeach ?>
</select>
</form>
<?php
// The Alphabetise plugin for Kirby CMS will Alphabetise a given page array or tag array
// and return it for further processing/display
// The 'key' text field must be longer than a single character.
// See readme for further information
// Recent Updates: Added key for ksort sort_flags - set orderby to SORT_STRING for numbers & then letters
// Default it is set to SORT_REGLAUR for letters and then numbers
// 'orderby' key is NOT a string!
// -----
// Russ Baldwin
// shoesforindustry.net
// v0.0.9
// -----
function alphabetise($parent, $options=array()) {
// default key values
// As we are using ksort the default 'orderby' is SORT_REGULAR
// To sort with number first you can use 'orderby' set to SORT_STRING
// Other ksort sort_flags may be usuable but not tested!
$defaults = array('key'=> 'title','orderby'=> SORT_REGULAR);
// merge defaults and options
$options = array_merge($defaults, $options);
//Gets the input into a two dimensional array - uses '~' as separator;
foreach ($parent as $item){
$temp = explode('~',$item->{$options['key']}() );
$temp = $temp[0];
$temp = strtolower($temp);
$array[$temp][] = $item;
}
// Check to see array is not empty
if ( (!empty ($array)) ) {
//Make an array of key and data
foreach ($array as $temp => $item){
if (strlen($temp)<2){
$temp=$temp.$temp;
$array[substr($temp, 0, 2)][]=$item[0];
} else {
$array[substr($temp, 0, 1)][]=$item[0];
}
unset ($array[$temp]);
}
// If all OK $array will be returned and sorted
ksort($array,$options['orderby']);
} else {
// There has been a problem so set $array with error message and then return $array
$array = array(
"Alphabetise Plugin Error: Problem with array or invalid key!
Make sure your array is valid, not empty & that the key is valid for this type of array. (You can probably ignore the errors after this point, until this error has been resolved.)" => "Error"
);
}
return $array;
}
?>
<?php $alphabetise = alphabetise($projects, array('key' => 'title')); ?>
<?php foreach($alphabetise as $letter => $items): ?>
<h4><?php echo strtoupper($letter) ?></h4>
<ul>
<?php foreach($items as $item): ?>
<li>
<a href="<?php echo $item->url()?>">
<?php echo $item->title()?>
</a>
</li>
<?php endforeach ?>
</ul>
<hr/>
<?php endforeach ?>
My controller
<?php
return function($site, $pages, $page) {
$experience = $page->children()->pluck('experience', null, true);
$hair = $page->children()->pluck('hair', null, true);
$eyes = $page->children()->pluck('eyes', null, true);
$keys = array('experience', 'hair', 'eyes');
// return all children if nothing is selected
$projects = $page->children()->visible();
// if there is a post request, filter the projects collection
if(r::is('POST') && $data = get()) {
$projects = $page->children()->visible()->filter(function($child) use($keys, $data) {
// loop through the post request
foreach($data as $key => $value) {
// only act if the value is not empty and the key is valid
if($value && in_array($key, $keys)) {
// return false if the child page's category and value don't match
if(!$match = $child->$key() == $value) {
return false;
}
}
}
// otherwise return the child page
return $child;
});
}
return compact('projects', 'experience', 'hair', 'eyes', 'data');
};