PHP group array by date
Permalink
Hi,
I'm struggling a bit with the coding-side of things and wondering if anyone can help? Very basic, I'm sure for anyone used to working in PHP.
I'm using the ProEvents add-on. Have dates against my events and displaying these as an Event List on my page, however, each date repeats. For example I have a few events running on the same day eg 21 March, some on the 22nd, 24th and so on. I want to only show the date once, and then have the event and it's time listed.
Eg
21 March
Event x - 12.30 PM
Event y - 1.00 PM
Event z - 2.30 PM
22 March
Event y - 1.00 PM
Event z - 2.30 PM
Event a - 4.00 PM
24 March
Event a - 8.00 AM
Event z - 1.45 PM
I essentially want to group events that fall on the same day; and then display them in chronological order (by time).
Here is the code I have for displaying the events:
I know I have to insert a foreach () somewhere where the date array is created, but not sure where exactly?
I'm also not sure what to put inside this function to group and sort and can't find an example on the web anywhere of this, well that makes sense anyway?
Any help or advice is greatly appreciated.
Cheers,
Choppie
I'm struggling a bit with the coding-side of things and wondering if anyone can help? Very basic, I'm sure for anyone used to working in PHP.
I'm using the ProEvents add-on. Have dates against my events and displaying these as an Event List on my page, however, each date repeats. For example I have a few events running on the same day eg 21 March, some on the 22nd, 24th and so on. I want to only show the date once, and then have the event and it's time listed.
Eg
21 March
Event x - 12.30 PM
Event y - 1.00 PM
Event z - 2.30 PM
22 March
Event y - 1.00 PM
Event z - 2.30 PM
Event a - 4.00 PM
24 March
Event a - 8.00 AM
Event z - 1.45 PM
I essentially want to group events that fall on the same day; and then display them in chronological order (by time).
Here is the code I have for displaying the events:
<div class="smallcal"> <div class="calwrap"> <!--<div class="img">--> <div class="day"><h4> <?php echo date('d', strtotime($date)); ?> </h4></div> <div class="month"><h4> <?php $date_month = date('M', strtotime($date)); echo $months[$date_month]; ?> </h4></div> <!--</div>--> </div><br /> <div class="infowrap">
Viewing 15 lines of 55 lines. View entire code block.
I know I have to insert a foreach () somewhere where the date array is created, but not sure where exactly?
I'm also not sure what to put inside this function to group and sort and can't find an example on the web anywhere of this, well that makes sense anyway?
Any help or advice is greatly appreciated.
Cheers,
Choppie
I do not have a current copy of ProEvents anywhere but I have used it previously. Could you attach the full view.php file (renamed to view.php.txt for attachment purposes) for this block? I think the snippet of code you provided just isn't enough to help you out. This should all be able to be done in the block view.
Hi Hutman,
Sure, here it is. This is the view file under /proevents/blocks/pro_events_list/templates/list_this_week.
The second file attached, is the view file under /proevents/blocks/pro_events_list
I wasn't sure which one you were after?
Thanks in advance!
Cheers,
Choppie
Sure, here it is. This is the view file under /proevents/blocks/pro_events_list/templates/list_this_week.
The second file attached, is the view file under /proevents/blocks/pro_events_list
I wasn't sure which one you were after?
Thanks in advance!
Cheers,
Choppie
you shouldn't have to do much with the query. You should be able to just iterate through the list and group by day. simpler to the attached example of the metro view that is grouped by month.
Hi RadiantWeb,
Thanks for the excellent advice! Who better to give advice than the awesome people who created these add-ons!
I figured out that there is a bit of code in the template you mentioned which groups the events by month:
When changing the 'M' to anything else, nothing display. Also, the $months variable probably needs changing but I'm not sure what to change this too? I think if I where to change it to the right variable, the groupings would be by day and not month and it would work correctly?
Thanks in advance,
Choppie
Thanks for the excellent advice! Who better to give advice than the awesome people who created these add-ons!
I figured out that there is a bit of code in the template you mentioned which groups the events by month:
When changing the 'M' to anything else, nothing display. Also, the $months variable probably needs changing but I'm not sure what to change this too? I think if I where to change it to the right variable, the groupings would be by day and not month and it would work correctly?
Thanks in advance,
Choppie
Hi all,
Okay, hang on. I've got dates grouping correctly by day now, and not by month by removing the $month variable
Now I just have to figure out how to get the actual date to display instead of "Array" as seen below. Any idea how to do this? Changing 'M' to 'd' in the first statement is obviously not the way to go.
It currently displays as:
Array
12.45 AM Event A
Array
1:00 PM Event B
Array
1:00 PM Event B
1:30 PM Event C
Also, I noticed that when I select AM for my Event Dates attribute, it displays as PM in the list block and vice versa. Is this a known bug?
Cheers,
Choppie
Okay, hang on. I've got dates grouping correctly by day now, and not by month by removing the $month variable
Now I just have to figure out how to get the actual date to display instead of "Array" as seen below. Any idea how to do this? Changing 'M' to 'd' in the first statement is obviously not the way to go.
It currently displays as:
Array
12.45 AM Event A
Array
1:00 PM Event B
Array
1:00 PM Event B
1:30 PM Event C
Also, I noticed that when I select AM for my Event Dates attribute, it displays as PM in the list block and vice versa. Is this a known bug?
Cheers,
Choppie
I think what you want to do is this
This will set the $date_day to something like 21 March or 1 April and then removing the [] around the echo will make it stop outputting "Array".
As for the AM/PM it could be a timezone issue, you should check your timezone settings for your site.
This will set the $date_day to something like 21 March or 1 April and then removing the [] around the echo will make it stop outputting "Array".
As for the AM/PM it could be a timezone issue, you should check your timezone settings for your site.
I'm still not any closer to solving this. I've gotten as far as figuring out that perhaps the group should be done where the SQL query is written and then the sort perhaps in the PHP template?
I’ve been scratching in files trying to figure this out. I’ve established so far that the data that is displayed in my ProEvents List block is stored in btproeventdates table.
So trying to find the SQL query so I can add GROUP BY date, I started with proevents/blocks/pro_events_list/controller.php - I can see only one select statement for a table, but this table is not btproeventdates but btProEventList instead as seen below.
I then tried going to proevents/controllers/helpers/eventify.php (I have no idea what this is actually used for) and could see a query to the DB table in question used (btproeventdates). However I think the eventify.php file is used to view, search and sort your uploaded events in the dashboard so not the “user display”. Here is the code where the btproeventdates table is used:
I then found a reference to the btproeventdates table in proevents/src/Models/EventList.php but reading the comments in the code which talk about filters, I’m pretty sure this is back-end stuff also.
In my view.php file of proevents/blocks/pro_events_list (which is the file controlling the output to the user) I can see at the top a reference to “eventify” but not sure if this is the same as the eventify.php file previously mentioned?
There are no other files within proevents/ which I could find with reference to the correct table.
I would really appreciate some help if there is anyone out there who understands any of this or has used this block. I’m really out of things to try.
Thanks,
Choppie