Jump to content

Check if page exist else show ...


Roych
 Share

Recommended Posts

Hello

I'm not a coder so would appreciate some help with PHP.

I created simple even calendar and all is working great so far.

I'm using next to fetch my events. Every event is a page.

<h2>My Header for events</h2>

<?php $events = $pages->find('template=calendar-post, limit=5, Start_date|End_date>=today, sort=Start_date'); ?>
	<?php if(count($events)): ?>
		<?php foreach($events as $single): ?>
          my events code!
<?php endforeach; ?>
<?php else: ?>
		<div>There are no events at the moment!</div>
<?php endif; ?>	

But I would like to change this and hide the entire section including the <h2> if there are no events, how can I do that. Meaning that I would then remove (There are no events .... from the code). Not sure how to check if page (template=calendar-post, Start_date|End_date>=today, sort=Start_date') exists.

Hope you uderstand

Thank you

R

Link to comment
Share on other sites

Assuming I understood the question...one way to do it:

<?php namespace ProcessWire;

// init empty string to store markup output
$out = "";

$events = $pages->find('template=calendar-post, limit=5, Start_date|End_date>=today, sort=Start_date');

// we found events
if($events->count()){

    $out .="<h2>My Header for events</h2>";
    // my events code!
    // start list - just an example
    $out .= "<ul>";
    foreach($events as $single) {
        $out .= "<li>{$single->title}</li>";
    }
    // close the list
    $out .= "</ul>";
}

// use this if you need to show 'no events info'
else {
    $out .= "<div>There are no events at the moment!</div>";
}

echo $out;

Please note, I haven't checked the validity of your selector.

Edited by kongondo
  • Like 2
Link to comment
Share on other sites

1 hour ago, kongondo said:

Assuming I understood the question...one way to do it:


<?php namespace ProcessWire;

// init empty string to store markup output
$out = "";

$events = $pages->find('template=calendar-post, limit=5, Start_date|End_date>=today, sort=Start_date');

// we found events
if($events->count()){

    $out .="<h2>My Header for events</h2>";
    // my events code!
    // start list - just an example
    $out .= "<ul>";
    foreach($events as $single) {
        $out .= "<li>{$single->title}</li>";
    }
    // close the list
    $out .= "</ul>";
}

// use this if you need to show 'no events info'
else {
    $out .= "<div>There are no events at the moment!</div>";
}

echo $out;

Please note, I haven't checked the validity of your selector.

Thank you very much I already had everything just not in the right place. All I had to do was to place 

<?php $events = $pages->find('template=calendar-post, limit=5, Start_date|End_date>=today, sort=Start_date'); ?>
	<?php if(count($events)): ?>

above everything. Figured it out thanks to your example ?

Thank you

R

  • Like 1
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...