Oziris
March 16, 2017, 3:36pm
1
Hi
I would like to get a list of every dates (called “date_from”) from one specific tag (called “city”). I don’t know how to pluck them on my controller file. I just know how to pluck every tags…Do you know some trick to use ?
<?php
return function($site, $pages, $page) {
$projects = page('projects')->children()->visible();
$title = $projects->pluck('tags', ',', true);
$jourspara = param('jourspara', null);
if($jourspara) {
$projects = $projects->filterBy('tags', $jourspara, ',');
}
return compact('projects', 'tags');
};
?>
Filter the collection by tag and then loop through the collection to get the dates:
<?php
$cityProjects = page('projects')->children()->visible()->filterBy('tags', 'city');
foreach($cityProjects as $cityProject):
echo $cityProject->date('Y-m-d', 'date_from');
endforeach
}
Oziris
March 16, 2017, 6:06pm
3
I tried to integer your code, but I’m in the wrong way.
<?php
return function($site, $pages, $page) {
$cityProjects = page('projects')->children()->visible()->filterBy('tags', 'city');
$title = $cityProjects->pluck('tags', ',', false);
$jourspara = param('jourspara', null);
if($jourspara) {
$projects = $projects->filterBy('tags', $jourspara, ',');
}
return compact('projects', 'tags');
};
?>
With that, I get many “City” words as a list but it doesn’t display all dates
<?php foreach($title as $tit): ?>
<li>
<?php echo html($tit) ?>
</li>
<?php endforeach ?>
And if I do
<?php foreach($title as $tit): ?>
<li>
<?php echo html($tit)->date('Y-m-d', 'date_from'); ?>
</li>
<?php endforeach ?>
I get Fatal error: Uncaught Error: Call to a member function date() on string in …
Well, the second won’t work at all, the first doesn’t make sense either, because your $title
variable now contains all tags from the filtered collection. To get the dates from $cityProjects
you need to loop through $cityProjects
in your template and echo the data field (as in my code snippet above). Or, if you just want an array of dates, pluck the dates instead of the tags. But I don’t know your use case.
Oziris
March 16, 2017, 10:41pm
5
Thanks to you, I managed to have a list of dates from my specific tags. I would like to filter every dates from this tag to get each projects corresponding. When I click on a date, it always display every projects…
This is what I have now
<?php
return function($site, $pages, $page) {
$cityProjects = page('projects')->children()->visible()->filterBy('tags', 'city');
$date_from = $cityProjects->pluck('date_from', ',', false);
$jourspara = param('jourspara', null);
if($jourspara) {
$projects = $cityProjects->filterBy('date_from', $jourspara, ',');
}
return compact('projects', 'date_from', 'jourspara');
};
?>
with this loop
<?php foreach($date_from as $tit): ?>
<li>
<a <?php e($tit == $jourspara, 'class="active"') ?> href="<?php echo url('recherches/' . url::paramsToString(['city' => $tit])) ?>">
<?php echo strftime("%d %B %Y",strtotime($tit)); ?>
</a>
</li>
<?php endforeach ?>
Oziris
March 17, 2017, 1:21pm
6
Yes I did it ! Sorry I’m learning PHP every days…
<?php
return function($site, $pages, $page) {
$cityProjects = page('projects')->children()->visible()->filterBy('tags', 'city');
$date_from = $cityProjects->pluck('date_from', ',', true);
$jourspara = param('date_from', null);
if($jourspara) {
$projects = $cityProjects->filterBy('date_from', $jourspara, ',');
}
return compact('projects', 'jourspara', 'date_from');
};
?>
and loop
<?php foreach($date_from as $tit): ?>
<li>
<a <?php e($tit == $jourspara, 'class="active"') ?> href="<?php echo url('recherches/' . url::paramsToString(['date_from' => $tit])) ?>">
<?php echo strftime("%d %B %Y",strtotime($tit)); ?>
</a>
</li>
<?php endforeach ?>