Hello,
I’ve created a table to display a list of books: each book has several tags from different fields (publishers, authors, designers, etc.).
I want to use these filters to display books by selecting a tag. I’ve created a line to display the tags by type. I’ve tried several filter tutorials (by route, filter by multiple fields), but none of them work. Do you have any advice?
My code :
<thead>
<tr id="filters">
<?php
$filters = ['','place', 'publisher', 'year', '', '', 'categorie', '', 'authors', 'designer', 'tools', '', '', '', ''];
$tags = [];
// Pluck tags for each filter and merge them into the $tags array
foreach ($filters as $filter) {
$tags[$filter] = array_unique($page->children()->listed()->pluck($filter, ',', true));
sort($tags[$filter]); // Sort each filter's tags alphabetically
}
// Iterate over each filter and output tags in their own <td>
foreach ($filters as $filter): ?>
<td>
<ul>
<?php foreach ($tags[$filter] as $tag): ?>
<li id='<?= 'filter-'.$filter?>'><a href="<?= url('collection', ['params' => ['filter' => $tag]]) ?>">
<?= html($tag) ?></a></li>
<?php endforeach ?>
</ul>
</td>
<?php endforeach ?>
</tr>
</thead>
<tbody class="list">
<?php
$filters = get('filter');
$place = get('place');
$publisher = get('publisher');
$books = page()
->children()
->listed();
$booksPlace = $books->pluck('place', ',', true);
$booksPublisher = $books->pluck('publisher', ',', true);
$books=$books
->when($filter, function($filter){
return $this->filterBy('place', $filter);
})
->when($filter, function($filter){
return $this->filter('place', $filter);
});
foreach($books->sortBy('year', 'desc') as $book): ?>
<tr>
<td class="title"><?= $book->title()->isNotEmpty() ? $book->title() : "—" ?></td>
<?php foreach ($fields as $field_name): ?>
<td class="<?= $field_name ?>"><?= $book->$field_name()->isNotEmpty() ? $book->$field_name() : "—" ?></td>
<?php endforeach; ?>
<td class="medias">
<div class="gallery">
<?php foreach($book->images() as $file): ?>
<figure>
<img src="<?= $file->url() ?>" srcset="<?= $file->srcset() ?>">
</figure>
<?php endforeach; ?>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>

