Roych

Upcoming events, how?

16 posts in this topic

Hello all

I'm creating some simple events where every child is a new event. I have start date, content and image field assignet to them. Now I would ilke to show only an upcoming events. Only events that are still to come, so that events older than the (start date) are not shown. I tried to work with THIS but no luck as Im not a coder. :(

Im showing all events like this now:

	<?php foreach ( $pages->find('template=calendar-post, limit=8, sort=Start_date') as $single ):?>

						<div class="calendar-block">


		<a href="<?=$single->url?>"><h6 style="font-size: 14px;font-weight: 400;">
	
			<img height="180" src="<?=$single->Slikces->first->url ?>" alt="<?= $single->Slikces->description->first ?>" style="margin-right: 10px; float: left; object-fit: cover;" width="220px">

			<div class="naslovv"><b><?=$single->title?></b></div></h6>
		</a>


		<div class="ura-datum">

				<span class="ime-dneva"><?php echo strftime("%A", strtotime($single->Start_date)); ?></span> | 
				<i class="fa fa-calendar" aria-hidden="true"></i>&nbsp;&nbsp;
				<?php echo strftime("%d %B", strtotime($single->Start_date)); ?> 
				<?php echo strftime("%Y", strtotime($single->Start_date)); ?> | 
				<i class="fa fa-clock-o">&nbsp;&nbsp;</i> <?php echo strftime("%H:%M", strtotime($single->Start_date)); ?><sup>h</sup> </div>

					<div style="text-align:justify; color:inherit;padding-top:10px;padding-right:10px;"><?=$single->Content?></div><a href="<?= $single->url ?>" class="button read-more-button big button-arrow"><?= $out = __("Read more"); ?></a>
		</div>
	<? endforeach; ?>

I'd like to show upcoming events on another template (homepage) as well.

Any help greatly appreciated ;)

Thank you in advance

R

Share this post


Link to post
Share on other sites
<?php foreach ( $pages->find('template=calendar-post, limit=8, Start_date>=today, sort=Start_date') as $single ) : ?>

You can use Start_date>=today as a selector to only show items with a Start_date that is either today or in the future.

7 people like this

Share this post


Link to post
Share on other sites

Oh, that easy, hehe. Works great ;) 

Thank you very much, was struggling with that for some time before I decided to ask here. ;)

R

1 person likes this

Share this post


Link to post
Share on other sites
On 11/04/2017 at 10:10 AM, arjen said:

<?php foreach ( $pages->find('template=calendar-post, limit=8, Start_date>=today, sort=Start_date') as $single ) : ?>

You can use Start_date>=today as a selector to only show items with a Start_date that is either today or in the future.

Nice! Thanks for sharing @arjen.

1 person likes this

Share this post


Link to post
Share on other sites
15 hours ago, SamC said:

Nice! Thanks for sharing @arjen.

No problem @SamC.

Instead of "today" you can also use strings like: "next monday" or "next year" or "+1 week 2 days 4 hours 2 seconds". See strtotime function.

4 people like this

Share this post


Link to post
Share on other sites
12 minutes ago, arjen said:

Instead of "today" you can also use strings like: "next monday" or "next year" or "+1 week 2 days 4 hours 2 seconds". See strtotime function.

Good to know, thank you ;)

R

1 person likes this

Share this post


Link to post
Share on other sites

Hello,

The upper works like a charm but was wondering if it is somehow possible to add some text if there is no upcoming events. like (There is no upcoming events ...) So maybe some if else statements or something, I'm not a coder, but understand a little.

I'm using Start_date>=today option from upper posts to show upcoming events.

	<?php foreach ( $pages->find('template=calendar-post, limit=8, sort=Start_date') as $single ):?>

Thank you

R

Share this post


Link to post
Share on other sites
$events = $pages->find('template=calendar-post, limit=8, Start_date>=today, sort=Start_date');

if (count($events)) {
	echo "There are events, yes!";
} else {
	echo "Out of luck";
}

Small clue :)

1 person likes this

Share this post


Link to post
Share on other sites

It works, but if there is an upcoming event, it also shows me the id number of an event how do I get rid of it. And for multilanguage site I tried 

<?= $out = __('No events at this time.'); ?>

but it's not working. (not that important atm)

Thank you ;)

R

Share this post


Link to post
Share on other sites

Hmm... I was to fast it's not working :/ I'm doing something wrong ...

My whole original code looks like this:

<?php foreach ( $pages->find('template=calendar-post, limit=8, Start_date>=today, sort=Start_date') as $single ):?>

<!-- Event -->
<li>
<div class="date">
	<span>
		<span class="day"><?php echo strftime("%d", strtotime($single->Start_date)); ?></span>
		<span class="month"><?php echo strftime("%h", strtotime($single->Start_date)); ?></span>
		<span class="year"><?php echo strftime("%Y", strtotime($single->Start_date)); ?></span>
	</span>
