Frontend display of category name in event properties

  • Christoph
  • Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
5 years 10 months ago - 5 years 10 months ago #113364 by Christoph
Hello,

I would like to display the name of a certain category in the event detail, list and table frontend. Now my approach would be to add the category id to the layout variables. Is that the right way and how do I do that?

Made an override in /html/com_eventbooking/common/event_properties.php

Thank you for help,
Chris
Last edit: 5 years 10 months ago by Christoph.

Please Log in or Create an account to join the conversation.

More
5 years 10 months ago #113381 by Tuan Pham Ngoc
Replied by Tuan Pham Ngoc on topic Frontend display of category name in event properties
Hi Chris

In the layout, try to use $item->categories, it stores list of all categories assigned to that event. Base on that, I guess you can write code to display categories information in the format you want

Regards,

Tuan

Please Log in or Create an account to join the conversation.

  • Christoph
  • Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
5 years 10 months ago #113532 by Christoph
Thank you for your message. I hoped the solution would be not that difficult. The category name should be part of the event detail information.
I tried:
<tr>
<td><strong>Category></strong>
</td><td><?php echo $item->categories; ?></td>
</tr>
But it just shows the word "array".

On categories_events.php I found this code and copied it:
<a href="<?php echo JRoute::_(EventbookingHelperRoute::getCategoryRoute($category->id, $Itemid)); ?>" class="eb-category-title-link">
<?php echo $category->name; ?></a>

Bu it also does not work.

Please provide the code to display the category name. I searched for it on the knowledge bise as well bt could not find any hint. Thank you

Please Log in or Create an account to join the conversation.

More
5 years 10 months ago - 5 years 10 months ago #113544 by James Riley
You're very close!!

Whenever you run into an "array" or "object" result when echoing a variable, that means that it is an array or object and can't be displayed with a simple echo command.

However, you can use print_r() to display the content of such variables:
Code:
<pre><?php print_r( $item->categories); ?></pre>
(The pre tags format it into a multi-line output rather than a single inline format, making it easier to read.)

On my site this outputs:
Code:
Array( [0] => stdClass Object ( [id] => 4 [name] => Other Events & Retreats [alias] => other-events-retreats ) )

So, now we can see the array keys and values instead of "array", and the [ name ] key in category[0] (edited to correct mistake) is what we'll need to reference. Modifying your original code as follows should work (code edited to correct mistake):
Code:
<tr> <td><strong>Category></strong></td> <td><?php echo $item->categories[0]->name; ?></td> </tr>

James Riley .: EventBooking user since 2014 ::: JoomDonation user since 2016 :.
.: grfx & web design / IT / AV @ St. Therese Institute of Faith and Mission, Bruno, SK, Canada :.
Last edit: 5 years 10 months ago by James Riley. Reason: Corrections
The following user(s) said Thank You: Christoph

Please Log in or Create an account to join the conversation.

  • Christoph
  • Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
5 years 10 months ago - 5 years 10 months ago #113549 by Christoph
Thank you James, your example was of great help to understand the problem. May be I'm too close to see it right. I used print_r() and got this:
Code:
Array ( [0] => stdClass Object ( [id] => 1 [name] => Afternoon activities [alias] => AA ) [1] => stdClass Object ( [id] => 29 [name] => Dance [alias] => dance ) [2] => stdClass Object ( [id] => 3 [name] => Music [alias] => music ) [3] => stdClass Object ( [id] => 14 [name] => P2 [alias] => p2 ) [4] => stdClass Object ( [id] => 15 [name] => P3 [alias] => p3 ) [5] => stdClass Object ( [id] => 33 [name] => English [alias] => english ) [6] => stdClass Object ( [id] => 32 [name] => German [alias] => german ) [7] => stdClass Object ( [id] => 41 [name] => Greek [alias] => greek ) [8] => stdClass Object ( [id] => 6 [name] => Monday [alias] => monday ) )

Some important information is given by subcategories of and the values should be displayed in the event details:

- Topics: "Dance", "Music"
- Recurring Day: "Monday"
- Grade: "P2", "P3"
- Spoken language: "English", "German", "Greek"

