Jump to content

Get pages by datefield


Sanyaissues
 Share

Recommended Posts

Hi all,

I have some pages with a custom datefield who stores the birth of the person. I want to show a list with the birthdays of the current month but i don't now how to get all the pages that match the condition.

I know that this doesn't work but it reflex what i'm trying to achieve:

$today = getdate();
$currentmonth = $today[month]; //november
$people = $pages->get("/people/")->find("birthday~=$currentmonth"); //voila i get all the people who born on november! 

birthday is an custom datefield...

Anybody could spare me a tip? Thanks!

Link to comment
Share on other sites

This is the post which helped me understand getting pages by date. I think you can work it out!

<?php
$start = strtotime( date('Y-m-d') . " 00:00:00");
$end = strtotime( date('Y-m-d') . " 23:59:59");
$items = $pages->find("template=linkmain,publishdate>=$start,publishdate<=$end,sort=-userid");
Link to comment
Share on other sites

Thanks arjen. Now i understand how to get pages between two dates, but I could not figure out how to delimit a range of days (01 of november to 30 of november) regardless of the year.

My current solution is to get all the people and then evaluate the month using a conditional:

$items = $pages->find("template=people, sort=birthday");
$currentmonth = date("m");

foreach($items as $people){
$birth = $people->birthday;
$birthmonth = strftime("%m", strtotime($birth));
if($birthmonth == $currentmonth){
echo "{$people->title}"; //Gets the name of the person
}
}
Link to comment
Share on other sites

My apologies since I misunderstood you. The way you've done it seems fine to me. You can also create a pageArray:

$happyPeople = new PageArray();
foreach($items as $people) {
   $happyPeople->add($people);
}

Now you manipulate the array like sorting, shuffling or all other stuff using the API.

Not tested, written in browser.

  • Like 1
Link to comment
Share on other sites

I don't think it's possible with a selector to recieve pages that have one specific month (across years). Your solution is the only I can think of.

However you could write an helper function to just be able to use $pages->findPeopleBorn(9); to keep template code thin. A module with a hook $this->addHook("Pages::findPeopleBorn",$this,"findPeopleBorn"); which then has a public method findPeopleBorn($event) that returns results would be a good way to do this. See "HelloWorld.module" for example.

One way to archive it without going through all users would be to separate day,month,year into their own fields. This would be easy to then query all pages with a specific month via a simple find(selector).

Or a custom mysql sql query to recieve the id's and load the pages from that. Assuming the date field is called "birthdate":

$tmplID = $templates->get("people")->id;
$month = 8;
$query = "
   SELECT field_birthdate.data,field_birthdate.pages_id
   FROM field_birthdate LEFT JOIN pages ON (field_birthdate.pages_id=pages.id)
   WHERE (EXTRACT(MONTH FROM field_birthdate.data) = $month)
   AND pages.status < 2048 AND pages.templates_id=$tmplID
   ORDER BY pages_id";

$res = $db->query($query);
if($res->num_rows){
   $ids = array();
   while($u = $res->fetch_array()) $ids[] = $u['pages_id'];
}

$ids = implode("|",$ids);
$people = $pages->find("id=$ids");
foreach($people as $p){
   echo "<p>$p->title</p>";
}
  • Like 4
Link to comment
Share on other sites

  • 3 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...