Forum

Thread tagged as: Question, Discussion, Events

Listing just the next event in every category

Hi,

I want to create a listing of events that only outputs the next event from each category (number of which will change over time). I can't define filter values for a date range in the future as the events are often infrequent and want the next event from each category to be listed. Is there a way I could just output the next event from each category in a date ascending list?

Have no code yet as I don't really know where to start :-(

Any help/guidance much appreciated.

Studio Daughter

Studio Daughter 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

I think you'd need to

  1. Get the list of categories

  2. Loop through the categories and get the next single event for each

Thanks, I'll give it a whirl and see how I get on.

Ok so far I've written this (through many trial and error syntax issues!) and this code actually renders the page which is progress. However still nothing outputs.

<?php 

    $category = perch_events_category(array(
        'skip-template' => true
    ));

    foreach ($category as $categories) {

        perch_events_custom(array(
                  'filter'     => 'eventDateTime',
                  'match'      => 'gte',
                  'value'      => date('Y-m-d'),
                  'sort'       => 'eventDateTime',
                  'sort-order' => 'desc',
                  'template'   =>'events/listing/event-list-simple.html',
        ));
    }
?>

Any guidance gratefully received :)

Rachel Andrew

Rachel Andrew 394 points
Perch Support

Have you tried adding debug to the page so you can see what is going on?

https://solutions.grabaperch.com/development/how-do-i-debug-problems

I'm getting…

Fetching from cache: perch_events_categoryd41d8cd98f00b204e9800998ecf8427e
Cache file not found: perch_events_categoryd41d8cd98f00b204e9800998ecf8427e
SELECT * FROM perch2_events_categories WHERE categorySlug=NULL LIMIT 1
Array
(
    [type] => 2
    [message] => Invalid argument supplied for foreach()
    [file] => /Users/Daughter-Greg/Code/neish_v2/index.php
    [line] => 36
)

I don't know what any of this means, but it looks like it can't find the categories, or my code for the loop is wrong?

Drew McLellan

Drew McLellan 2638 points
Perch Support

The first argument to perch_events_category needs to be the category slug.

I think you probably want perch_events_categories instead.

So I've changed it to ~~~perch_events_categories();~~~ so it now looks like this (i've echo'd $category to test the output of the variable:

<?php 

    $category = perch_events_categories(array(
        'skip-template' => true,
    ));

    foreach ($category as $categories) {

        echo $category;

        perch_events_custom(array(

                        'category' => '$category',
                        'template'   =>'events/listing/event-list-simple.html',
         ));
    }
?>

This outputs:

Array
<ul>
    <li>17th - 19th November 2015</li>  
    <li>lig</li>
    <li>2nd - 4th February 2016</li>
    <li>1 July 2016</li>    
    <li>31 October 2017</li>
</ul>

Array
<ul>
    <li>17th - 19th November 2015</li>  
    <li>lig</li>
    <li>2nd - 4th February 2016</li>
    <li>1 July 2016</li>    
    <li>31 October 2017</li>
</ul>

Array
<ul>

    <li>17th - 19th November 2015</li>
    <li>2nd - 4th February 2016</li>
    <li>1 July 2016</li>    
    <li>31 October 2017</li>
</ul>

I've have 3 categories and the loop happens 3 times which is correct and every date (four of them) is being listed which is also correct. I can't seem to get the categories to then loop through. It's outputting the $category as 'Array'. Not sure why

$category = perch_events_categories(array(
    'skip-template' => true,
));

echo $category;

is outputting 'Array' as a string and not the category slugs? Do I need to add a filter to the first argument to get the category slug?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Can you change

 echo $category;

to

 print_r($category);

What does it output then?

Drew McLellan

Drew McLellan 2638 points
Perch Support

My suspicion is that this:

 'category' => '$category',

should be:

 'category' => $category['catPath'],
print_r($category);

Outputs:

Array
(
    [0] => Array
        (
            [image] => 
            [desc] => 
            [categoryID] => 4
            [categoryTitle] => Core Level Coaching
            [categorySlug] => core-level-coaching
            [categoryEventCount] => 1
            [categoryFutureEventCount] => 1
            [categoryDynamicFields] => {"image":null,"desc":{"raw":"","processed":""}}
            [_id] => 4
            [qty] => 1
            [perch_image] => 
            [perch_desc] => 
        )

    [1] => Array
        (
            [image] => 
            [desc] => 
            [categoryID] => 6
            [categoryTitle] => Leading with NLP
            [categorySlug] => leading-with-nlp
            [categoryEventCount] => 2
            [categoryFutureEventCount] => 2
            [categoryDynamicFields] => {"image":null,"desc":{"raw":"","processed":""}}
            [_id] => 6
            [qty] => 2
            [perch_image] => 
            [perch_desc] => 
        )

    [2] => Array
        (
            [image] => 
            [desc] => 
            [categoryID] => 7
            [categoryTitle] => The Language of Leadership
            [categorySlug] => the-language-of-leadership
            [categoryEventCount] => 1
            [categoryFutureEventCount] => 1
            [categoryDynamicFields] => {"image":null,"desc":{"raw":"","processed":""}}
            [_id] => 7
            [qty] => 1
            [perch_image] => 
            [perch_desc] => 
        )
)
Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, try:

'category' => $category[0]['categorySlug'],