-
Showing registered users in EventList module
Posted on February 10th, 2009 3 comments
When using EventList module in conjunction with EventList 1.0RC component for Joomla 1.5, I missed the possibility of displaying in the module the number of registered attendees to each event, together with the already showing date, time, event name, etc.My approach to the issue was to add some extra PHP code to the EventList Mod template; concretely the code below. But before you attempt to try the same in your website, remember to backup the affected file.
Retrieving the data
The modifications are done to the template file of the mod, found at:
modules/mod_eventlist/tmpl/default.php
I added this code right after the line which contains <?php foreach ($list as $item) :
$eid = $item->id; // Query database $query = "SELECT uid FROM #__eventlist_register WHERE event = $eid"; $db->setQuery($query); if (!$db->query()) { echo $db->stderr(); return false; } // Get the results $rows = $db->loadObjectList(); foreach ( $rows as $row ) { // Increment $n++; } $attendees = $n; // Reset counter $n = 0;What the code does is the following: for each event displayed in the module, a query is made to the database requesting the user id’s (uid) from all the rows whose event id (eid) matches with the event id of the one currently being printed to the list of events. As an example, if the event being printed is event 100, the query will select all the rows with registrations to event 100.
(Note that the fact we’re retrieving the user id (uid) can be useful to check too if the user is registered in the event and display accordingly, but more code is required to achieve that).
Line 1 of the code above gets the specific event id and assigns it to $eid, for using it in the database query.
Line 20 makes an increment of 1 for each matching row found. This is synonimous of saying that we’re incrementing by one the value of $n for each user found to be registered in the event.
Line 24 passes the value of $n to another variable. This is because $n will be resetted before its value its actually printed; if this line is not included the value of $n would always be echoed as “0″ regardless of the number of registered users found. See the second piece of code below to understand what I mean.
Line 28 of the code resets the value of the variable. This is so because otherwise, if you’re showing multiple events in the module, each successive event will make increments (in line 20) starting from the total number of matching rounds found in the previous event (if the first event shown has four attendees and the second has one, the displayed number of attendees for the latter would be five).
This code deals with the counting of registrations. To show the number of attendees in the frontend some additional code is required, the code is below.
Formatting and displaying the result
This code is to be placed wherever you want the result to be shown in the template (I put it in the line right under the name of the event, as seen in the example image).
<? // Singular or plural? if ($attendees != 1) { $g = "attendee"; } else { $g = "attendees"; } // Echo results echo "$attendees $g"; ?>The first part of this code simply checks the total number of registered users in order to merely choose whether the suffix should be in singular or plural (namely, either X attendee or X attendees). We want it to be “attendees” except if the number is 1, then we want it to be “attendee”.
The second part of the code echoes the results put together. As an example, it would print something like “1 attendee”, or “4 attendees” or “0 attendees”.
The module should now show how many users have registered to each of the events it lists.
Final notes
Despite there might be other (and perhaps better) ways to achieve the same effect with a single query to the database, the way here explained queries the database once for each event fetched for display in the EventList module. This is, in a list of five events, the database will be queried five times. This can make the page be a bit slower, but in my case this was not as important as the effect, especially because the number of users being handled is not so big. This can be a bit controlled by setting the maximum number of events to be shown to a small number (I have it to 5), from the module settings in the backend. Selecting the module to show in a number of pages instead of all of them can also avoid making unnecessarily slow those which do not require displaying the module.
Who knows if in the next release of EventList this possibility will come on default?
Related posts:
- Hide modules from registered users in Joomla 1.5
- Hide content from registered users in PHPBB3
- OpenCart: display stock levels in storefront, as numbers or colour icons
Computers & programming, Content Management Systems, HTML, MySQL, PHP custom, eventlist, fields, joomla, module, mysql, php, schlu, users, web3 responses to “Showing registered users in EventList module”
-
Chris K August 11th, 2009 at 20:38
With new release of EventList (moved to com versus mod) have you relooked at how to implement this? Seems kinda of logical to be able to show total registered attendees, so this is a great feature.
-
Hi Chris,
Nope, unfortunately I am using a too heavily modified (customised) version of Eventlist 1.0RC and haven’t considered upgrading to the latest; therefore I ignore if it works in the same way or not, but I could very well imagine the logic behind for making those changes must be the same…
If I eventually upgrade, I will for sure post any new findings here
Regards
-
This looks like a good job done. Thanks alot
Leave a reply



