Sorting with filters

If i select a combination that is not possible, im getting a error.
How can i solve this?

Cheers, Maarten

Examples that work

<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');
};

What do you get if you var_dump() $items before you use it in line 77?

When no selection is made im getting this result.

C

array(1) { [0]=> object(Page)#192 (30) { [“title”]=> string(6) “Celine” [“id”]=> string(13) “models/celine” [“uid”]=> string(6) “celine” [“slug”]=> string(6) “celine” [“parent”]=> string(6) “models” [“uri”]=> string(13) “models/celine” [“url”]=> string(35) “http://localhost:8888/models/celine” [“contentUrl”]=> string(48) “http://localhost:8888/content/13-models/1-celine” [“tinyUrl”]=> string(30) “http://localhost:8888/x/j75dj4” [“root”]=> string(65) “/Volumes/TO EDIT/Dropbox/Website/ionic/content/13-models/1-celine” [“dirname”]=> string(8) “1-celine” [“diruri”]=> string(18) “13-models/1-celine” [“depth”]=> int(2) [“num”]=> string(1) “1” [“hash”]=> string(6) “j75dj4” [“modified”]=> string(25) “2016-11-24T08:33:05+00:00” [“template”]=> string(6) “models” [“intendedTemplate”]=> string(6) “models” [“isVisible”]=> bool(true) [“isOpen”]=> bool(false) [“isActive”]=> bool(false) [“isHomePage”]=> bool(false) [“isErrorPage”]=> bool(false) [“isCachable”]=> bool(true) [“isWritable”]=> bool(true) [“content”]=> object(Content)#193 (2) { [“root”]=> string(79) “/Volumes/TO EDIT/Dropbox/Website/ionic/content/13-models/1-celine/models.nl.txt” [“fields”]=> array(9) { [“sectionmeta”]=> string(0) “” [“title”]=> string(6) “Celine” [“seotitle”]=> string(0) “” [“description”]=> string(0) “” [“featuredimage”]=> string(0) “” [“sectionmodel”]=> string(0) “” [“experience”]=> string(4) “main” [“hair”]=> string(5) “brown” [“eyes”]=> string(5) “green” } } [“headers”]=> NULL [“children”]=> object(Children)#270 (0) { } [“siblings”]=> object(Children)#266 (4) { [0]=> string(10) “models/eva” [1]=> string(12) “models/fleur” [2]=> string(12) “models/sanne” [3]=> string(12) “models/evana” } [“files”]=> object(Files)#268 (0) { } } }
Celine
E

array(2) { [0]=> object(Page)#203 (30) { [“title”]=> string(3) “Eva” [“id”]=> string(10) “models/eva” [“uid”]=> string(3) “eva” [“slug”]=> string(3) “eva” [“parent”]=> string(6) “models” [“uri”]=> string(10) “models/eva” [“url”]=> string(32) “http://localhost:8888/models/eva” [“contentUrl”]=> string(45) “http://localhost:8888/content/13-models/2-eva” [“tinyUrl”]=> string(31) “http://localhost:8888/x/1yelm1v” [“root”]=> string(62) “/Volumes/TO EDIT/Dropbox/Website/ionic/content/13-models/2-eva” [“dirname”]=> string(5) “2-eva” [“diruri”]=> string(15) “13-models/2-eva” [“depth”]=> int(2) [“num”]=> string(1) “2” [“hash”]=> string(7) “1yelm1v” [“modified”]=> string(25) “2016-11-24T08:33:23+00:00” [“template”]=> string(6) “models” [“intendedTemplate”]=> string(6) “models” [“isVisible”]=> bool(true) [“isOpen”]=> bool(false) [“isActive”]=> bool(false) [“isHomePage”]=> bool(false) [“isErrorPage”]=> bool(false) [“isCachable”]=> bool(true) [“isWritable”]=> bool(true) [“content”]=> object(Content)#204 (2) { [“root”]=> string(76) “/Volumes/TO EDIT/Dropbox/Website/ionic/content/13-models/2-eva/models.nl.txt” [“fields”]=> array(9) { [“sectionmeta”]=> string(0) “” [“title”]=> string(3) “Eva” [“seotitle”]=> string(0) “” [“description”]=> string(0) “” [“featuredimage”]=> string(0) “” [“sectionmodel”]=> string(0) “” [“experience”]=> string(4) “main” [“hair”]=> string(5) “brown” [“eyes”]=> string(5) “brown” } } [“headers”]=> NULL [“children”]=> object(Children)#269 (0) { } [“siblings”]=> object(Children)#266 (4) { [0]=> string(13) “models/celine” [1]=> string(12) “models/fleur” [2]=> string(12) “models/sanne” [3]=> string(12) “models/evana” } [“files”]=> object(Files)#272 (0) { } } [1]=> object(Page)#236 (30) { [“title”]=> string(5) “Evana” [“id”]=> string(12) “models/evana” [“uid”]=> string(5) “evana” [“slug”]=> string(5) “evana” [“parent”]=> string(6) “models” [“uri”]=> string(12) “models/evana” [“url”]=> string(34) “http://localhost:8888/models/evana” [“contentUrl”]=> string(47) “http://localhost:8888/content/13-models/5-evana” [“tinyUrl”]=> string(31) “http://localhost:8888/x/1xt7jqy” [“root”]=> string(64) “/Volumes/TO EDIT/Dropbox/Website/ionic/content/13-models/5-evana” [“dirname”]=> string(7) “5-evana” [“diruri”]=> string(17) “13-models/5-evana” [“depth”]=> int(2) [“num”]=> string(1) “5” [“hash”]=> string(7) “1xt7jqy” [“modified”]=> string(25) “2016-11-24T09:16:19+00:00” [“template”]=> string(6) “models” [“intendedTemplate”]=> string(6) “models” [“isVisible”]=> bool(true) [“isOpen”]=> bool(false) [“isActive”]=> bool(false) [“isHomePage”]=> bool(false) [“isErrorPage”]=> bool(false) [“isCachable”]=> bool(true) [“isWritable”]=> bool(true) [“content”]=> object(Content)#237 (2) { [“root”]=> string(78) “/Volumes/TO EDIT/Dropbox/Website/ionic/content/13-models/5-evana/models.nl.txt” [“fields”]=> array(9) { [“sectionmeta”]=> string(0) “” [“title”]=> string(5) “Evana” [“seotitle”]=> string(0) “” [“description”]=> string(0) “” [“featuredimage”]=> string(0) “” [“sectionmodel”]=> string(0) “” [“experience”]=> string(4) “main” [“hair”]=> string(6) “blonde” [“eyes”]=> string(4) “blue” } } [“headers”]=> NULL [“children”]=> object(Children)#273 (0) { } [“siblings”]=> object(Children)#266 (4) { [0]=> string(13) “models/celine” [1]=> string(10) “models/eva” [2]=> string(12) “models/fleur” [3]=> string(12) “models/sanne” } [“files”]=> object(Files)#274 (0) { } } }
Eva
Evana
F

array(1) { [0]=> object(Page)#214 (30) { [“title”]=> string(5) “Fleur” [“id”]=> string(12) “models/fleur” [“uid”]=> string(5) “fleur” [“slug”]=> string(5) “fleur” [“parent”]=> string(6) “models” [“uri”]=> string(12) “models/fleur” [“url”]=> string(34) “http://localhost:8888/models/fleur” [“contentUrl”]=> string(47) “http://localhost:8888/content/13-models/3-fleur” [“tinyUrl”]=> string(31) “http://localhost:8888/x/1axb9qh” [“root”]=> string(64) “/Volumes/TO EDIT/Dropbox/Website/ionic/content/13-models/3-fleur” [“dirname”]=> string(7) “3-fleur” [“diruri”]=> string(17) “13-models/3-fleur” [“depth”]=> int(2) [“num”]=> string(1) “3” [“hash”]=> string(7) “1axb9qh” [“modified”]=> string(25) “2016-11-24T08:29:51+00:00” [“template”]=> string(6) “models” [“intendedTemplate”]=> string(6) “models” [“isVisible”]=> bool(true) [“isOpen”]=> bool(false) [“isActive”]=> bool(false) [“isHomePage”]=> bool(false) [“isErrorPage”]=> bool(false) [“isCachable”]=> bool(true) [“isWritable”]=> bool(true) [“content”]=> object(Content)#215 (2) { [“root”]=> string(78) “/Volumes/TO EDIT/Dropbox/Website/ionic/content/13-models/3-fleur/models.nl.txt” [“fields”]=> array(9) { [“sectionmeta”]=> string(0) “” [“title”]=> string(5) “Fleur” [“seotitle”]=> string(0) “” [“description”]=> string(0) “” [“featuredimage”]=> string(0) “” [“sectionmodel”]=> string(0) “” [“experience”]=> string(11) “development” [“hair”]=> string(5) “brown” [“eyes”]=> string(4) “blue” } } [“headers”]=> NULL [“children”]=> object(Children)#275 (0) { } [“siblings”]=> object(Children)#266 (4) { [0]=> string(13) “models/celine” [1]=> string(10) “models/eva” [2]=> string(12) “models/sanne” [3]=> string(12) “models/evana” } [“files”]=> object(Files)#276 (0) { } } }
Fleur
S

array(1) { [0]=> object(Page)#225 (30) { [“title”]=> string(5) “Sanne” [“id”]=> string(12) “models/sanne” [“uid”]=> string(5) “sanne” [“slug”]=> string(5) “sanne” [“parent”]=> string(6) “models” [“uri”]=> string(12) “models/sanne” [“url”]=> string(34) “http://localhost:8888/models/sanne” [“contentUrl”]=> string(47) “http://localhost:8888/content/13-models/4-sanne” [“tinyUrl”]=> string(31) “http://localhost:8888/x/1nbar2y” [“root”]=> string(64) “/Volumes/TO EDIT/Dropbox/Website/ionic/content/13-models/4-sanne” [“dirname”]=> string(7) “4-sanne” [“diruri”]=> string(17) “13-models/4-sanne” [“depth”]=> int(2) [“num”]=> string(1) “4” [“hash”]=> string(7) “1nbar2y” [“modified”]=> string(25) “2016-11-24T08:30:50+00:00” [“template”]=> string(6) “models” [“intendedTemplate”]=> string(6) “models” [“isVisible”]=> bool(true) [“isOpen”]=> bool(false) [“isActive”]=> bool(false) [“isHomePage”]=> bool(false) [“isErrorPage”]=> bool(false) [“isCachable”]=> bool(true) [“isWritable”]=> bool(true) [“content”]=> object(Content)#226 (2) { [“root”]=> string(78) “/Volumes/TO EDIT/Dropbox/Website/ionic/content/13-models/4-sanne/models.nl.txt” [“fields”]=> array(9) { [“sectionmeta”]=> string(0) “” [“title”]=> string(5) “Sanne” [“seotitle”]=> string(0) “” [“description”]=> string(0) “” [“featuredimage”]=> string(0) “” [“sectionmodel”]=> string(0) “” [“experience”]=> string(3) “new” [“hair”]=> string(6) “blonde” [“eyes”]=> string(4) “blue” } } [“headers”]=> NULL [“children”]=> object(Children)#277 (0) { } [“siblings”]=> object(Children)#266 (4) { [0]=> string(13) “models/celine” [1]=> string(10) “models/eva” [2]=> string(12) “models/fleur” [3]=> string(12) “models/evana” } [“files”]=> object(Files)#278 (0) { } } }
Sanne

Solved!

After i made this test i’ve found the solution :slight_smile:

<?php
$foo = $projects;



echo "Length: " . strlen($foo) . "<br>";



if (strlen($foo) === 0) echo 'Null length is Zero <br>';
?>    

<hr/>

<?php if (strlen($foo) == 0 && !is_null($foo)) echo 'Truly an empty string <br>';
else echo 'Not empty';
?>    

<hr/>
    
<?php if (strlen($foo) == 0 && isset($foo)) echo 'Truly an empty string <br>';
else echo 'Not empty';
?>    

<hr/>

<?php var_dump($foo) ?> 

Solution

<?php  $alphabetise = alphabetise($projects, array('key' => 'title')); ?>

<?php if (strlen($projects) != 0 && isset($projects)): ?>
  
    
<?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 ?>
<?php else: ?> 
<h4>No results</h4>    
<?php endif; ?>