How do I get the 4 subcategory values into the details table. Especially since the array id number is not always the same, depending on which categories are set for each event . Another event might have these subcategory arrays:
Code:
Array ( [0] => stdClass Object ( [id] => 1 [name] => Afternoon activities [alias] => AA ) [1] => stdClass Object ( [id] => 30 [name] => Crafting [alias] => crafting ) [2] => stdClass Object ( [id] => 13 [name] => P1 [alias] => p1 ) [3] => stdClass Object ( [id] => 33 [name] => English [alias] => english ) [4] => stdClass Object ( [id] => 10 [name] => Friday [alias] => friday ) )

The reason why I use categories for Topic, Recurring Day, Grade and Spoken language is to filter the events with the available layouts. But only with the ability to display them in event details the Events Booking Component makes sense for my use case.

Any hints how to figure out the right way to display the values in the frontend would be highly appreciated, thank you!!
Last edit: 5 years 10 months ago by Christoph.

Please Log in or Create an account to join the conversation.

More
5 years 10 months ago - 5 years 10 months ago #113551 by James Riley
I missed one level of the array in the code I originally posted -- the very first [0] key'd object. To access the category name, you'd have to use
Code:
echo $item->categories[0]->name

James Riley .: EventBooking user since 2014 ::: JoomDonation user since 2016 :.
.: grfx & web design / IT / AV @ St. Therese Institute of Faith and Mission, Bruno, SK, Canada :.
Last edit: 5 years 10 months ago by James Riley.

Please Log in or Create an account to join the conversation.

More
5 years 10 months ago - 5 years 10 months ago #113553 by James Riley
Okay... I see now how you're wanting to use additional categories as sort of a sub-grouping system.

Use a foreach loops to run through the array, and compare each ID to a list of IDs, adding the matching [name] to a variable for each "subgroup".

For example, you could do something like this:
Code:
<?php $catTopicIDs=array(1,3,29 /* add other "topic" IDs */ ); $catTopic = ''; $catLanguageIDs=array(33,32,41 /* add other "langauge" IDs */ ); $catLanguage = ''; // etc foreach($item->categories as $category) { // check if $category contains one of the Category Topics ids if (in_array($category['id'],$catTopicIDs)) { if ($catTopic) $catTopic .= ', '; // add a comma if there is already previous data added to this variable $catTopic .= $category['name']; } // check if $category contains one of the Category Langauage ids if (in_array($category['id'],$catLanguageIDs)) { if ($catLanguage) $catLanguage .= ', '; // add a comma if there is already previous data added to this variable $catLanguage .= $category['name']; } // etc etc etc for the rest of the "sub-groups" } ?>

...and then later in your table structure, echo those string variables that you've built ($catTopic, $catLanguage, etc).

James Riley .: EventBooking user since 2014 ::: JoomDonation user since 2016 :.
.: grfx & web design / IT / AV @ St. Therese Institute of Faith and Mission, Bruno, SK, Canada :.
Last edit: 5 years 10 months ago by James Riley. Reason: Fixed missing ')' in the "if in_array" lines
The following user(s) said Thank You: Christoph

Please Log in or Create an account to join the conversation.

  • Christoph
  • Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
5 years 10 months ago #113568 by Christoph
Yes, that is the logic needed. You're amazing! I think I understand your input, according to I made this:
Code:
foreach ($item->categories as $category) { // check if $category contains one of the Category Topics ids if (in_array($category['id'],$catTopicIDs)) { if ($catTopic) { $catTopic .= ', '; // add a comma if there is already previous data added to this variable } $catTopic .= $category['name']; } }

After running the code in the event_properties.php file, I got this fatal error: - Cannot use object of type stdClass as array

I'm very much a php newbie, but what I understand is that the in_array() function does not work with the stdClass object. What syntax should I use for accessing data? Thank you!

Please Log in or Create an account to join the conversation.

More
5 years 10 months ago #113570 by Tuan Pham Ngoc
Replied by Tuan Pham Ngoc on topic Frontend display of category name in event properties
Hi

There is a small typo in the code which James proposed. Try to change $category to $category->id and it should be OK

Regards,

Tuan
The following user(s) said Thank You: Christoph

Please Log in or Create an account to join the conversation.

  • Christoph
  • Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
5 years 10 months ago #113573 by Christoph
Perfect, it works! Thank you very much for your help, @Tuan and @James

Please Log in or Create an account to join the conversation.

Moderators: Tuan Pham Ngoc