Roych

Upcoming events, how?

Recommended Posts

Roych    8

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
arjen    1,079
<?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.

  • Like 7

Share this post


Link to post
Share on other sites
Roych    8

Oh, that easy, hehe. Works great ;) 

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

R

  • Like 1

Share this post


Link to post
Share on other sites
SamC    177
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.

  • Like 1

Share this post


Link to post
Share on other sites
arjen    1,079
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.

  • Like 4

Share this post


Link to post
Share on other sites
Roych    8
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

  • Like 1

Share this post


Link to post
Share on other sites
Roych    8

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
arjen    1,079
$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 :)

  • Like 1

Share this post


Link to post
Share on other sites
Roych    8

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
arjen    1,079

Could you post the code? Not sure where the id is coming from. 

Share this post


Link to post
Share on other sites
Roych    8

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
rick    329

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
Roych    8

 

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
Robin S    2,490

@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; ?>

 

  • Like 3

Share this post


Link to post
Share on other sites
Roych    8

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
Roych    8

Hello, I need you help again with this :undecided:

Everything is working fine but I need to put another line of text like "TODAY" if a date is today. 

<?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" aria-hidden="true">  </i> <?php echo strftime("%H:%M", strtotime($single->Start_date)); ?><sup><?= $out = __("h"); ?></sup></li>
                
                  
<!--  
<div class='Danes-text'>TODAY!</div>
 -->
                  
                  
				</ul>
            </div>
        </li>
        <!-- /Event -->
    <?php endforeach; ?>
<?php else: ?>
    <div style="font-size: 14px;margin-top:-15px;color: #63b2f5;"><?= $out = __("No upcoming events at this time!"); ?></div>
<?php endif; ?>

You can see where I wan't to put in the TODAY text. Can this be done? I tried if elseif else but no luck. :(

Any ideas how can this be done?

Thank you in advance

R

Share this post


Link to post
Share on other sites
arjen    1,079

What did you try on the if?

It could be something like:

<?php if (date('d-m-Y') == date('d-m-Y', $single->Start_date) : ?>
	<div class='Danes-text'>TODAY!</div>
<?php endif; ?>

 

  • Like 1

Share this post


Link to post
Share on other sites
Roych    8

Thank you for help ;)

I tried something similar, but had no luck. I tried your code It gave error than I realised one ")" was missing. ;) But still not working,  sadly.  Nothing shows up.  Any ideas? ???

<?php if (date('d-m-Y') == date('d-m-Y', $single->Start_date)): ?>
	<div class='Danes-text'>TODAY!</div>
<?php endif; ?>

This is what I have  now ...

R

Share this post


Link to post
Share on other sites
Robin S    2,490

@Roych, if a second argument is given to date() it must be a timestamp. So you need the unformatted value of the date field:

<?php if (date('d-m-Y') == date('d-m-Y', $single->getUnformatted('Start_date'))): ?>
	<div class='Danes-text'>TODAY!</div>
<?php endif; ?>

 

  • Like 2

Share this post


Link to post
Share on other sites
Roych    8
10 hours ago, Robin S said:

@Roych, if a second argument is given to date() it must be a timestamp. So you need the unformatted value of the date field:


<?php if (date('d-m-Y') == date('d-m-Y', $single->getUnformatted('Start_date'))): ?>
	<div class='Danes-text'>TODAY!</div>
<?php endif; ?>

 

Yes, thank you this one is working perfectly ;) Good to know this, wouldn't have found that one out myself.

Thank you
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.