iam searching for a way to filter content on a page, so for example iam working
on a job portal and have a list of fifty jobs, but just wanna display all jobs
in a specific city by selecting from a dropdown menu.
Is there a way that works with pagination? For me it does´t matter
if its a javascript solution or php, also i thought about to use
the search as filter. Do this makes sense?
i see that i need a controller script for it, can i have two controller files for one template?
Do i have to set something up in my blueprint to test the script?
No, you can’t have two controllers. But why would you need them?
The script as is requires certain fields (color, brand, size) to be present in your pages. But of course, you can just adapt it to filter by the fields present in your content files.
All you need is the form with the select fields in your template where you display your jobs, and the controller code in the controller for that template. Of course, you have to adapt the fields used in the controller/form to fit your filter criteria.
All variable like $color,$brandand$sizes` are defined in the controller. You don’t have to define $item as it get’s defined in the foreach loop. Please read about using foreach loop in PHP if you don’t know how they work. You need a basic understanding of PHP to be able to do this, we can’t offer that sort of support here.
Yes i understand loops, but don´t understand the script…so what i have changed:
<form id="filters" method="post">
<!-- give the select the name of the category to filter by -->
<select name="ort" onchange="this.form.submit()">
<option selected value="">Select a color</option>
<!-- let's fill the options with our colors -->
<?php foreach($ort as $item): ?>
<!-- we don't want empty items -->
<?php if(!$item) continue ?>
<!-- set the option to selected if selected -->
<option<?php e(isset($data['ort']) && $data['ort'] == $item, ' selected') ?> value="<?php echo $item ?>"><?php echo $item?></option>
<?php endforeach ?>
</select>
</form>
i replaced color with ort because i have a field ort in my blueprint. And this is my controller:
<?php
return function($site, $pages, $page) {
$ort = $page->children()->pluck('ort', null, true);
$keys = array('ort');
// 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('products', 'ort', 'data');
};
// return all children if nothing is selected
$nom_list = $page->children()->visible();
// if there is a post request, filter the projects collection
if(r::is('POST') && $data = get()) {
$nom_list = $page->children()->visible()->filter(function($child) use($keys, $data) {
anything else i have to change, does´t work for me?
<?php
return function($site, $pages, $page) {
$ort = $page->children()->pluck('ort', ',', true);
$keys = array('ort');
// return all children if nothing is selected
$nom_list = $page->children()->visible();
// if there is a post request, filter the projects collection
if(r::is('POST') && $data = get()) {
$nom_list = $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('products', 'ort', 'data');
};
and this is my template:
<?php
$nom_list = $page->children()->flip()->visible()->paginate(6);
$pagination = $nom_list->pagination();
?>
<?php snippet('header') ?>
<div class="grey_bg">
<div class="container small_pad min_h">
<div class="row"><div class="trenner"></div></div>
<form id="filters" method="post">
<!-- give the select the name of the category to filter by -->
<select name="ort" onchange="this.form.submit()">
<option selected value="">Select a color</option>
<!-- let's fill the options with our colors -->
<?php foreach($ort as $item): ?>
<!-- we don't want empty items -->
<?php if(!$item) continue ?>
<!-- set the option to selected if selected -->
<option<?php e(isset($data['ort']) && $data['ort'] == $item, ' selected') ?> value="<?php echo $item ?>"><?php echo $item?></option>
<?php endforeach ?>
</select>
</form>
<div class="row small_pad">
<?php foreach($nom_list as $proj):?>
<div class=" col-sm-12 col-md-6 col-lg-6 wow fadeIn" data-wow-delay="0.5s" data-wow-duration="1.5s">
<div class="teaser_box">
<h2><span class="white_t"><?php echo $proj->title() ?></span></h2>
<p>Einsatzort: <span class="subtitle"><?php echo $proj->ort() ?></span></p>
<p><?php echo excerpt($proj->beschreibung(), 100) ?></p>
<p><a class="link_b" href="<?php echo $proj->url() ?>">JETZT ANSEHEN<span class="arrow_icon"></span></a></p>
</div>
</div>
<?php endforeach ?>
</div>
Thank you so far, but iam really confused with this script so i updated my template and deleted the nom_list definition and add the controller to this:
<?php
return function($site, $pages, $page) {
$ort = $page->children()->pluck('ort', ',', true);
$keys = array('ort');
// return all children if nothing is selected
$nom_list = $page->children()->flip()->visible()->paginate(6);
// if there is a post request, filter the projects collection
if(r::is('POST') && $data = get()) {
$nom_list = $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('nom_list', 'ort', 'data');
};
But nothing happens, also $child confuse me, where this come from? It always showing all entries, so it seems that the filtering does´t work