Jump to content

Recommended Posts

Posted

Hi everyone, thanks in advance for your help.

I have an Event set up using a datetime field. I'm trying to grab the earliest event that has not yet passed, e.g. soonest upcoming event.

This is what I have so far.

<?php 
	$today = mktime(0,0,0,date("m"),date("d"),date("Y"));

	$events = $pages->find("template=calendar-event, sort=calendar_event");
	foreach($events as $event) {
		$date = $event->getUnformatted('calendar_event');
		if($date > $today) {
			echo $event->first()->render();
		}
	}
?>

The first() part is throwing an error but I put it here so you can see what I'm trying to do.

What would be the best way to go about doing this?

Thanks for your help.

Posted
Shouldn't that be $events->first() ?

I haven't tried your suggestion but wouldn't it get the first event based on sorting order (ascending)? I want to get that but using the condition that the date is no earlier than today, i.e. "get events sorted by date in ascending order, from that get the first event but only if the date hasn't past yet." I want to get the soonest upcoming event.

Posted
(Sorry tinacious, formatting code and quotes just doesn't work for me atm...)
 
You could do it the way you've described with a little change:
if($date > $today) {
    // now you've got what you're looking for in $event
    $firstUpcomingEvent = $event;
    // break out of the loop
    break;
}
 
 
What would be the best way to go about doing this?
 
 
While the example above would do it, I'd say the best way to go is using a single get() with the right kind of selector . This is all you need:
 
$today = mktime(0,0,0,date("m"),date("d"),date("Y"));
$firstUpcomingEvent = $pages->get("template=calendar-event, calendar_event>$today, sort=calendar_event");
Although, it's a bit confusing to me when you've got a date field called 'calendar_event' in a template called 'calendar-event'. I would definitely make a mistake with that kind of setup one day myself . I'd change the name of the field to something else, like 'calendar_event_date' or whatever that makes the selectors understandable. But that's just me, the selector does work as it is.
 
Maybe it's just a snippet and there's some html coming up in the actual template file or something, but just in case: it's a good practice to omit the closing PHP tag (at the end of the file) to prevent accidental whitespace, see http://php.net/manual/en/language.basic-syntax.phptags.php
 
(
Edit 1: I don't get it, since the forum update editing code and/or quotes just does not work. Is it just me? Using Chrome on a Mac.
Edit 2: Let's see if I got it right after second edit... Going to use plain text editor from now on.
Edit 3: Nope, not even that works anymore. Without any tags then?
)
  • Like 2
Posted

@Pete: Nope, didn't help. Even when I replaced the whole post with a plain text version (with code/quote tags with square brackets around them), divs and other html markup appeared after save. I don't think that has anything to do with js, although I haven't got a clue how this piece of software has been implemented.

It looks like others are having same kind of problems as well, see this for example: http://processwire.com/talk/topic/2616-iteration-problem-insert-element-every-nth-loop/

Posted

Browser? You're using browsers, graphical ones?

No no, I'm using a self-written TCP/IP stack and then:

>telnet processwire.com 80

Trying 207.58.138.35...
Connected to processwire.com.
Escape character is '^]'.
GET /talk/ HTTP/1.0
Host: processwire.com
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.101 Safari/537.11
 
HTTP/1.0 200 OK
...
 
No wonder you're always faster than me with your replies...
  • Like 3
Posted
I have an Event set up using a datetime field. I'm trying to grab the earliest event that has not yet passed, e.g. soonest upcoming event.
$event = $pages->get("template=calendar-event, sort=calendar_event, calendar_event>today"); 

if($event->id) {
  // you got one
}

That's repeating what nik already said. But I wanted to mention that you can just type "today" in the selector (no need for the mktime). PW runs any non-integer you put in there through PHP's strtotime(), so you can do things like "today", "yesterday", "next week", "+3 hours" etc. 

  • Like 2
Posted
That's repeating what nik already said. But I wanted to mention that you can just type "today" in the selector (no need for the mktime). PW runs any non-integer you put in there through PHP's strtotime(), so you can do things like "today", "yesterday", "next week", "+3 hours" etc. 

Wow that's good to know... very nice.

The time before ProcessWire when I had to write the sql queries myself... don't miss them ;)

Posted
(Sorry tinacious, formatting code and quotes just doesn't work for me atm...)
 
You could do it the way you've described with a little change:
if($date > $today) {
    // now you've got what you're looking for in $event
    $firstUpcomingEvent = $event;
    // break out of the loop
    break;
}

Thanks Nik, using a break within the foreach loop worked (without the $firstUpcomingEvent line). I used this code:

<?php 
$today = mktime(0,0,0,date("m"),date("d"),date("Y"));

$events = $pages->find("template=calendar-event, sort=calendar_event");
foreach($events as $event) {
	$date = $event->getUnformatted('calendar_event');
	if($date > $today) {
		echo $event->render();
		break;
	}
}
?>

I did not try your other suggestion because I tried your first one and it worked, but it does look like a more efficient, less bloated way to accomplish the same task.

Thanks for your help!  :lol:

Posted
$event = $pages->get("template=calendar-event, sort=calendar_event, calendar_event>today"); 

if($event->id) {
  // you got one
}

That's repeating what nik already said. But I wanted to mention that you can just type "today" in the selector (no need for the mktime). PW runs any non-integer you put in there through PHP's strtotime(), so you can do things like "today", "yesterday", "next week", "+3 hours" etc. 

Thanks! I didn't realize ProcessWire could do that! That's great! :lol:  

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
×
×
  • Create New...