get category_id and the id's of it's sub categories

This forum is for general questions about extensions for Joomla! 2.5.
Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
User avatar
djemmers
Joomla! Intern
Joomla! Intern
Posts: 90
Joined: Thu May 22, 2008 7:09 am

get category_id and the id's of it's sub categories

Postby djemmers » Fri Jan 28, 2011 8:13 am

hi

I am wondering how I can get the current category_id of a page and the id's of the subcategories throug php commands
I found this in the API, but don't know how to use it:
http://docs.joomla.org/API16:JCategories
One fine day I'll be a Joomla 'expert' !
Tornooi.net : Sports database (tournaments and camps) for Belgium and the Netherlands

bongobongo
Joomla! Explorer
Joomla! Explorer
Posts: 345
Joined: Mon Dec 15, 2008 11:10 am

Re: get category_id and the id's of it's sub categories

Postby bongobongo » Wed Feb 02, 2011 1:15 pm

Me to want to know this...

Anybody that can help?

Turbo2011
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Fri Jan 21, 2011 3:02 pm

Re: get category_id and the id's of it's sub categories

Postby Turbo2011 » Thu Feb 03, 2011 11:04 am

It depends wether your menu item points to an single article or a blog or list view of articles in a category.
in second case you can catch the id by:

Code: Select all

$catID = JRequest::getVar('id')


Be aware that if you display a single article you will get the article id, not category id. You can check in which view you are by:

Code: Select all

$view = JRequest::getVar('view');


returning 'article' string when displaying single article.

Once having the category id it should be easy to get the child categories:
http://docs.joomla.org/API16:JCategories/get

When in Single Article View, i actually dont know how to retrieve the category id, im sorry

User avatar
djemmers
Joomla! Intern
Joomla! Intern
Posts: 90
Joined: Thu May 22, 2008 7:09 am

Re: get category_id and the id's of it's sub categories

Postby djemmers » Thu Feb 03, 2011 1:51 pm

tnx for the reply,
but I can't seem to get the child categories...

I tried

Code: Select all

jimport( 'joomla.application.categories' );
$catID = JRequest::getVar('id');
echo $catID."<br/>";
print_r(get($catID));

gives:Fatal error: Call to undefined function get()

so I am not calling the get function correctly.

can you tell me what I should do?
One fine day I'll be a Joomla 'expert' !
Tornooi.net : Sports database (tournaments and camps) for Belgium and the Netherlands

Turbo2011
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Fri Jan 21, 2011 3:02 pm

Re: get category_id and the id's of it's sub categories

Postby Turbo2011 » Thu Feb 03, 2011 2:15 pm

Try JCategories::get($catID)

User avatar
djemmers
Joomla! Intern
Joomla! Intern
Posts: 90
Joined: Thu May 22, 2008 7:09 am

Re: get category_id and the id's of it's sub categories

Postby djemmers » Thu Feb 03, 2011 2:29 pm

Code: Select all

$catID = JRequest::getVar('id');
echo $catID;
$test=JCategories::get($catID);
print_r($test);

gives

78
Fatal error: Call to undefined method plgSystemJumi::_load() in /home/tornooi/domains/tornooi.net/public_html/joomla160/libraries/joomla/application/categories.php on line 160

does that mean the problem lies in jumi?
I think not because I can do things like:

Code: Select all

$exdb = & JDatabase::getInstance( $option );
if ( is_a($exdb, 'JException') ) {
         $msg = 'Unable to connect to the target database';
         echo $msg;
       error_log( $msg );
         return false;
      }
One fine day I'll be a Joomla 'expert' !
Tornooi.net : Sports database (tournaments and camps) for Belgium and the Netherlands

User avatar
djemmers
Joomla! Intern
Joomla! Intern
Posts: 90
Joined: Thu May 22, 2008 7:09 am

Re: get category_id and the id's of it's sub categories

Postby djemmers » Thu Feb 03, 2011 2:50 pm

I tried:

Code: Select all

$catID = JRequest::getVar('id');
echo $catID;
$test= & JCategories::get($catID);
print_r($test);

same error:
Fatal error: Call to undefined method plgSystemJumi::_load() in /home/tornooi/domains/tornooi.net/public_html/joomla160/libraries/joomla/application/categories.php on line 160
One fine day I'll be a Joomla 'expert' !
Tornooi.net : Sports database (tournaments and camps) for Belgium and the Netherlands

Turbo2011
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Fri Jan 21, 2011 3:02 pm

Re: get category_id and the id's of it's sub categories

Postby Turbo2011 » Thu Feb 03, 2011 3:07 pm

jimport('joomla.application.categories');
$categories = new JCategories('com_content');
$subCategories = $categories->get($catID);

that worked for me, where 'com_content' is the extension

*updated*

User avatar
djemmers
Joomla! Intern
Joomla! Intern
Posts: 90
Joined: Thu May 22, 2008 7:09 am

Re: get category_id and the id's of it's sub categories

Postby djemmers » Fri Feb 04, 2011 8:39 am

ok, now the error is gone but I can't get the subcategories.

Code: Select all

$catID = JRequest::getVar('id');
echo $catID;
jimport('joomla.application.categories');
$categories = new JCategories('com_content');
print_r($categories);
$subCategories = $categories->get($catID);
print_r($subCategories);

gives
78
JCategories Object ( [_nodes:protected] => [_checkedCategories:protected] => [_extension:protected] => c [_table:protected] => c [_field:protected] => c [_key:protected] => c [_statefield:protected] => c [_options:protected] => com_content )

How do I know what extention I use?