</div>
<div class="event-content">
	<h6><a href="<?=$single->url?>"><?=$single->title?></a></h6>
	<ul class="event-meta">
		<li><i class="fa fa-clock-o">&nbsp;&nbsp;</i> <?php echo strftime("%H:%M", strtotime($single->Start_date)); ?><sup><?= $out = __("h"); ?></sup></li>
		<li><i class="icons icon-location"></i> 340 W 50th St.New York</li>
	</ul>
</div>
</li>
<!-- /Event -->

<? endforeach; ?>	

I tried to work with your code but I'm doing it totaly wrong I guess :/

<?= $events = $pages->find('template=calendar-post, limit=8, Start_date>=today, sort=Start_date');
if (count($events)) {
   echo '<li>',
		'<div class="date">',
		'<span>',
		'<span class="day">{echo strftime("%d", strtotime($single->Start_date))}</span>',
		'<span class="month">{ echo strftime("%h", strtotime($single->Start_date))}</span>',
		'<span class="year">{ echo strftime("%Y", strtotime($single->Start_date))}</span>',
		'</span>',
		'</div>',
			
		'<div class="event-content">',
		'<h6><a href="<?=$single->url?>"><?=$single->title?></a></h6>',
		'<ul class="event-meta">',
		'<li><i class="fa fa-clock-o">  </i> <?php echo strftime("%H:%M", strtotime($single->Start_date)); ?><sup><?= $out = __("h"); ?></sup></li>',
		'<li><i class="icons icon-location"></i> 340 W 50th St.New York</li>',
		'</ul>',
        '</div>',
		'</li>';
} else {
	echo "<span style='font-size:12px;text-align:center;'>No upcoming events at this time!</span> ";
}
?>	

If I just paste your empty code in I get Id's of articles like   1106|1107|1108 ...

 

Thank you

R

Share this post


Link to post
Share on other sites

I'll assume those are two separate code snippets...

In the second snippet, you are assigning the page array to $events, but you are referencing $single from your first snippet.

Also, these lines

'<span class="day">{echo strftime("%d", strtotime($single->Start_date))}</span>'

should not have {echo. I believe you use {} to enclose echo'd variables, and not functions. <-- Don't quote me on that. :)

There is also a mix of shorthand (<?=) and regular (<?php echo). I would use one style everywhere to make it easier to read.

Share this post


Link to post
Share on other sites

 

38 minutes ago, rick said:

'<span class="day">{echo strftime("%d", strtotime($single->Start_date))}</span>'

this should be the same as in the first code, I forgot to change it back as I was playing and trying all sorts of things. As I said I'm not a coder so I'm playing around, sorry for that :o

So the first code is the one I'm using to show the events and all is working. All I need is to show the text if there is no upcoming events. Not sure how to put arjen's code together.

thx

R

Share this post


Link to post
Share on other sites

@Roych, you might find it easier to stick with your original event markup (i.e. as primarily HTML) rather than echoing the entire event markup in PHP. You can switch in and out of PHP as much as you like, and treat PHP similar to how you would work with a template language like Twig or Smarty:

<?php $events = $pages->find('template=calendar-post, limit=8, Start_date>=today, sort=Start_date'); ?>
<?php if(count($events)): ?>
    <?php foreach($events as $single): ?>
        <!-- Event -->
        <li>
            <div class="date">
                <span>
                    <span class="day"><?php echo strftime("%d", strtotime($single->Start_date)); ?></span>
                    <span class="month"><?php echo strftime("%h", strtotime($single->Start_date)); ?></span>
                    <span class="year"><?php echo strftime("%Y", strtotime($single->Start_date)); ?></span>
                </span>
            </div>
            <div class="event-content">
                <h6><a href="<?=$single->url?>"><?=$single->title?></a></h6>
                <ul class="event-meta">
                    <li><i class="fa fa-clock-o">&nbsp;&nbsp;</i> <?php echo strftime("%H:%M", strtotime($single->Start_date)); ?><sup><?= $out = __("h"); ?></sup></li>
                    <li><i class="icons icon-location"></i> 340 W 50th St.New York</li>
                </ul>
            </div>
        </li>
        <!-- /Event -->
    <?php endforeach; ?>
<?php else: ?>
    <p>No upcoming events at this time!</p>
<?php endif; ?>

 

3 people like this

Share this post


Link to post
Share on other sites

Yes, exactly what I need ;) It's working perfectly ;) Thank you for the code, very useful. I was trying to do something like this but not realy good with PHP. :D

R

Share this post


Link to post
Share on other sites

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

  • Recently Browsing   0 members

    No registered users viewing this page.