Hi,
I would like to known if there is an easy way to find a category id when I've only it's name.
A solution (but not very usefull) is to get all the categories, compare their name, extract the correct id.
Watch
GitHub Core
Show your support for Zikula! Sign up at Github account and watch the Core project!
GitHub Modules
- internetking created topic »password problem« 25. May
- mesteele101 responded to »ERR (3): E_USER_ERROR: Smarty error: [in pagesvar:pagesitem2en line XXX]…« 25. May
- mazdev responded to »Pages 2.5.0 and updating - Page not found« 25. May
- ehdwma created topic »Hide "Register new account" and change template to 3 col« 25. May
- mesteele101 responded to »Zikula 1.3.3 - Selecting a category in Pages not working« 25. May
- mdee created topic »How to implement returnpage ?« 25. May
- nestormateo responded to »Fillters in Clip« 24. May
Zikula Blog
- Anatomy of Open Source Projects on Mar 07
- Continuous Review on Mar 01
- Not Invented Here on Feb 24
- How to Contribute Your Code at Github on Jan 13
- 10 Steps to Coding-Nirvana: Tips for Successful Module Writing on Nov 12
- Submitting Bug Report Tickets That Get Results on Aug 17
- Cozi Tricks #1: Syntax Highlighting on Aug 07
Login
How to get a category id by using it's name ?
-
- Rank: Team Member
- Registered: Sep 05, 2006
- Last visit: May 06, 2010
- Posts: 78
-
- Rank: Developer
- Registered: Aug 23, 2003
- Last visit: May 31, 2010
- Posts: 1428
When you know the category property, you can get the category by using getCategoryByPath
In News the following is used for example in pnuser.php, where $cat and $prop are inputs. So $cat can be the name of the category (or the ID) and $prop is the category property name
Code
...
// get the categories registered for the News stories
$catregistry = CategoryRegistryUtil::getRegisteredModuleCategories('News', 'stories');
$properties = array_keys($catregistry);
$lang = pnUserGetLang();
// validate the property
// and build the category filter - mateo
if (!empty($prop) && in_array($prop, $properties) && !empty($cat)) {
if (!is_numeric($cat)) {
$rootCat = CategoryUtil::getCategoryByID($catregistry[$prop]);
$cat = CategoryUtil::getCategoryByPath($rootCat['path'].'/'.$cat);
} else {
$cat = CategoryUtil::getCategoryByID($cat);
}
...
Does that help?
--
campertoday.nl, Module development, Dutch Zikula Community -
- Rank: Team Member
- Registered: Sep 05, 2006
- Last visit: May 06, 2010
- Posts: 78
Yes but with this solution you've to put the $prop into the URL, I would like to have some urls as short as possible so I prefered my first solution. But I think that it could be good to have a class like getCategoryByID but for category name getCategoryByName. -
- Rank: Developer
- Registered: Aug 23, 2003
- Last visit: May 31, 2010
- Posts: 1428
I you have a fixed property you can hard code that in of course, that would lead to a short URL. It seems that you're aiming for a solution where you already know the property beforehand.
The flexibiltiy of categories and properties has the disadvantage that searching by name is not direct. The result can be multiple category id's when leaving out the properpty.
--
campertoday.nl, Module development, Dutch Zikula Community -
- Rank: Team Member
- Registered: Sep 05, 2006
- Last visit: May 06, 2010
- Posts: 78
Thanks for your answers, I think that getCategoryByPath is the answer to what I want to do.
I thought that with getCategoryByPath we can only used the path with the ids of the categories but it's wrong, we can use the names of the categories.
Just a last question, if the property is not set it will used the default one (Global category) ? -
- Rank: Developer
- Registered: Aug 23, 2003
- Last visit: May 31, 2010
- Posts: 1428
I think you will need the parent path as well. For instance when I look at the path of some category in my site (via category administration) I see:
/__SYSTEM__/Modules/Global/Site
Where Site is the actual category name. And since I'm using the Global category for this category within News, the global category path is also needed.
When opening the category registry, you will see the path of the property you are using for a cerain module. So in the example above in my News I use the global category.
That is what this part:
Code
$rootCat = CategoryUtil::getCategoryByID($catregistry[$prop]);
$cat = CategoryUtil::getCategoryByPath($rootCat['path'].'/'.$cat);
does. It finds the rootcategorypath by means of the property name, which you know already and then adds that to the requested catname for the full path. The full path is then fed to getCategoryByPath and gives the category as object.
--
campertoday.nl, Module development, Dutch Zikula Community -
- Rank: Team Member
- Registered: Sep 05, 2006
- Last visit: May 06, 2010
- Posts: 78
Yes I confirm with getCategoryByPath we must use the full path.
Thanks a lot for your help erik, all is working fine like I want now!
- Moderated by:
- Support
Users on-line
- 0 users
This list is based on users active over the last 60 minutes.