and what does the "protected" mean?
One fine day I'll be a Joomla 'expert' !
Tornooi.net : Sports database (tournaments and camps) for Belgium and the Netherlands

User avatar
djemmers
Joomla! Intern
Joomla! Intern
Posts: 90
Joined: Thu May 22, 2008 7:09 am

Re: get category_id and the id's of it's sub categories

Postby djemmers » Mon Feb 14, 2011 12:58 pm

I also posted this in a dutch forum
http://forum.dutchjoomla.org/showthread.php?t=124152
no luck yet...
One fine day I'll be a Joomla 'expert' !
Tornooi.net : Sports database (tournaments and camps) for Belgium and the Netherlands

emptypixels
Joomla! Apprentice
Joomla! Apprentice
Posts: 31
Joined: Mon Nov 17, 2008 2:41 pm

Re: get category_id and the id's of it's sub categories

Postby emptypixels » Wed Apr 20, 2011 12:53 am

This worked out for me.
Hopefully this will work for others. ;)

To get the category id (whether viewing article or category blog), I use this...

Code: Select all

function getArticleCategory($id) {
      $db = & JFactory::getDBO();
      if (JRequest::getCmd('view', 0) == "category") {
         return JRequest::getInt('id');
      } elseif (Jrequest::getCmd('view', 0) == "article") {
         $temp=explode(":",JRequest::getInt('id'));
         $query = "SELECT catid FROM #__content WHERE id = ".$temp[0];
         $db->setQuery( $query );
         $items  = $db->loadResult();
         return $items;
      }
   }



To get the sub categories of the current category (or category of the article), use this...

Code: Select all

function getList(&$params) {
      $categories = JCategories::getInstance('Content');
      $article_id = JRequest::getInt('id');
      $category = $categories->get(modArticleSubcategoriesHelper::getArticleCategory($article_id));
      
      $items = $category->getChildren();
      if($params->get('count', 0) > 0 && count($items) > $params->get('count', 0)) {
         $items = array_slice($items, 0, $params->get('count', 0));
      }
      return $items;
   }

User avatar
djemmers
Joomla! Intern
Joomla! Intern
Posts: 90
Joined: Thu May 22, 2008 7:09 am

Re: get category_id and the id's of it's sub categories

Postby djemmers » Wed Apr 20, 2011 7:34 am

hi tnx for offering your sollution.
But it doesn't seem to work for me
first part works fine (but that was the part I got working too)

for the second part I get following error:
Fatal error: Class 'modArticleSubcategoriesHelper' not found in /home/tornooi/domains/tornooi.net/public_html/joomla160/jumifiles/events.php on line 63
and line 63 is:

Code: Select all

$category = $categories->get(modArticleSubcategoriesHelper::getArticleCategory($article_id));


I also noticed that $params is empty
As I said, I use jumi to execute the file
if I use print_r on $categories I get:

Code: Select all

ContentCategories Object ( [_nodes:protected] => [_checkedCategories:protected] => [_extension:protected] => com_content [_table:protected] => #__content [_field:protected] => catid [_key:protected] => id [_statefield:protected] => state [_options:protected] => Array ( [table] => #__content [extension] => com_content [access] => true [published] => 1 ) )


anny suggestions?
One fine day I'll be a Joomla 'expert' !
Tornooi.net : Sports database (tournaments and camps) for Belgium and the Netherlands

emptypixels
Joomla! Apprentice
Joomla! Apprentice
Posts: 31
Joined: Mon Nov 17, 2008 2:41 pm

Re: get category_id and the id's of it's sub categories

Postby emptypixels » Wed Apr 20, 2011 2:59 pm

Sorry about that.
I just grabbed that code from a class I wrote a while ago.
I've attached the module I wrote to sort this out. Let me know if you can find anything helpful from this.
You may be able to plug this in and get what you're looking for, or at the very least grab some code that might help.
You do not have the required permissions to view the files attached to this post.

User avatar
djemmers
Joomla! Intern
Joomla! Intern
Posts: 90
Joined: Thu May 22, 2008 7:09 am

Re: get category_id and the id's of it's sub categories

Postby djemmers » Fri Apr 22, 2011 9:20 am

that is exactly what I am looking for!
tnx
but I keep having the same problem:

Fatal error: Call to a member function get() on a non-object in /home/tornooi/domains/tornooi.net/public_html/joomla160/jumifiles/events.php on line 41

it always is here:

if($params->get('count', 0) > 0 && count($items) > $params->get('count', 0)) {

the $params is empty ...
where is it's value set?
One fine day I'll be a Joomla 'expert' !
Tornooi.net : Sports database (tournaments and camps) for Belgium and the Netherlands

emptypixels
Joomla! Apprentice
Joomla! Apprentice
Posts: 31
Joined: Mon Nov 17, 2008 2:41 pm

Re: get category_id and the id's of it's sub categories

Postby emptypixels » Fri Apr 22, 2011 2:28 pm

The params are set when you configure the module.
By selecting the number of categories, etc., the params should be set.

User avatar
djemmers
Joomla! Intern
Joomla! Intern
Posts: 90
Joined: Thu May 22, 2008 7:09 am

Re: get category_id and the id's of it's sub categories

Postby djemmers » Tue Apr 26, 2011 8:04 am

ok, tnx

I'll try to install the module instad of taking bits of the code.
and see where that gets me.
One fine day I'll be a Joomla 'expert' !
Tornooi.net : Sports database (tournaments and camps) for Belgium and the Netherlands


Return to “Extensions for Joomla! 2.5”

Who is online

Users browsing this forum: No registered users and 14 guests