Keyword

Add PHP Condition Recursively

  • Joe Campbell
  • Joe Campbell's Avatar Topic Author
  • Offline
  • Platinum Member
More
8 years 1 month ago #156895 by Joe Campbell
Add PHP Condition Recursively was created by Joe Campbell
How can I add a condition recursively to a category and its sub-categories.

So instead of doing:

if($this->item->category->name == 'AAA' OR $this->item->category->name == 'BBB' OR $this->item->category->name == 'CCC')

How do I do something like this

if recursive(category->name ==('AAA'))

Is this possible?

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

  • Mohamed Abdelaziz
  • Mohamed Abdelaziz's Avatar
  • Offline
  • Platinum Member
  • Joomla Developer
More
8 years 1 month ago #156899 by Mohamed Abdelaziz
Replied by Mohamed Abdelaziz on topic Add PHP Condition Recursively
I don't think there is a straight forward method to do it.
At least it needs a loop to traverse the category and its sub-categories.

Multiple Extra Fields Groups for K2
AutoMeta for K2
Chained Fields for K2
More K2 Extensions In My Extensions Store

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

  • Joe Campbell
  • Joe Campbell's Avatar Topic Author
  • Offline
  • Platinum Member
More
8 years 1 month ago #156901 by Joe Campbell
Replied by Joe Campbell on topic Add PHP Condition Recursively
Ok - thanks @MohamedAbdelaziz

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

  • Joe Campbell
  • Joe Campbell's Avatar Topic Author
  • Offline
  • Platinum Member
More
7 years 10 months ago #159222 by Joe Campbell
Replied by Joe Campbell on topic Add PHP Condition Recursively
I would greatly appreciate if someone would provide a PHP Snippet :)

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

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
7 years 10 months ago #159229 by Krikor Boghossian
Replied by Krikor Boghossian on topic Add PHP Condition Recursively
The most straightforward way to do this would be to map the categories ID into a simple array - php.net/manual/en/language.types.array.php

Then use the in_array() - php.net/manual/en/function.in-array.php - check for the category's id. $this->item->category->id.

This is more bulletproof than the name method. Furthermore you won't have to query the database in order to retrieve the IDs manually (-1 query).

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

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

  • Joe Campbell
  • Joe Campbell's Avatar Topic Author
  • Offline
  • Platinum Member
More
7 years 10 months ago #159232 by Joe Campbell
Replied by Joe Campbell on topic Add PHP Condition Recursively
Thanks @Krikor

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

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
7 years 10 months ago #159240 by Krikor Boghossian
Replied by Krikor Boghossian on topic Add PHP Condition Recursively
You 're welcome buddy :)

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

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


Powered by Kunena Forum